Java Spring注销功能未注销
我试图编写一个注销函数,将用户返回主页(“/”),但当我按下按钮时,它什么也不做。我一直在关注教程,但它们似乎都有一个问题,那就是它对我不起作用 安全配置代码Java Spring注销功能未注销,java,spring,spring-security,Java,Spring,Spring Security,我试图编写一个注销函数,将用户返回主页(“/”),但当我按下按钮时,它什么也不做。我一直在关注教程,但它们似乎都有一个问题,那就是它对我不起作用 安全配置代码 @覆盖 受保护的无效配置(HttpSecurity http)引发异常{ http.authorizeRequests() .antMatchers(“/login”) .permitAll() .antMatchers(“/admin”) .hasAnyRole(“管理员”、“用户”) .及() .formLogin() .login页
@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http.authorizeRequests()
.antMatchers(“/login”)
.permitAll()
.antMatchers(“/admin”)
.hasAnyRole(“管理员”、“用户”)
.及()
.formLogin()
.login页面(“/login”)
.defaultSuccessUrl(“/admin”)
.failureUrl(“/login?error=true”)
.permitAll()
.及()
.logout().logoutUrl(“/login”)
.permitAll()
.及()
.csrf()
.disable()
;
返回;
}
控制器
@RequestMapping(value=“/logout”,method=RequestMethod.GET)
公共字符串注销页(HttpServletRequest请求,HttpServletResponse响应){
Authentication auth=SecurityContextHolder.getContext().getAuthentication();
如果(auth!=null){
新的SecurityContextLogoutHandler()。注销(请求、响应、身份验证);
}
返回“重定向:/login?注销”;
}
最后存储JSP注销
任何帮助都将不胜感激。还有其他一些技巧,因为这是我在这里的第一篇文章:)注销()配置:
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/logout.done")
.deleteCookies("JSESSIONID")
.invalidateHttpSession(true)
或以编程方式在控制器中:
@RequestMapping(value="/logout", method = RequestMethod.GET)
public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
HttpSession session= request.getSession(false);
SecurityContextHolder.clearContext();
session= request.getSession(false);
if(session != null) {
session.invalidate();
}
for(Cookie cookie : request.getCookies()) {
cookie.setMaxAge(0);
}
return "logout";
选择其中一种方式,如果已经创建了自定义路由,则不应添加.logout()配置
在jsp上:
<a href="${pageContext.request.contextPath}/logout?${_csrf.parameterName}=${_csrf.token}">Logout</a>
按如下方式设置您的SecurityConfig:
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/login")
.permitAll()
谢谢你的回复,不幸的是它仍然不起作用。我已经将代码添加到安全配置中。谢谢,但恐怕它仍然不起作用,我已经将新的控制器代码添加到我的主控制器中,但它仍然不起作用。这可能是由于JSP中的任何内容造成的吗?@JeffereyJohnSmith尝试从浏览器而不是从JSP键入{site}/logout并检查:1)是否执行了注销2)是否可以在调试模式下通过断点捕获注销方法。并仅选择所述方法中的一种。您不应该同时提供注销配置和控制器方法。好的,如果我从url调用注销,它确实会将我注销,我需要做什么才能有一个按钮或链接来为我执行此操作?@JeffereyJohnSmith