2012-08-13 21 views
12

Zauważyłem, że istnieje kilka pytań dotyczących tego tematu. Przejrzałem je i nie mogłem zastosować ich do mojej specyficznej konfiguracji Spring. Chciałbym skonfigurować przekierowanie logowania, aby było warunkowe, w zależności od roli użytkownika. To, co mam tak daleko:Spring conditional 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> 

Myślałem this question może być w tej samej linii, co usiłuję zrobić. Czy ktoś wie, jak mogę go zastosować?

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

Obecnie nie mam klasę jak public class Test implements AuthenticationSuccessHandler {} jak pokazano na this example.

+3

Twoje pytanie jest identyczna [ten] (http://stackoverflow.com/questions/7470405/authenticationsuccesshandler-example-for-spring-security-3) –

+0

Dziękuję. Czy możesz sprawdzić ** EDIT 1 **. Jestem zdezorientowany, ponieważ już przypisuję klasę wiosenną do tej fasoli? – ThreaT

+0

Nie loguje się do autoryzacji uwierzytelniającej? http://static.springsource.org/spring-security/site/docs/3.0.x/reference/appendix-namespace.html –

Odpowiedz

20

Mam testowany kod i działa, nie ma fizyka jądrowa w nim

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

zmiany w kontekście zabezpieczeń:

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

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

aktualizacja jeśli chcesz używać default-target-url podejście, będzie działać równie dobrze, ale będzie uruchamiany, gdy użytkownik pierwszy raz wejdzie na stronę logowania:

<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

co jeśli chcę przekierować do innej domeny? @Boris Treukhov – Mahdi

+1

Jeśli ktoś używa kodu Java do kontekstu Sprint: 'formLogin(). SuccessHandler (new MySuccessHandler())' – RAS

Powiązane problemy