java Spring Security 3 的 AuthenticationSuccessHandler 示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7470405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:07:41  来源:igfitidea点击:

AuthenticationSuccessHandler example for Spring Security 3

javaspring

提问by Vivek

I am a newbie to Spring Security 3 . I am using roles for users to login.

我是 Spring Security 3 的新手。我正在使用角色供用户登录。

I want to redirect a user to a different page based on the role of that user, I understand is that I would have to implement the AuthenticationSuccessHandlerfor the same, but some examples in that direction would help.

我想根据该用户的角色将用户重定向到不同的页面,我知道我必须实现AuthenticationSuccessHandler相同的页面,但是这个方向的一些示例会有所帮助。

Thanks in advance, Vivek

提前致谢,维维克

回答by nfechner

You can do something like this:

你可以这样做:

public class Test implements AuthenticationSuccessHandler {
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains("ROLE_USER") {
            response.sendRedirect("/userpage");
        }
    }
}

In the XML config add this:

在 XML 配置中添加:

<bean id="authenticationFilter" class="YOUR_AUTH_FILTER_HERE">
    <!-- There might be more properties here, depending on your auth filter!! -->
    <property name="authenticationSuccessHandler" ref="successHandler" />
</bean> 

<bean id="successHandler" class="Test"/>