Spring Security 是一個能夠為基於 Spring 的企業應用系統提供聲明式的安全
訪問控制解決方案的安全框架。它提供了一組可以在 Spring 應用上下文中配置
的 Bean,充分利用了 Spring IoC,DI(控制反轉 Inversion of Control ,DI:Dependency
Injection 依賴注入)和 AOP(面向切面編程)功能,為應用系統提供聲明式的安
全訪問控制功能,減少了為企業系統安全控制編寫大量重復代碼的工作。
1. 創建工程
2. 添加依賴
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>5.0.0.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <path>/</path> <port>8080</port> </configuration> </plugin> </plugins> </build>
3. 編寫配置文件
<?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 pattern="/login.html" security="none"/> <http pattern="/login_error.html" security="none"/> <!--角色訪問權限--> <http use-expressions="false"> <!--角色可以訪問的資源--> <intercept-url pattern="/**" access="ROLE_USER"/> <!--設置登錄表單--> <form-login login-page="/login.html" default-target-url="/index.html" authentication-failure-forward-url="/login_error.html"/> <!--退出--> <logout/> <!--禁止跨站請求偽造校驗--> <csrf disabled="true"/> </http> <!--加密方式 明文--> <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder"/> <!--認證管理器--> <authentication-manager> <authentication-provider> <!--設置密碼加密方式--> <password-encoder ref="passwordEncoder"/> <!--擁有角色的用戶名和密碼--> <user-service> <user name="orange" password="123456" authorities="ROLE_USER"/> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
4. 配置tomcat
<?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"> <!--springSecurity監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-security.xml</param-value> </context-param> <!--springSecurity過濾器--> <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>
5. 測試
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁面</title> </head> <body> <form action="/login" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> <input type="submit" name="submit" value="登錄"> </form> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>spring security</title> </head> <body> 歡迎使用 Spring Security <a href="/logout">退出</a> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄失敗</title> </head> <body> 密碼或用戶名錯誤 </body> </html>