开发者

AuthenticationSuccessHandler example for Spring Security 3

开发者 https://www.devze.com 2023-04-06 08:40 出处:网络
I am a newbie to Spring Security 3 . I am using roles for users to login. 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

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

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 AuthenticationSuccessHandler for the same, but some examples in that d开发者_StackOverflowirection would help.

Thanks in advance, Vivek


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:

<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"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号