2012-07-14 2 views
0

가능한 중복 :
Setting custom Post-Login Destinations based on user ROLES using spring security하나의 로그인 페이지를 사용하여 다른 페이지를 Spring 3.0 보안으로 리디렉션 할 수 있습니까?

내가 스프링을 사용하여 자바에서 내 프로젝트를하고있는 중이 야. 내 프로젝트에서 봄 보안을 사용하고 있습니다.

내 문제는 ROLE_USER 또는 ROLE_ADMIN 인 역할에 따라 다른 페이지로 리디렉션하려고한다는 것입니다. 즉, 관리자가 로그인 한 경우 한 페이지로 리디렉션해야하며 일반 사용자가 다른 페이지에 로그인했지만 두 사용자 모두 로그인 페이지가 동일해야합니다.

이제 spring-servlet.xml 파일에 아래 코드를 사용하고 있습니다. 그래서 저에게 그것에 대한 해결책을 제안 해주십시오.

<security:http auto-config="true"> 
    <security:intercept-url pattern="/airline/*" access="ROLE_USER" /> 
    <security:form-login login-page="/login" default-target-url="/logout" 
     authentication-failure-url="/login" /> 
    <security:logout logout-success-url="/logout" /> 
</security:http> 

<security:authentication-manager> 
    <security:authentication-provider> 
    <security:jdbc-user-service data-source-ref="dataSrc" 
     users-by-username-query="select username,password,enabled from spring_users where username=?" 
     authorities-by-username-query="select u.username, ur.authority from spring_users u, spring_roles ur where u.user_id=ur.user_id and u.username=?"/> 
    </security:authentication-provider> 
</security:authentication-manager> 

답변

4

성공적인 인증 후에 탐색 흐름을 제어하려면 고유 한 AuthenticationSuccessHandler를 추가하면됩니다.

public class CustomAuthenticationHandler extends SimpleUrlAuthenticationSuccessHandler{ 

@Override 
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
     Authentication authentication) throws ServletException, IOException { 
     String userTargetUrl = "/welcome.xhtml"; 
     String adminTargetUrl = "/admin/welcome.xhtml"; 
     Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); 
     if (roles.contains("ROLE_ADMIN")) { 
      getRedirectStrategy().sendRedirect(request, response, adminTargetUrl); 
     } 
     else if(roles.contains("ROLE_USER")) { 
      getRedirectStrategy().sendRedirect(request, response, userTargetUrl); 
     } 
     else { 
      super.onAuthenticationSuccess(request, response, authentication); 
      return; 
     } 
} 

} 
:

는 CustomAuthenticationHandler 클래스는 다음과 같습니다

<form-login login-page="/login.xhtml" authentication-success-handler-ref="customAuthenticationHandler"/> 
... 
</http> 
<beans:bean id="customAuthenticationHandler" class="com.examples.CustomAuthenticationHandler" /> 

의 customAuthenticationHandler 빈을 참조하여 <form-login> element에 다음 속성을 추가

관련 문제