Spring Security是一個可以爲基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架 。html
其中最主要的安全操做有兩個。java
認證:是爲用戶創建一個他所聲明的主體 ,就是完成用戶的登陸web
受權:指的是一個用戶可否在應用中執行某個操做。在進行受權以前已經完成了用戶的認證。spring
使用idea+maven建立一個java web工程,目錄以下express
並建立好登陸的頁面,登陸失敗的頁面,和登陸成功的頁面,login.html,success.html,failed.html,還有工程的首頁index.jspapache
pom文件的內容以下api
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lyy</groupId> <artifactId>web_03_security_quicklystart</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>web_03_security_quicklystart Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>5.0.2.RELEASE</spring.version> <spring.security.version>5.0.1.RELEASE</spring.security.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>${spring.security.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <port>80</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <server>tomcat7</server> </configuration> </plugin> </plugins> </build> </project>
spring-security.xml的內容以下tomcat
<?xml version="1.0" encoding="UTF-8"?> <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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> <!--spring-security的入門配置--> <!--配置哪些資源不會被攔截 /xxx表示根路徑下的某個資源--> <security:http security="none" pattern="/login.html"/> <security:http security="none" pattern="/failed.html"/> <security:http auto-config="true" use-expressions="false"> <!-- 配置連接地址,表示任意路徑都須要ROLE_USER權限 --> <security:intercept-url pattern="/**" access="ROLE_USER"/> <!--自定義登陸頁面--> <security:form-login login-page="/login.html" login-processing-url="/login" username-parameter="username" password-parameter="password" authentication-failure-forward-url="/failed.html" default-target-url="/success.html" authentication-success-forward-url="/success.html" /> <!--關閉csrf,默認是開啓的--> <security:csrf disabled="true"/> </security:http> <security:authentication-manager> <security:authentication-provider> <!--這裏配置了兩個用戶,分別具備USER和ADMIN的權限--> <security:user-service> <security:user name="user" password="{noop}user" authorities="ROLE_USER"/> <security:user name="admin" password="{noop}admin" authorities="ROLE_ADMIN"/> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans>
這個配置文件中的主要內容以下:安全
(1) 配置security不進行權限控制的資源,如登陸和失敗頁面java-web
<!--配置哪些資源不會被攔截 /xxx表示根路徑下的某個資源--> <security:http security="none" pattern="/login.html"/> <security:http security="none" pattern="/failed.html"/>
(2) 配置任意路徑都須要ROLE_USER權限
(3) 配置使用自定義的登陸頁面
(4) 配置兩個用戶,分別具備USER和ADMIN的權限
注意配置路徑的訪問權限時必須帶上ROLE_前綴
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
注意springSecurityFilterChain這個過濾器的名稱不能更改
啓動工程,輸入localhost進行訪問,會出現以下的登陸頁面
使用user:user和admin:admin這兩個帳戶均可以完成登陸,登陸成功後會跳轉到登陸成功頁面
須要注意的是:
配置文件中配置的是全部資源都要ROLE_USER權限才能訪問,因此若是使用user登陸成功後,能夠訪問到工程中的其餘資源,好比首頁;但使用admin登陸後,由於只有ROLE_ADMIN權限,因此不能訪問工程中的其餘資源