初識Spring Security
本文參考或者轉(zhuǎn)自: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相關(guān)權(quán)限控制。--> <!--intercept-url定義了一個權(quán)限控制的規(guī)則。 pattern屬性表示我們將對哪些url進(jìn)行權(quán)限控制,其也可以是一個正則表達(dá)式,如上的寫法表示我們將對所有的URL進(jìn)行權(quán)限控制; access屬性表示在請求對應(yīng)的URL時需要什么權(quán)限,默認(rèn)配置時它應(yīng)該是一個以逗號分隔的角色列表,請求的用戶只需擁有其中的一個角色就能成功訪問對應(yīng)的URL。 這里的“ROLE_USER”表示請求的用戶應(yīng)當(dāng)具有ROLE_USER角色。“ROLE_”前綴是一個提示Spring使用基于角色的檢查的標(biāo)記。-->
<!--注:auto-config="true"時,SpringSecurity發(fā)現(xiàn)沒有登錄回自動創(chuàng)建登陸頁面--> <security:http auto-config="true"> <security:intercept-url pattern="/**" access="ROLE_USER"/> </security:http> <!--使用AuthenticationManager 進(jìn)行認(rèn)證相關(guān)配置--> <!--authentication-manager元素指定了一個AuthenticationManager,其需要一個AuthenticationProvider(對應(yīng)authentication-provider元素)來進(jìn)行真正的認(rèn)證--> <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進(jìn)行處理
<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 配置的用戶名、密碼登陸
浙公網(wǎng)安備 33010602011771號