目錄spring
1.1 form-login元素介紹安全
1.1.1 使用自定義登陸頁面jsp
1.1.2 指定登陸後的頁面post
1.1.3 指定登陸失敗後的頁面url
1.2 http-basicspa
http元素下的form-login元素是用來定義表單登陸信息的。當咱們什麼屬性都不指定的時候Spring Security會爲咱們生成一個默認的登陸頁面。若是不想使用默認的登陸頁面,咱們能夠指定本身的登陸頁面。orm
自定義登陸頁面是經過login-page屬性來指定的。提到login-page咱們不得不提另外幾個屬性。blog
因此,咱們能夠經過以下定義使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>
經過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"/>
除了能夠指定登陸認證成功後的頁面和對應的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屬性將再也不發生做用。
以前介紹的都是基於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將引導咱們到登陸頁面,而不是彈出一個窗口。