您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Spring Security中调用target-url之前在登录/注销时重定向到方法?

如何在Spring Security中调用target-url之前在登录/注销时重定向到方法?

自己写 和 。

例:

<security:form-login login-page="/login"
    login-processing-url="/login_check"
    authentication-failure-url="/login?error=true"
    authentication-success-handler-ref="myAuthenticationSuccessHandler"
/>

<security:logout
    logout-url="/logout"
    success-handler-ref="mylogoutSuccessHandler"
/>

@Component
public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    @Autowired
    private UserService userService;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

        // changeLastLoginTime(username)
        userService.changeLastLoginTime(authentication.getName());

        setDefaultTargetUrl("/home");
        super.onAuthenticationSuccess(request, response, authentication);
    }
}

@Component
public class MylogoutSuccessHandler extends SimpleUrllogoutSuccessHandler {

    @Override
    public void onlogoutSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        if (authentication != null) {
            // do something 
        }

        setDefaultTargetUrl("/login");
        super.onlogoutSuccess(request, response, authentication);       
    }
}
Java 2022/1/1 18:17:45 有461人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶