我正在使用React / Spring Rest并进行编程注销。登录工作正常,当用户单击注销时,请求将发送到控制器并注销。我遇到的问题是,在控制器内部的注销方法中,我从安全上下文获取NULL。看来旧的会议已经过去了。我如何获取/保留SecurityContextHolder上的用户信息。此外,我正在使用spring-redis来维护会话。成功登录后,会话信息将存储在redis上,但SecurityContext将变为null。
一些代码段(注销):
HttpSession session = request.getSession(false);
//session is null
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
// authentication.getName is null
if (session != null) {
session.invalidate();
session.setMaxInactiveInterval(0);
}
SecurityContextHolder.clearContext();
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : request.getCookies()) {
if ("JSESSONID".equals(cookie.getName())) {
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}`
还有httpCOnfig:
http
.authorizeRequests()
.antMatchers("/api/user/**").hasAnyRole(ROLE_ADMIN, ROLE_REGISTERED_USER)
.antMatchers("/api/admin/**").hasRole(ROLE_ADMIN)
.antMatchers("/api/public/**").permitAll()
.anyRequest().authenticated()
.and()
.csrf().disable()
.cors()
.and()
.httpBasic();
}