java – 如何通过spring 4 resttemplate发送收到的jsessionid

前端之家收集整理的这篇文章主要介绍了java – 如何通过spring 4 resttemplate发送收到的jsessionid前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在客户端站点上使用JavaFX和Spring4编写信使,在服务器站点上编写Spring4.我用spring-security 3.2保护了服务器.现在我的问题:我在客户端上有一个登录页面,将登录信息发送到spring-security并接收JSESSIONID cookie.这工作正常,但当我尝试用我的请求发送JSESSIONID时,我变成了一个

  1. org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.messenger.rest.JSONConversationResult] and content type [text/html;charset=UTF-8]

服务器Inizializer

  1. public class SpringMvcInitializer extends
  2. AbstractAnnotationConfigDispatcherServletInitializer {
  3. @Override
  4. protected Class

Server SecurityInizializer

  1. public class SpringSecurityInitializer extends
  2. AbstractSecurityWebApplicationInitializer {
  3. }

Server SecurityConfig

  1. @Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Autowired
  5. private DriverManagerDataSource dataSource;
  6. @Override
  7. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  8. String authQuery = "select userid,authority from user where userid = ?";
  9. String userQuery = "select userid,pw,enabled from user where userid = ?";
  10. auth.jdbcAuthentication().dataSource(dataSource)
  11. .passwordEncoder(passwordEncoder())
  12. .usersByUsernameQuery(userQuery)
  13. .authoritiesByUsernameQuery(authQuery);
  14. }
  15. @Override
  16. protected void configure(HttpSecurity http) throws Exception {
  17. http
  18. .authorizeRequests()
  19. .antMatchers("/register").permitAll()
  20. .antMatchers("/getconvs","/getcontacts").hasRole("USER")
  21. .and()
  22. .formLogin()
  23. .and()
  24. .csrf().disable();
  25. }
  26. @Bean
  27. public AuthenticationEntryPoint authenticationEntryPoint() {
  28. return new de.daschner.messenger.security.AuthenticationEntryPoint();
  29. }
  30. @Bean
  31. public SuccessHandler successHandler() {
  32. return new SuccessHandler();
  33. }
  34. @Bean
  35. public SimpleUrlAuthenticationFailureHandler failureHandler() {
  36. return new SimpleUrlAuthenticationFailureHandler();
  37. }
  38. @Bean
  39. public AuthenticationManager authenticationManager() throws Exception {
  40. return super.authenticationManager();
  41. }
  42. @Bean
  43. public PasswordEncoder passwordEncoder() {
  44. return new BCryptPasswordEncoder(11);
  45. }
  46. }

安全“页面”的服务器请求映射

  1. @RequestMapping(value="/getconvs",method={RequestMethod.GET},produces={MediaType.APPLICATION_JSON_VALUE})
  2. public @ResponseBody JSONConversationResult getConvsList(HttpServletRequest request,@RequestParam(value="uid") String uid){
  3. JSONConversationResult ret = new JSONConversationResult();
  4. Map

客户端发送登录获取Cookie

  1. Map登录不正确,我的查询数据库获取用户.

  2. 正确的登录帖子

  3. ClientHttpResponse response = restTemplate.execute(
  4.             "http://localhost:8080/messenger-webapp/login",new RequestCallback() {
  5.                 @Override
  6.                 public void doWithRequest(ClientHttpRequest request) throws IOException {
  7.                     request.getBody().write(("username=" + user + "&password=" + pw).getBytes());
  8.                 }
  9.             },new ResponseExtractor
  10. 正确的查询

  11. String authQuery = "select u.userid,r.role_name from user u,role r,user_role a where u.dbid = a.user_id and r.dbid = a.role_id and u.userid = ?";
  12. 我希望这会有助于其他人.如果有人有其他选择,请告诉我.

最佳答案
好的我发现问题不在于JSESSIONID没有正确发送.但我的登录不正确,请告诉我.

猜你在找的Spring相关文章