一 簡介:Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency ...
一 簡介:Spring Security是一個能夠為基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴註入)和AOP(面向切麵編程)功能,為應用系統提供聲明式的安全訪問控制功能,減少了為企業系統安全控制編寫大量重覆代碼的工作.
Spring Security入門小Demo
1.創建工程spring-security-demo 引入pom依賴
<--spring相關依賴 略-->
<--security相關依賴-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-security.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <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-app> |
3.創建index.xml
4.配置spring-security.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" 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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
<!-- 頁面攔截規則 --> <http use-expressions="false"> <intercept-url pattern="/**" access="ROLE_USER" /> <form-login/> </http>
<!-- 認證管理器 --> <authentication-manager> <authentication-provider> <user-service> <user name="admin" password="123456" authorities="ROLE_USER"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans> |
use-expressions 為是否使用使用 Spring 表達式語言( SpEL ),預設為true ,如果開啟,則
攔截的配置應該寫成以下形式
<intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> |
這樣就可以實現攔截了 不過預設有登陸界面 也可以自己定義登陸界面
1.首先自定義一個登陸界面(一個成功界面,一個失敗界面)
2.修改spring-security.xml
<!-- 以下頁面不被攔截 --> <http pattern="/login.html" security="none"></http> <http pattern="/login_error.html" security="none"></http> <!-- 頁面攔截規則 --> <http use-expressions="false"> <intercept-url pattern="/*" access="ROLE_USER" /> <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-url="/login_error.html"/> <csrf disabled="true"/> </http> |
security="none" 設置此資源不被攔截.
login-page:指定登錄頁面。
authentication-failure-url:指定了身份驗證失敗時跳轉到的頁面。
default-target-url:指定了成功進行身份驗證和授權後預設呈現給用戶的頁面。
csrf disabled="true" 關閉csrf ,如果不加會出現錯誤
ok.