gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:32:40 26 4
gpt4 key购买 nike

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

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]

服务器初始化器

public class SpringMvcInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {ApplicationConfig.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {WebConfig.class};
}

@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}

服务器安全初始化器

public class SpringSecurityInitializer extends
AbstractSecurityWebApplicationInitializer {
}

服务器安全配置

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DriverManagerDataSource dataSource;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
String authQuery = "select userid, authority from user where userid = ?";
String userQuery = "select userid, pw, enabled from user where userid = ?";

auth.jdbcAuthentication().dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery(userQuery)
.authoritiesByUsernameQuery(authQuery);

}

@Override
protected void configure(HttpSecurity http) throws Exception {


http
.authorizeRequests()
.antMatchers("/register").permitAll()
.antMatchers("/getconvs", "/getcontacts").hasRole("USER")
.and()
.formLogin()
.and()
.csrf().disable();
}

@Bean
public AuthenticationEntryPoint authenticationEntryPoint() {
return new de.daschner.messenger.security.AuthenticationEntryPoint();
}

@Bean
public SuccessHandler successHandler() {
return new SuccessHandler();
}

@Bean
public SimpleUrlAuthenticationFailureHandler failureHandler() {
return new SimpleUrlAuthenticationFailureHandler();
}

@Bean
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(11);
}
}

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

@RequestMapping(value="/getconvs", method={RequestMethod.GET},
produces={MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody JSONConversationResult getConvsList(HttpServletRequest request, @RequestParam(value="uid") String uid){
JSONConversationResult ret = new JSONConversationResult();
Map<String, Map<Date, String>> convs = convService.getConvsList(uid);
if (convs != null) {
ret.setConversations(convs);
ret.setMessage("OK");
ret.setError(0);
} else {
ret.setError(1);
ret.setMessage("Verbindungsfehler");
}
return ret;
}

客户端发送Login并获取Cookie

    Map<String, String> loginform = new HashMap<String, String>();
loginform.put("username", user);
loginform.put("password", pw);
HttpEntity<Map<String, String>> login = new HttpEntity<Map<String, String>>(loginform);

ResponseEntity<HttpServletResponse> response = restTemplate.exchange(
"http://localhost:8080/messenger-webapp/login",
HttpMethod.POST,
login,
HttpServletResponse.class);

HttpHeaders headers = response.getHeaders();
Set<String> keys = headers.keySet();
String cookie = "";
for (String header : keys) {
if (header.equals("Set-Cookie")) {
cookie = headers.get(header).get(0);
}
}
String jsessionid = cookie.split(";")[0];
conf.setJsessionid(jsessionid.split("=", 2)[1]);
return ret;

客户端发送 JSESSIONID 请求

    ResponseEntity<JSONConversationResult> response = restTemplate.exchange(
"http://localhost:8080/messenger-webapp/getconvs?uid=" + uid,
HttpMethod.GET,
getAuthHeader(),
JSONConversationResult.class);

JSONConversationResult ret = response.getBody();
return ret;

private HttpEntity<String> getAuthHeader() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Cookie", "JSESSIONID=" + config.getJsessionid());
return new HttpEntity<String>(requestHeaders);
}

我希望你能帮助我。

编辑:

好的,我发现问题不在于 JSESSIONID 发送不正确。但是我的登录不正确,我查询从数据库中获取用户。

正确的登录帖子

ClientHttpResponse response = restTemplate.execute(
"http://localhost:8080/messenger-webapp/login",
HttpMethod.POST,
new RequestCallback() {

@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getBody().write(("username=" + user + "&password=" + pw).getBytes());
}
},
new ResponseExtractor<ClientHttpResponse>() {

@Override
public ClientHttpResponse extractData(ClientHttpResponse response)
throws IOException {
return response;
}
});

正确的查询

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 = ?";

我希望这对其他人有帮助。如果有人有替代方案,请告诉我。

最佳答案

好的,我发现问题不在于 JSESSIONID 发送不正确。但是我的登录不正确,我查询从数据库中获取用户。

正确的登录帖子

ClientHttpResponse response = restTemplate.execute(
"http://localhost:8080/messenger-webapp/login",
HttpMethod.POST,
new RequestCallback() {

@Override
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getBody().write(("username=" + user + "&password=" + pw).getBytes());
}
},
new ResponseExtractor<ClientHttpResponse>() {

@Override
public ClientHttpResponse extractData(ClientHttpResponse response)
throws IOException {
return response;
}
});

正确的查询

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 = ?";

我希望这对其他人有帮助。如果有人有替代方案,请告诉我。

关于java - 如何通过spring 4 resttemplate发送接收到的jsessionid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24446783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com