首先咱們爲Spring Security專門創建一個Spring的配置文件,該文件就專門用來做爲Spring Security的配置。使用Spring Security咱們須要引入Spring Security的NameSpace。web
<beans xmlns="http://www.springframework.org/schema/beans"正則表達式
xmlns:security="http://www.springframework.org/schema/security"spring
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"數據庫
xsi:schemaLocation="http://www.springframework.org/schema/beans瀏覽器
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd安全
http://www.springframework.org/schema/securityapp
http://www.springframework.org/schema/security/spring-security-3.1.xsd">框架
</beans>ide
Spring Security命名空間的引入能夠簡化咱們的開發,它涵蓋了大部分Spring Security經常使用的功能。它的設計是基於框架內大範圍的依賴的,能夠被劃分爲如下幾塊。url
引入了Spring Security的NameSpace以後咱們就可使用該命名空間下的元素來配置Spring Security了。首先咱們來定義一個http元素,security只是咱們使用命名空間的一個前綴。http元素是用於定義Web相關權限控制的。
<security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
如上定義中,intercept-url定義了一個權限控制的規則。pattern屬性表示咱們將對哪些url進行權限控制,其也能夠是一個正則表達式,如上的寫法表示咱們將對全部的URL進行權限控制;access屬性表示在請求對應的URL時須要什麼權限,默認配置時它應該是一個以逗號分隔的角色列表,請求的用戶只需擁有其中的一個角色就能成功訪問對應的URL。這裏的「ROLE_USER」表示請求的用戶應當具備ROLE_USER角色。「ROLE_」前綴是一個提示Spring使用基於角色的檢查的標記。
有了權限控制的規則了後,接下來咱們須要定義一個AuthenticationManager用於認證。咱們先來看以下定義:
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="user" authorities="ROLE_USER"/>
<security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
authentication-manager元素指定了一個AuthenticationManager,其須要一個AuthenticationProvider(對應authentication-provider元素)來進行真正的認證,默認狀況下authentication-provider對應一個DaoAuthenticationProvider,其須要UserDetailsService(對應user-service元素)來獲取用戶信息UserDetails(對應user元素)。這裏咱們只是簡單的使用user元素來定義用戶,而實際應用中這些信息一般都是須要從數據庫等地方獲取的,這個將放到後續再講。咱們能夠看到經過user元素咱們能夠指定user對應的用戶名、密碼和擁有的權限。user-service還支持經過properties文件來指定用戶信息,如:
<security:user-service properties="/WEB-INF/config/users.properties"/>
其中屬性文件應遵循以下格式:
username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]
因此,對應上面的配置文件,咱們的users.properties文件的內容應該以下所示:
#username=password,grantedAuthority[,grantedAuthority][,enabled|disabled]
user=user,ROLE_USER
admin=admin,ROLE_USER,ROLE_ADMIN
至此,咱們的Spring Security配置文件的配置就完成了。完整配置文件將以下所示。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<security:http auto-config="true">
<security:intercept-url pattern="/**" access="ROLE_USER"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="user" authorities="ROLE_USER"/>
<security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>
以後咱們告訴Spring加載這個配置文件。一般,咱們能夠在web.xml文件中經過context-param把它指定爲Spring的初始配置文件,也能夠在對應Spring的初始配置文件中引入它。這裏咱們採用前者。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml,/WEB-INF/config/spring-security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Spring的配置文件是經過對應的ContextLoaderListener來加載和初始化的,上述代碼中的applicationContext.xml文件就是對應的Spring的配置文件,若是沒有能夠不用配置。接下來咱們還須要在web.xml中定義一個filter用來攔截須要交給Spring Security處理的請求,須要注意的是該filter必定要定義在其它如SpringMVC等攔截請求以前。這裏咱們將攔截全部的請求,具體作法以下所示:
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
接下來能夠啓動咱們的應用,而後在瀏覽器中訪問咱們的主頁。你會看到以下頁面。
由於咱們的spring-security.xml文件中配置好了全部的請求都須要「ROLE_USER」權限,因此當咱們在請求主頁的時候,Spring Security發現咱們尚未登陸,Spring會引導咱們到登陸界面。使用正確的用戶名和密碼(如上面配置的user/user或admin/admin)登陸後,若是符合對應的權限咱們就能夠訪問主頁了,不然將出現403(禁止訪問)界面。
可能你會奇怪,咱們沒有創建上面的登陸頁面,爲何Spring Security會跳到上面的登陸頁面呢?這是咱們設置http的auto-config=」true」時Spring Security自動爲咱們生成的。
當指定http元素的auto-config=」true」時,就至關於以下內容的簡寫。
<security:http>
<security:form-login/>
<security:http-basic/>
<security:logout/>
</security:http>
這些元素負責創建表單登陸、基本的認證和登出處理。它們均可以經過指定對應的屬性來改變它們的行爲。