首页 > 解决方案 > 未在 SimpleUrlLogoutSuccessHandle 上调用 onLogoutSuccess

问题描述

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private DataSource dataSource; // get by Spring

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            // Here, you are making the public directory on the classpath root available without authentication (e..g. for css files)
            .antMatchers("/public/**", "/registration.html").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login.html")
            .successHandler((request, response, authentication) -> new DefaultRedirectStrategy().sendRedirect(request, response, "/welcome"))
            .failureUrl("/login-error.html")
            .permitAll()
            .and()
            .logout()
            .logoutSuccessHandler(new CustomLogoutSuccessHandler())
            .permitAll();
    }
}

这是我的自定义处理程序:CustomLogoutSuccessHandler.java

public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler implements LogoutSuccessHandler {
    private static Logger logger = LogManager.getLogger(CustomLogoutSuccessHandler.class);

    @Override
    public void onLogoutSuccess(
            HttpServletRequest request,
            HttpServletResponse response,
            Authentication authentication)
            throws IOException, ServletException {
        logger.info("onLogoutSuccess:");
        response.sendRedirect(request.getContextPath() + "/login.html");
    }
}

在我的模板 ( users.html) 中,我添加了一个这样的链接:

<p align="center"><a th:href="@{/logout.html}">Logout</a></p>

但是,当单击注销链接时,onLogoutSuccess不会在我的自定义处理程序类上调用该方法。

为什么?

标签: javaspring-bootspring-security

解决方案


推荐阅读