Spring Security(02)——关于登陆

目录spring

1.1     form-login元素介绍安全

1.1.1    使用自定义登陆页面jsp

1.1.2    指定登陆后的页面post

1.1.3    指定登陆失败后的页面url

1.2     http-basicspa

 

1.1     form-login元素介绍

       http元素下的form-login元素是用来定义表单登陆信息的。当咱们什么属性都不指定的时候Spring Security会为咱们生成一个默认的登陆页面。若是不想使用默认的登陆页面,咱们能够指定本身的登陆页面。orm

 

1.1.1   使用自定义登陆页面

       自定义登陆页面是经过login-page属性来指定的。提到login-page咱们不得不提另外几个属性。blog

  • username-parameter:表示登陆时用户名使用的是哪一个参数,默认是“j_username”。
  • password-parameter:表示登陆时密码使用的是哪一个参数,默认是“j_password”。
  • login-processing-url:表示登陆时提交的地址,默认是“/j-spring-security-check”。这个只是Spring Security用来标记登陆页面使用的提交地址,真正关于登陆这个请求是不须要用户本身处理的。

 

       因此,咱们能够经过以下定义使Spring Security在须要用户登陆时跳转到咱们自定义的登陆页面。资源

   <security:http auto-config="true">get

      <security:form-login login-page="/login.jsp"

         login-processing-url="/login.do" username-parameter="username"

         password-parameter="password" />

      <!-- 表示匿名用户能够访问 -->

      <security:intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>

      <security:intercept-url pattern="/**" access="ROLE_USER" />

   </security:http>

 

       须要注意的是,咱们以前配置的是全部的请求都须要ROLE_USER权限,这意味着咱们自定义的“/login.jsp”也须要该权限,这样就会造成一个死循环了。解决办法是咱们须要给“/login.jsp”放行。经过指定“/login.jsp”的访问权限为“IS_AUTHENTICATED_ANONYMOUSLY”或“ROLE_ANONYMOUS”能够达到这一效果。此外,咱们也能够经过指定一个http元素的安全性为none来达到相同的效果。如:

   <security:http security="none" pattern="/login.jsp" />

   <security:http auto-config="true">

      <security:form-login login-page="/login.jsp"

         login-processing-url="/login.do" username-parameter="username"

         password-parameter="password" />

      <security:intercept-url pattern="/**" access="ROLE_USER" />

   </security:http>

       它们二者的区别是前者将进入Spring Security定义的一系列用于安全控制的filter,然后者不会。当指定一个http元素的security属性为none时,表示其对应pattern的filter链为空。从3.1开始,Spring Security容许咱们定义多个http元素以知足针对不一样的pattern请求使用不一样的filter链。当为指定pattern属性时表示对应的http元素定义将对全部的请求发生做用。

 

       根据上面的配置,咱们自定义的登陆页面的内容应该是这样子的:

   <form action="login.do" method="post">

      <table>

         <tr>

            <td>用户名:</td>

            <td><input type="text" name="username"/></td>

         </tr>

         <tr>

            <td>密码:</td>

            <td><input type="password" name="password"/></td>

         </tr>

         <tr>

            <td colspan="2" align="center">

                <input type="submit" value="登陆"/>

                <input type="reset" value="重置"/>

            </td>

         </tr>

      </table>

   </form>

 

1.1.2   指定登陆后的页面

经过default-target-url指定

       默认状况下,咱们在登陆成功后会返回到本来受限制的页面。但若是用户是直接请求登陆页面,登陆成功后应该跳转到哪里呢?默认状况下它会跳转到当前应用的根路径,即欢迎页面。经过指定form-login元素的default-target-url属性,咱们可让用户在直接登陆后跳转到指定的页面。若是想让用户不论是直接请求登陆页面,仍是经过Spring Security引导过来的,登陆以后都跳转到指定的页面,咱们能够经过指定form-login元素的always-use-default-target属性为true来达到这一效果。

 

经过authentication-success-handler-ref指定

       authentication-success-handler-ref对应一个AuthencticationSuccessHandler实现类的引用。若是指定了authentication-success-handler-ref,登陆认证成功后会调用指定AuthenticationSuccessHandler的onAuthenticationSuccess方法。咱们须要在该方法体内对认证成功作一个处理,而后返回对应的认证成功页面。使用了authentication-success-handler-ref以后认证成功后的处理就由指定的AuthenticationSuccessHandler来处理,以前的那些default-target-url之类的就都不起做用了。

       如下是自定义的一个AuthenticationSuccessHandler的实现类。

publicclass AuthenticationSuccessHandlerImpl implements

      AuthenticationSuccessHandler {

 

   publicvoid onAuthenticationSuccess(HttpServletRequest request,

         HttpServletResponse response, Authentication authentication)

         throws IOException, ServletException {

      response.sendRedirect(request.getContextPath());

   }

 

}

 

       其对应使用authentication-success-handler-ref属性的配置是这样的:

   <security:http auto-config="true">

      <security:form-login login-page="/login.jsp"

         login-processing-url="/login.do" username-parameter="username"

         password-parameter="password"

         authentication-success-handler-ref="authSuccess"/>

      <!-- 表示匿名用户能够访问 -->

      <security:intercept-url pattern="/login.jsp"

         access="IS_AUTHENTICATED_ANONYMOUSLY" />

      <security:intercept-url pattern="/**" access="ROLE_USER" />

   </security:http>

   <!-- 认证成功后的处理类 -->

   <bean id="authSuccess" class="com.xxx.AuthenticationSuccessHandlerImpl"/>

 

1.1.3   指定登陆失败后的页面

       除了能够指定登陆认证成功后的页面和对应的AuthenticationSuccessHandler以外,form-login一样容许咱们指定认证失败后的页面和对应认证失败后的处理器AuthenticationFailureHandler。

经过authentication-failure-url指定

       默认状况下登陆失败后会返回登陆页面,咱们也能够经过form-login元素的authentication-failure-url来指定登陆失败后的页面。须要注意的是登陆失败后的页面跟登陆页面同样也是须要配置成在未登陆的状况下能够访问,不然登陆失败后请求失败页面时又会被Spring Security重定向到登陆页面。

   <security:http auto-config="true">

      <security:form-login login-page="/login.jsp"

         login-processing-url="/login.do" username-parameter="username"

         password-parameter="password"

         authentication-failure-url="/login_failure.jsp"

         />

      <!-- 表示匿名用户能够访问 -->

      <security:intercept-url pattern="/login*.jsp*"

         access="IS_AUTHENTICATED_ANONYMOUSLY" />

      <security:intercept-url pattern="/**" access="ROLE_USER" />

   </security:http>

 

经过authentication-failure-handler-ref指定

       相似于authentication-success-handler-ref,authentication-failure-handler-ref对应一个用于处理认证失败的AuthenticationFailureHandler实现类。指定了该属性,Spring Security在认证失败后会调用指定AuthenticationFailureHandler的onAuthenticationFailure方法对认证失败进行处理,此时authentication-failure-url属性将再也不发生做用。

 

1.2     http-basic

       以前介绍的都是基于form-login的表单登陆,其实Spring Security还支持弹窗进行认证。经过定义http元素下的http-basic元素能够达到这一效果。

   <security:http auto-config="true">

      <security:http-basic/>

      <security:intercept-url pattern="/**" access="ROLE_USER" />

   </security:http>

      

       此时,若是咱们访问受Spring Security保护的资源时,系统将会弹出一个窗口来要求咱们进行登陆认证。效果以下:


      

 


 

       固然此时咱们的表单登陆也仍是可使用的,只不过当咱们访问受包含资源的时候Spring Security不会自动跳转到登陆页面。这就须要咱们本身去请求登陆页面进行登陆。

       须要注意的是当咱们同时定义了http-basic和form-login元素时,form-login将具备更高的优先级。即在须要认证的时候Spring Security将引导咱们到登陆页面,而不是弹出一个窗口。

相关文章
相关标签/搜索