spring security是一個提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組能夠在 Spring 應用上下文中配置的 Bean,充分利用了Spring IoC,DI(控制反轉 Inversion of Control ,DI:Dependency Injection 依賴注入)和 AOP(面向切面編程)功能,爲應用系統提供聲明式的安全訪問控制功能,減小了爲企業系統安全控制編寫大量重複代碼的工做。html
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.buwei</groupId> 8 <artifactId>spring-security-demo</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <!-- 集中定義依賴版本號 --> 13 <properties> 14 <spring.version>4.2.4.RELEASE</spring.version> 15 <servlet-api.version>2.5</servlet-api.version> 16 <mysql.version>5.1.32</mysql.version> 17 <druid.version>1.0.9</druid.version> 18 <security.version>3.2.3.RELEASE</security.version> 19 </properties> 20 21 <dependencies> 22 23 <!--spring security的依賴--> 24 <dependency> 25 <groupId>org.springframework.security</groupId> 26 <artifactId>spring-security-web</artifactId> 27 <version>4.1.0.RELEASE</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.security</groupId> 31 <artifactId>spring-security-config</artifactId> 32 <version>4.1.0.RELEASE</version> 33 </dependency> 34 <!-- Spring相關依賴 --> 35 <dependency> 36 <groupId>org.springframework</groupId> 37 <artifactId>spring-context</artifactId> 38 <version>${spring.version}</version> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework</groupId> 42 <artifactId>spring-beans</artifactId> 43 <version>${spring.version}</version> 44 </dependency> 45 <dependency> 46 <groupId>org.springframework</groupId> 47 <artifactId>spring-webmvc</artifactId> 48 <version>${spring.version}</version> 49 </dependency> 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-jdbc</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 <dependency> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-aspects</artifactId> 58 <version>${spring.version}</version> 59 </dependency> 60 <dependency> 61 <groupId>org.springframework</groupId> 62 <artifactId>spring-jms</artifactId> 63 <version>${spring.version}</version> 64 </dependency> 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-context-support</artifactId> 68 <version>${spring.version}</version> 69 </dependency> 70 <dependency> 71 <groupId>org.springframework</groupId> 72 <artifactId>spring-test</artifactId> 73 <version>${spring.version}</version> 74 </dependency> 75 76 </dependencies> 77 78 <build> 79 <plugins> 80 <!-- java編譯插件 --> 81 <plugin> 82 <groupId>org.apache.maven.plugins</groupId> 83 <artifactId>maven-compiler-plugin</artifactId> 84 <version>3.2</version> 85 <configuration> 86 <source>1.8</source> 87 <target>1.8</target> 88 <encoding>UTF-8</encoding> 89 </configuration> 90 </plugin> 91 <plugin> 92 <groupId>org.apache.tomcat.maven</groupId> 93 <artifactId>tomcat7-maven-plugin</artifactId> 94 <configuration> 95 <!--指定端口號--> 96 <port>8080</port> 97 <!--制定路徑--> 98 <path>/</path> 99 </configuration> 100 </plugin> 101 </plugins> 102 </build> 103 </project>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 7 <!-- 配置監聽掃描spring-security的文件 --> 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:spring-security.xml</param-value> 11 </context-param> 12 <listener> 13 <listener-class> 14 org.springframework.web.context.ContextLoaderListener 15 </listener-class> 16 </listener> 17 <!--配置過濾器,過濾根目錄下全部資源,springSecurityFilterChain這個名字不能改變--> 18 <filter> 19 <filter-name>springSecurityFilterChain</filter-name> 20 <filter-class> 21 org.springframework.web.filter.DelegatingFilterProxy 22 </filter-class> 23 </filter> 24 <filter-mapping> 25 <filter-name>springSecurityFilterChain</filter-name> 26 <url-pattern>/*</url-pattern> 27 </filter-mapping> 28 </web-app>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans:beans 3 xmlns="http://www.springframework.org/schema/security" 4 xmlns:beans="http://www.springframework.org/schema/beans" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> 8 9 <!--設置不攔截頁面--> 10 <http pattern="/login.html" security="none"/> 11 <http pattern="/login_error.html" security="none"/> 12 13 <!-- use-expressions:設置是否啓動SpEL表達式,默認值是true。 14 啓動的時候access設置爲access="hasRole('ROLE_USER')"--> 15 <http use-expressions="false"> 16 <!-- 17 配置SpringSecurity的攔截路徑(攔截規則) 18 * pattern:配置攔截規則。 /* 表明的是根路徑下的全部資源(不包含子路徑) /**表明的是根路徑下全部的資源(包含子路徑) 19 * access:設置角色 角色命名 ROLE_角色名稱 如:ROLE_USER 20 --> 21 <intercept-url pattern="/**" access="ROLE_USER"/> 22 <!-- 23 開啓表單驗證 24 login-processing-url:提交的路徑的設置 默認值"/login" 能夠修改 25 username-parameter="username" 26 password-parameter="password" 27 login-page :登陸頁面名稱 以 / 開始 28 default-target-url :登陸成功後跳轉的頁面 29 authentication-failure-url:登陸失敗後跳轉的頁面 30 --> 31 <form-login login-page="/login.html" default-target-url="/index.html" 32 always-use-default-target="true" authentication-failure-url="/login_error.html"/> 33 <!-- 不使用csrf的校驗 --> 34 <csrf disabled="true"/> 35 <!-- 註銷的配置 --> 36 <logout logout-url="/logout" logout-success-url="/logout.html" /> 37 </http> 38 39 <!-- 配置認證管理器 --> 40 <authentication-manager> 41 <!-- 認證的提供者,這裏配置固定寫死,項目中使用的時候能夠配置單獨的bean來做爲認證的提供者 --> 42 <authentication-provider> 43 <user-service> 44 <user name="buwei" password="123456" authorities="ROLE_USER"/> 45 </user-service> 46 </authentication-provider> 47 </authentication-manager> 48 49 </beans:beans>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首頁</title> 6 </head> 7 <body> 8 <h1>恭喜你,登陸成功</h1> 9 <!--"/logout"爲spring security默認提供的登出路徑--> 10 <a href="/logout">退出登陸</a> 11 </body> 12 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登陸界面</title> 6 </head> 7 <body> 8 9 <h1>spring security登陸測試頁面</h1> 10 <!--action默認配置提交到"/login",提交形式必須爲post--> 11 <form action="/login" method="post"> 12 用戶名:<input type="text" name="username"><br> 13 密碼:<input type="password" name="password"><br> 14 <button type="submit" name="submit">登陸</button> 15 </form> 16 </body> 17 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登陸失敗界面</title> 6 </head> 7 <body> 8 <h1>用戶名或密碼錯誤</h1> 9 </body> 10 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>首頁</title> 6 </head> 7 <body> 8 <h1>退出成功</h1> 9 </body> 10 </html>
啓動項目,瀏覽器中輸入地址:http://localhost:8080/index.html,會自動跳轉到login.htmljava
嘗試着輸入正確的用戶名和密碼以及錯誤的用戶名和密碼來作測試。mysql
1 package com.buwei; 2 3 import org.springframework.security.core.GrantedAuthority; 4 import org.springframework.security.core.authority.SimpleGrantedAuthority; 5 import org.springframework.security.core.userdetails.User; 6 import org.springframework.security.core.userdetails.UserDetails; 7 import org.springframework.security.core.userdetails.UserDetailsService; 8 import org.springframework.security.core.userdetails.UsernameNotFoundException; 9 10 import java.util.ArrayList; 11 import java.util.List; 12 13 /** 14 * 認證類 15 * 16 * @author buwei 17 * @date 2018/12/11 9:13 18 */ 19 public class UserDetailsServiceImpl implements UserDetailsService { 20 @Override 21 public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException { 22 System.out.println("通過了認證類"); 23 // 構建角色列表,實際的驗證過程當中咱們的角色列表是要從數據庫中查出來的,這裏直接設置了 24 List<GrantedAuthority> grandAuths = new ArrayList<GrantedAuthority>(); 25 // 設置一個訪問權限須要的角色名稱 26 grandAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 27 // 在設置一個不是訪問權限須要的角色名稱 28 grandAuths.add(new SimpleGrantedAuthority("ROLE_TEST")); 29 // 下面的參數也是假的參數,只是爲了簡單測試 30 return new User("buwei", "123456", grandAuths); 31 } 32 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans:beans 3 xmlns="http://www.springframework.org/schema/security" 4 xmlns:beans="http://www.springframework.org/schema/beans" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd"> 8 9 <!--設置不攔截頁面--> 10 <http pattern="/login.html" security="none"/> 11 <http pattern="/login_error.html" security="none"/> 12 13 <!-- use-expressions:設置是否啓動SpEL表達式,默認值是true。 14 啓動的時候access設置爲access="hasRole('ROLE_USER')"--> 15 <http use-expressions="false"> 16 <!-- 17 配置SpringSecurity的攔截路徑(攔截規則) 18 * pattern:配置攔截規則。 /* 表明的是根路徑下的全部資源(不包含子路徑) /**表明的是根路徑下全部的資源(包含子路徑) 19 * access:設置角色 角色命名 ROLE_角色名稱 如: ROLE_USER 20 --> 21 <intercept-url pattern="/**" access="ROLE_USER"/> 22 <!-- 23 開啓表單驗證 24 login-processing-url:提交的路徑的設置 默認值"/login" 能夠修改 25 username-parameter="username" 26 password-parameter="password" 27 login-page :登陸頁面名稱 以 / 開始 28 default-target-url :登陸成功後跳轉的頁面 29 authentication-failure-url:登陸失敗後跳轉的頁面 30 --> 31 <form-login login-page="/login.html" default-target-url="/index.html" 32 always-use-default-target="true" authentication-failure-url="/login_error.html"/> 33 <!-- 不使用csrf的校驗 --> 34 <csrf disabled="true"/> 35 <!-- 註銷的配置 --> 36 <logout logout-url="/logout" logout-success-url="/logout.html" /> 37 </http> 38 39 <!-- 配置認證管理器 --> 40 <authentication-manager> 41 <!-- 認證的提供者 --> 42 <authentication-provider user-service-ref="userDetailsService"> 43 <!--<user-service> 44 <user name="buwei" password="123456" authorities="ROLE_USER"/> 45 </user-service>--> 46 </authentication-provider> 47 </authentication-manager> 48 49 <!--配置認證類--> 50 <beans:bean id="userDetailsService" class="com.buwei.UserDetailsServiceImpl"> 51 </beans:bean> 52 53 </beans:beans>