本文參考或者轉自:http://haohaoxuexi.iteye.com/blog/2154299
1、新建Spring Security配置檔案spring-security.xml:<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <!--http元素用於定義Web相關許可權控制。--> <!--intercept-url定義了一個許可權控制的規則。 pattern屬性表示我們將對哪些url進行許可權控制,其也可以是一個正規表示式,如上的寫法表示我們將對所有的URL進行許可權控制; access屬性表示在請求對應的URL時需要什麼許可權,預設配置時它應該是一個以逗號分隔的角色列表,請求的使用者只需擁有其中的一個角色就能成功訪問對應的URL。 這裡的“ROLE_USER”表示請求的使用者應當具有ROLE_USER角色。“ROLE_”字首是一個提示Spring使用基於角色的檢查的標記。-->
<!--注:auto-config="true"時,SpringSecurity發現沒有登入回自動建立登陸頁面--> <security:http auto-config="true"> <security:intercept-url pattern="/**" access="ROLE_USER"/> </security:http> <!--使用AuthenticationManager 進行認證相關配置--> <!--authentication-manager元素指定了一個AuthenticationManager,其需要一個AuthenticationProvider(對應authentication-provider元素)來進行真正的認證--> <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>
2、在web.xml檔案中通過context-param把它指定為Spring的初始配置檔案,告訴Spring載入這個配置檔案。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/spring-security.xml</param-value> </context-param>
3、配置filter,將請求交給Spring Security進行處理
<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>
最終的web.xml檔案如下(SpringMvc專案,因此有Spring MVC配置)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/spring-security.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
4、啟動專案如下圖:
可使用security:user 配置的使用者名稱、密碼登陸