---
title: "Spring Security 3.0 多页面登录配置  "
author: "113.com"
date: "2014-07-11T16:44:37Z"
tags: []
categories: ["编程语言"]
slug: "spring-security-3-0"
draft: false
lastmod: "2014-07-11T16:44:37Z"
id: 2090863
url: "https://113-com.iteye.com/blog/2090863"
---

### Spring Security 3.0 多页面登录配置

 http://zhousl.koo.blog.163.com/blog/static/7136380420113208174680/

 

网上很多文章是关于Spring Security 2实现多页面登录的。因为现在需要使用Spring Security 3.x来实现所以只能自己动手解决。参考了网上这两篇文章[《spring security 2中使用通过自定义过滤器实现多登录页面》](http://kalashnicov.javaeye.com/blog/609440)和[《spring security 全配置》](http://hi.baidu.com/hontlong/blog/item/055edd248e300b114d088d4c.html)。总的来说Security 2 和 3还是有不小差别的。

      现在开始进入正题。同样使用《spring security 2中使用通过自定义过滤器实现多登陆页面》中的那几个问题。

      问题：

            对于多登录界面，要求实现不同的用户，比如前台用户和后台用户，分别在以下情况中实现到不同页面的转向：

           1、在未登录时，访问受限页面

           2、在登录以后，转向到不同的默认页面，比如前台用户转向到个人中心主页，后台用户转向到后台管理页面的首页。

           3、在登录失败时，导向到错误页面。

           4、在注销登录时，不同的用户转向到不同的注销成功界面。

     **第一个问题**，因为Spring Security会通过AuthenticationEntryPoint来实现未登录用户访问被保护资源时自动跳转到登录页面，所以我们这里需要的就是扩展AuthenticaitonEntryPoint。

     首先修改配置文件，如下：

     &lt;http entry-point-ref="loginPageEntryPoint"&gt;  
        &lt;intercept-url pattern="/login.\*\*" access="IS\_AUTHENTICATED\_ANONYMOUSLY"/&gt;  
        &lt;intercept-url pattern="/admin/login.\*\*" access="IS\_AUTHENTICATED\_ANONYMOUSLY"/&gt;  
        &lt;intercept-url pattern="/admin/\*\*" access="ROLE\_ADMIN"/&gt;  
        &lt;intercept-url pattern="/\*\*" access="ROLE\_USER"/&gt;

         ……

     &lt;beans:bean id="loginUrlEntryPoint" class="cn.csdn.blog.loafap.security.LoginUrlEntryPoint"&gt;&lt;/beans:bean&gt;

   

     注意&lt;http&gt; 的配置，因为我们要通过自定义过滤器来实现，所以这里去掉auto-config="true"这个属性。

     LoginUrlEntryPoint.java内容如下：

     public class LoginUrlEntryPoint implements AuthenticationEntryPoint {

         public void commence(HttpServletRequest request, HttpServletResponse response,  
                   AuthenticationException authException) throws IOException, ServletException {  
             String targetUrl = null;  
             String url = request.getRequestURI();  
          
             if(url.indexOf("admin") != -1){  
                 //未登录而访问后台受控资源时，跳转到后台登录页面  
                 targetUrl = "/admin/login.jsp";  
             }else{  
                 //未登录而访问前台受控资源时，跳转到前台登录页面  
                 targetUrl = "/login.jsp";  
             }  
          
             targetUrl = request.getContextPath() + targetUrl;  
             response.sendRedirect(targetUrl);  
         }

     }

     以上就实现了问题一的解决。

     **第二、三问题**， 在进行表单认证的时候，UsernamePasswordAuthenticationFilter 是验证执行机制的过滤器，这与Spring Security2.x 不同，spring security2.x 用的是AuthenticationProcessingFilter 过滤器。在Spring Security3.x中已不推荐使用AuthenticationProcessingFilter 过滤器啦。这里我们对比Spring Security2.x来看看。

     Spring Security 2.x的配置：

     &lt;!-- 验证前台用户 --&gt;    
     &lt;bean id="**loginFilter**"    
           class="**org.springframework.security.ui.webapp.AuthenticationProcessingFilter**"&gt;    
           &lt;sec:custom-filter before="AUTHENTICATION\_PROCESSING\_FILTER" /&gt;    
           &lt;property name="authenticationManager" ref="authenticationManager" /&gt;    
           &lt;property name="authenticationFailureUrl" value="/login.jsp?error=true" /&gt;    
           &lt;property name="defaultTargetUrl" value="/login.jsp?error=true" /&gt;    
           &lt;property name="alwaysUseDefaultTargetUrl" value="true" /&gt;    
           &lt;property name="filterProcessesUrl" value="/login" /&gt;    
     &lt;/bean&gt;    
     &lt;!-- 验证后台用户 --&gt;    
     &lt;bean id="**adminLoginFilter**"    
            class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"&gt;    
           &lt;sec:custom-filter position="AUTHENTICATION\_PROCESSING\_FILTER" /&gt;    
           &lt;property name="authenticationManager" ref="authenticationManager" /&gt;     
           &lt;!-- 认证错误页面，已经被覆盖，由urlStrategy决定--&gt;    
           &lt;property name="authenticationFailureUrl" value="/admin/login.jsp?error=true" /&gt;    
           &lt;!-- 认证成功页面--&gt;    
           &lt;property name="defaultTargetUrl" value="/admin/main.jsp" /&gt;    
           &lt;property name="alwaysUseDefaultTargetUrl" value="true" /&gt; 

           &lt;property name="filterProcessesUrl" value="/admin/login" /&gt;   
      &lt;/bean&gt;

      Spring Security 3.x的配置：

      &lt;!-- 验证前台用户 --&gt;    
      &lt;beans:bean id="**loginFilter**"    

            class="**org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter**"&gt;  
           &lt;beans:property name="authenticationManager" ref="authenticationManager"/&gt;  
           &lt;beans:property name="authenticationFailureHandler" ref="failureHandler"/&gt;  
           &lt;beans:property name="authenticationSuccessHandler" ref="successHandler"/&gt;  
           &lt;beans:property name="filterProcessesUrl" value="/login"/&gt;  
      &lt;/beans:bean&gt;

      &lt;beans:bean id="failureHandler"

             class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"&gt;  
            &lt;beans:property name="defaultFailureUrl" value="/login.jsp?error=true" /&gt;  
       &lt;/beans:bean&gt;

       &lt;beans:bean id="successHandler"

              class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"&gt;  
             &lt;beans:property name="alwaysUseDefaultTargetUrl" value="true"/&gt;  
             &lt;beans:property name="defaultTargetUrl" value="/main.jsp"/&gt;  
       &lt;/beans:bean&gt;  
       &lt;!-- 验证后台用户 --&gt;    
       &lt;beans:bean id="**adminLoginFilter**"

               class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"&gt;  
               &lt;beans:property name="authenticationManager" ref="authenticationManager"/&gt;  
               &lt;beans:property name="authenticationFailureHandler" ref="adminFailureHandler"/&gt;  
               &lt;beans:property name="authenticationSuccessHandler" ref="adminSuccessHandler"/&gt;  
               &lt;beans:property name="filterProcessesUrl" value="/admin/login"/&gt;  
        &lt;/beans:bean&gt;

        &lt;beans:bean id="adminFailureHandler" 

                class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler"&gt;  
               &lt;beans:property name="defaultFailureUrl" value="/admin/login.jsp?error=true" /&gt;  
        &lt;/beans:bean&gt;

        &lt;beans:bean id="adminSuccessHandler" 

                class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"&gt;  
                &lt;beans:property name="alwaysUseDefaultTargetUrl" value="true"/&gt;  
                &lt;beans:property name="defaultTargetUrl" value="/admin/main.jsp"/&gt;  
        &lt;/beans:bean&gt;

      对比一下是不是就能看出很明显的差别啦，感觉Spring Security3.x的配置要比2.x写的多了些，还是2.x配置项写的少些。

      第四个问题，我们也是通过定义两个LogoutFilter来实现。这个和Spring Security2.x配置差不多，只是过滤器实际中在&lt;http&gt;中注册的，而不是直接在&lt;bean&gt;里注册。

    &lt;!-- 注销过滤器，完成前台用户注销时的定向功能 --&gt;  
    &lt;beans:bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter"&gt;  
        &lt;beans:constructor-arg value="/login.jsp" /&gt;  
        &lt;beans:constructor-arg&gt;  
            &lt;beans:list&gt;  
                &lt;beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" /&gt;  
            &lt;/beans:list&gt;  
        &lt;/beans:constructor-arg&gt;  
        &lt;beans:property name="filterProcessesUrl" value="/logout" /&gt;  
    &lt;/beans:bean&gt;  
    &lt;!-- 注销过滤器，完成后台用户注销时的定向功能 --&gt;  
    &lt;beans:bean id="adminLogoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter"&gt;  
        &lt;beans:constructor-arg value="/admin/login.jsp" /&gt;  
        &lt;beans:constructor-arg&gt;  
            &lt;beans:list&gt;  
                &lt;beans:bean class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler" /&gt;  
            &lt;/beans:list&gt;  
        &lt;/beans:constructor-arg&gt;  
        &lt;beans:property name="filterProcessesUrl" value="/admin/logout" /&gt;  
    &lt;/beans:bean&gt;

   

    上面改用到的过滤器我们都配置完成啦，现在我们在&lt;http&gt;中配置我们所需要的这4个过滤器，这点和2.x的配置是不同的。

   

    &lt;http entry-point-ref="loginUrlEntryPoint"&gt;  
        &lt;intercept-url pattern="/login.\*\*" access="IS\_AUTHENTICATED\_ANONYMOUSLY"/&gt;  
        &lt;intercept-url pattern="/admin/login.\*\*" access="IS\_AUTHENTICATED\_ANONYMOUSLY"/&gt;  
        &lt;intercept-url pattern="/admin/\*\*" access="ROLE\_ADMIN"/&gt;  
        &lt;intercept-url pattern="/\*\*" access="ROLE\_USER"/&gt;  
       

        &lt;!-- 登录过滤器 --&gt;  
        &lt;custom-filter before="FORM\_LOGIN\_FILTER" ref="loginFilter"/&gt;  
        &lt;custom-filter position="FORM\_LOGIN\_FILTER" ref="adminLoginFilter"/&gt;

        &lt;!-- 注销过滤器 --&gt;  
        &lt;custom-filter before="LOGOUT\_FILTER" ref="logoutFilter"/&gt;  
        &lt;custom-filter position="LOGOUT\_FILTER" ref="adminLogoutFilter"/&gt;  
    &lt;/http&gt; 

    注意Spring Security2.x中的AUTHENTICATION\_PROCESSING\_FILTER在Spring Security3.x中已被FORM\_LOGIN\_FILTER替换。