如何从REST服务(JavaFX、RestEasy、Spring Security)发送和接收数据
我用SpringBoot和JavaFX创建应用程序作为客户端。我正在使用RestEasy从REST服务发送和接收数据。当我不使用SpringSecurity时,数据通常被发送和接收。当我添加Spring安全性时,我无法访问数据,也不知道如何使用RestEasy访问数据 我的测试REST Api:如何从REST服务(JavaFX、RestEasy、Spring Security)发送和接收数据,java,spring,spring-boot,javafx,resteasy,Java,Spring,Spring Boot,Javafx,Resteasy,我用SpringBoot和JavaFX创建应用程序作为客户端。我正在使用RestEasy从REST服务发送和接收数据。当我不使用SpringSecurity时,数据通常被发送和接收。当我添加Spring安全性时,我无法访问数据,也不知道如何使用RestEasy访问数据 我的测试REST Api: @RequestMapping(value = "/test", method = RequestMethod.POST) public ResponseEntity test(@RequestBody
@RequestMapping(value = "/test", method = RequestMethod.POST)
public ResponseEntity test(@RequestBody Usr usr) {
System.out.println(usr);
return new ResponseEntity("Ok", HttpStatus.OK);
}
@RequestMapping(value = "/test2", method = RequestMethod.GET)
public ResponseEntity test2() {
System.out.println("Test2");
return new ResponseEntity("OkGet", HttpStatus.OK);
}
我的代码,与此连接:
在/测试端点上:
ClientRequest clientRequest = new ClientRequest(applicationUrl);
clientRequest.body("application/json", usr);
String o = (String)clientRequest.post(Usr.class).getEntity(String.class);
System.out.println("Resp: " + o);
ClientRequest clientRequest = new ClientRequest(applicationUrl2);
String o = (String)clientRequest.get().getEntity(String.class);
System.out.println("Resp: " + o);
在/test2端点上:
ClientRequest clientRequest = new ClientRequest(applicationUrl);
clientRequest.body("application/json", usr);
String o = (String)clientRequest.post(Usr.class).getEntity(String.class);
System.out.println("Resp: " + o);
ClientRequest clientRequest = new ClientRequest(applicationUrl2);
String o = (String)clientRequest.get().getEntity(String.class);
System.out.println("Resp: " + o);
这段代码在我不使用Spring安全性的情况下工作
我正在尝试类似的操作,但不起作用,返回空字符串,在服务器端,控制台中的数据不会打印出来:
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8090),
new UsernamePasswordCredentials("a", "a")
);
ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(client);
ClientRequest clientRequest = new ClientRequest(applicationUrl2, executor);
String o = (String)clientRequest.get().getEntity(String.class);
System.out.println("Resp: " + o);
更新-添加Spring安全配置
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/test", "/test2").authenticated()
.and().formLogin();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("a")
.password("{noop}a")
.roles("USER");
}
我想我有一个解决办法。我删除了
.formLogin()
并添加了
.and().httpBasic()
.and().csrf().disable();
第一个问题可能是重定向到登录页面,然后是缺乏授权。
我将离职,这可能对某人有用D发布您的安全配置,没有它,我们无法提供帮助。很可能是某个spring安全拦截器阻止了您的请求。@上面添加了最低点配置。请提供一个示例来说明此问题。