1. 程式人生 > >Spring security 3中登入後跳轉到不同頁面

Spring security 3中登入後跳轉到不同頁面

在spring security 3中,在登入 後,如何根據不同的需要跳轉到不同的頁面呢 ? 
其中要 自定義的過濾器是 AuthenticationSuccessHandler, 

Java程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans:beans  
  3.     xmlns="http://www.springframework.org/schema/security"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:beans="http://www.springframework.org/schema/beans"
      
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/security   
  8.         http://www.springframework.org/schema/security/spring-security-3.1.xsd  
  9.         http://www.springframework.org/schema/beans   
  10.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">  
  11.     <http use-expressions="true"
     >  
  12.         <intercept-url pattern="/login*" access="permitAll" />  
  13.         <intercept-url pattern="/**" access="isAuthenticated()" />  
  14.         <form-login login-page='/login.html'  
  15.             authentication-failure-url="/login.html?error=true"  
  16.             authentication-success-handler-ref="myAuthenticationSuccessHandler"
    />  
  17.         <logout/>  
  18.     </http>  
  19.     <beans:bean id="myAuthenticationSuccessHandler"  
  20.         class="org.company.MySimpleUrlAuthenticationSuccessHandler" />  
  21.     <authentication-manager>  
  22.         <authentication-provider>  
  23.             <user-service>  
  24.                 <user name="user1" password="user1Pass" authorities="ROLE_USER" />  
  25.                 <user name="admin1" password="admin1Pass" authorities="ROLE_ADMIN" />  
  26.             </user-service>  
  27.         </authentication-provider>  
  28.     </authentication-manager>  
  29. </beans:beans>  


  然後自定義的類要實現 AuthenticationSuccessHandler介面 ,程式碼如下 : 
Java程式碼  收藏程式碼
  1. public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {  
  2.     protected Log logger = LogFactory.getLog(this.getClass());  
  3.     private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();  
  4.     @Override  
  5.     public void onAuthenticationSuccess(HttpServletRequest request,   
  6.       HttpServletResponse response, Authentication authentication) throws IOException {  
  7.         handle(request, response, authentication);  
  8.         clearAuthenticationAttributes(request);  
  9.     }  
  10.     protected void handle(HttpServletRequest request,   
  11.       HttpServletResponse response, Authentication authentication) throws IOException {  
  12.         String targetUrl = determineTargetUrl(authentication);  
  13.         if (response.isCommitted()) {  
  14.             logger.debug("Response has already been committed. Unable to redirect to " + targetUrl);  
  15.             return;  
  16.         }  
  17.         redirectStrategy.sendRedirect(request, response, targetUrl);  
  18.     }  
  19.     protected String determineTargetUrl(Authentication authentication) {  
  20.         boolean isUser = false;  
  21.         boolean isAdmin = false;  
  22.         Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();  
  23.         for (GrantedAuthority grantedAuthority : authorities) {  
  24.             if (grantedAuthority.getAuthority().equals("ROLE_USER")) {  
  25.                 isUser = true;  
  26.                 break;  
  27.             } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {  
  28.                 isAdmin = true;  
  29.                 break;  
  30.             }  
  31.         }  
  32.         if (isUser) {  
  33.             return "/homepage.html";  
  34.         } else if (isAdmin) {  
  35.             return "/console.html";  
  36.         } else {  
  37.             throw new IllegalStateException();  
  38.         }  
  39.     }  
  40.     protected void clearAuthenticationAttributes(HttpServletRequest request) {  
  41.         HttpSession session = request.getSession(false);  
  42.         if (session == null) {  
  43.             return;  
  44.         }  
  45.         session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);  
  46.     }  
  47.     public void setRedirectStrategy(RedirectStrategy redirectStrategy) {  
  48.         this.redirectStrategy = redirectStrategy;  
  49.     }  
  50.     protected RedirectStrategy getRedirectStrategy() {  
  51.         return redirectStrategy;  
  52.     }  
  53. }  

  其中要關注的是determineTargetUrl方法,傳入 的引數是 Authentication型別的,然後進行許可權的 判斷