2012-08-13 1 views
12

Ich habe festgestellt, dass zu diesem Thema ein paar Fragen gestellt werden. Ich habe sie durchgesehen und konnte sie nicht auf mein spezifisches Spring-Setup anwenden. Ich möchte meine Login-Weiterleitung basierend auf der Rolle des Benutzers als bedingt konfigurieren. Das ist, was ich habe, so weit:Spring Security konditional default-target-url

<http auto-config="true" use-expressions="true"> 
     <custom-filter ref="filterSecurityInterceptor" before="FILTER_SECURITY_INTERCEPTOR"/> 
     <access-denied-handler ref="accessDeniedHandler"/> 
     <form-login 
      login-page="/login" 
      default-target-url="/admin/index" 
      authentication-failure-url="/index?error=true" 
      /> 
     <logout logout-success-url="/index" invalidate-session="true"/> 
</http> 

Ich dachte this question in der gleichen Linie sein könnte als das, was ich zu tun bin versucht. Weiß jemand wie ich es anwenden kann?

EDIT 1

<bean id="authenticationProcessingFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> 
    <property name="authenticationManager" ref="authenticationManager" /> 
    <property name="authenticationSuccessHandler" ref="authenticationSuccessHandler"/> 
</bean> 
<bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"> 
    <property name="defaultTargetUrl" value="/login.jsp"/> 
</bean> 

EDIT 2

Zur Zeit habe ich keine Klasse wie public class Test implements AuthenticationSuccessHandler {} wie in this example gezeigt.

+3

Ihre Frage ist identisch mit [diesem] (http://stackoverflow.com/questions/7470405/authenticationsuccesshandler-example-for-spring-security-3) –

+0

Danke. Könnten Sie bitte ** EDIT 1 ** überprüfen. Ich bin verwirrt, weil ich dieser Bohne bereits eine Federklasse zuordne? – ThreaT

+0

Hat sich ein authenticationSuccessHandler nicht formlogin? http://static.springsource.org/spring-security/site/docs/3.0.x/reference/appendix-namespace.html –

Antwort

20

ich den Code getestet und es funktioniert, da in es

public class MySuccessHandler implements AuthenticationSuccessHandler { 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, 
      HttpServletResponse response, Authentication authentication) 
      throws IOException, ServletException { 
     Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); 
     if (roles.contains("ROLE_ADMIN")){ 
      response.sendRedirect("/Admin.html"); 
      return; 
     } 
     response.sendRedirect("/User.html"); 
    }  
} 

Änderungen im Sicherheitskontext keine Raketenwissenschaft ist:

<bean id="mySuccessHandler" class="my.domain.MySuccessHandler"> 
    </bean> 

<security:form-login ... authentication-success-handler-ref="mySuccessHandler"/> 

Update wenn Sie default-target-url Ansatz verwenden möchten, Es funktioniert genauso gut, wird aber ausgelöst, wenn Ihr Benutzer zum ersten Mal auf die Anmeldeseite zugreift:

<security:form-login default-target-url="/welcome.htm" />

@Controller 
public class WelcomeController { 
    @RequestMapping(value = "/welcome.htm") 
    protected View welcome() { 

     Set<String> roles = AuthorityUtils 
       .authorityListToSet(SecurityContextHolder.getContext() 
         .getAuthentication().getAuthorities()); 
     if (roles.contains("ROLE_ADMIN")) { 
      return new RedirectView("Admin.htm"); 
     } 
     return new RedirectView("User.htm"); 
    } 
} 
+0

Was ist, wenn ich auf Anthere-Domain umleiten möchte? @Boris Treukhov – Mahdi

+1

Falls jemand Java-Code für den Sprint-Kontext verwendet: 'formLogin(). SuccessHandler (new MySuccessHandler())' – RAS