복붙노트

[SPRING] Spring Security 3 용 AuthenticationSuccessHandler 예제

SPRING

Spring Security 3 용 AuthenticationSuccessHandler 예제

저는 Spring Security 3의 초보자입니다. 사용자 로그인을 위해 역할을 사용하고 있습니다.

해당 사용자의 역할에 따라 다른 페이지로 사용자를 리디렉션하고 싶습니다. 동일한 AuthenticationSuccessHandler를 구현해야한다는 것을 이해합니다. 그러나 그 방향의 몇 가지 예가 도움이 될 것입니다.

미리 감사드립니다. 비벡

해결법

  1. ==============================

    1.다음과 같이 할 수 있습니다.

    다음과 같이 할 수 있습니다.

    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");
            }
        }
    }
    

    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"/>
    
  2. from https://stackoverflow.com/questions/7470405/authenticationsuccesshandler-example-for-spring-security-3 by cc-by-sa and MIT license