我一直在寻找一种解决方案,但找不到任何解决方案,至少不是当前的一种或使用非基于XML的Spring和Spring Security配置的解决方案。
我需要实现一个将在spring注销处理程序之前使用的处理程序。我已经阅读了很多有关LogoutSuccessHandler的文章,但是在注销过滤器成功注销后调用了该文章,并且我需要访问存储在用户会话中的用户数据以执行一些数据库条目,站点注销信息等。一旦spring注销了用户,它就会丢失,因此必须在此之前。
我尝试创建自己的自定义注销类,并在我的应用程序配置类中定义它,如下所示:
@Bean
public CustomLogoutHandler customLogoutHandler() {
return new CustomLogoutHandler();
}
我的类像春天文档所说的那样扩展了LogoutHandler:
public class CustomLogoutHandler extends LogoutHandler {
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
// business logic here
}
}
这仍然无法正常工作。我在代码中放置了一个断点,但它从未被提取。有谁知道这可能是什么原因,或者我需要做些什么才能使其起作用?
要使用实现Spring的LogoutHandler.class的自定义注销处理程序,您需要让Spring知道您正在使用.addLogoutHandler的注销选项下的配置文件中使用自己的注销处理程序。我认为您错过了这一步。在安全性配置文件中:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
... // Other methods here
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.otherConfigOptions
.logout()
.addLogoutHandler(customLogoutHandler()) <- custom handler
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.otherConfigOptions....
}
}
定义bean,我将其放在SecurityConfig.class中,但我认为您可以将其放在Web或应用程序配置类中,具体取决于您如何设置项目。
@Bean
public CustomLogoutHandler customLogoutHandler() {
return new CustomLogoutHandler();
}
然后,创建您的CustomLogoutHandler.class,确保要实施LogoutHandler并覆盖注销方法。在这里,您可以使用Authentication类访问已添加到用户请求范围中的任何内容。
public class CustomLogoutHandler implements LogoutHandler {
@Override
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
// business logic here
}
}
您还应该看看这个问题和答案,它讨论了Spring中自定义处理程序映射的顺序。
我希望这有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句