1.Spring Security框架入门
1.1Spring Security简介
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架。它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
1.2Spring Security入门小Demo
1.2.1最简单Demo
(1)创建web模块springsecurity-test
(2)pom.xml
<?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"> <parent> <artifactId>pyg-test</artifactId> <groupId>cn.itcast</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springsecurity-test</artifactId> <packaging>war</packaging> <name>springsecurity-test</name> <!-- 配置依赖关系 --> <dependencies> <!-- servlet-api、jsp-api、el-api --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>8.5.16</version> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.10.RELEASE</version> </dependency> <!-- Spring-Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>4.2.3.RELEASE</version> </dependency> </dependencies> <!-- 项目构建部分 --> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 指定端口 --> <port>9090</port> <!-- 请求路径 --> <path>/</path> </configuration> </plugin> </plugins> </build> </project> |
(3)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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- 配置Spring加载文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <!-- 配置Spring的核心监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置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> |
(4)创建spring 配置文件applicationContext-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"> <!-- 配置请求拦截规则 --> <security:http use-expressions="false"> <!-- 配置拦截的请求URL,通过什么角色或权限访问 --> <security:intercept-url pattern="/**" access="ROLE_USER"/> <!-- 配置开启表单登录 --> <security:form-login/> </security:http> <!-- 配置认证管理器 --> <security:authentication-manager> <!-- 配置认证提供者 --> <security:authentication-provider> <!-- 配置用户服务 --> <security:user-service> <!-- 认证用户信息: 用户名、密码、角色或权限 --> <security:user name="admin" password="123456" authorities="ROLE_USER"/> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans> |
此案例我们没有登录页面,而是使用了系统自动生成的登录页面,效果如下:
配置说明:
<security:intercept-urlpattern=”/**”/>:表示拦截的地址
/* 表示的是该目录下的资源,只包括本级目录不包括下级目录
/** 表示的是该目录以及该目录下所有级别子目录的资源
<security:form-login/>: 开启表单登录
<security-http use-expressions=”false”>:是否使用使用 Spring 表达式语言(SpEL),默认为true ,如果开启,则拦截的配置应该写成以下形式:
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/> |
1.2.2用户自定义登录页
实际开发中,我们不可能使用系统生成的登录页面,而是使用我们自己的登录页面。
(1)构建登录页面login.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action='/login' method='post'> <table> <tr><td>用户名:</td> <td><input type='text' name='username'></td></tr> <tr><td>密码:</td> <td><input type='password' name='password'/></td></tr> <tr> <td colspan='2'><input type="submit" value="登录"/></td> </tr> </table> </form> </body> </html> |
(2)构建登录失败页面 login_error.html(内容略)
(3)构建登录成功页面 login_success.html(内容略)
(4)修改spring配置文件applicationContext-security.xml
<!-- 配置哪些请求地址不需要拦截 --> <security:http pattern="/login.html" security="none"/> <security:http pattern="/login_error.html" security="none"/> <!-- 配置请求拦截规则 use-expressions: 是否用SpEL表达式 --> <security:http use-expressions="false"> <!-- 配置拦截的请求URL,通过什么角色或权限访问 --> <security:intercept-url pattern="/**" access="ROLE_USER"/> <!-- 配置开启表单登录 login-page: 配置自己的登录页面 authentication-success-forward-url: 认证成功需要跳转的URL authentication-failure-url: 认证失败需要跳转的URL password-parameter: 密码请求参数名称 username-parameter: 用户名请求参数名称 --> <security:form-login login-page="/login.html" authentication-success-forward-url="/login_success.html" authentication-failure-url="/login_error.html" password-parameter="password" username-parameter="username"/> <!-- 关闭跨站请求伪造CSRF --> <security:csrf disabled="true"/> </security:http> |
security="none" 设置此资源不被拦截.
如果你没有设置登录页security="none",将会出现以下错误
说明:因为登录页面会被反复重定向。
login-page:指定登录页面。
authentication-failure-url:指定了身份验证失败时跳转到的页面。
authentication-success-forward-url:指定身份验证成功时跳转到的页面。
default-target-url:指定了成功进行身份验证和授权后默认呈现给用户的页面。
password-parameter: 密码请求参数名称 username-parameter: 用户名请求参数名称
csrfdisabled="true" 关闭csrf ,如果不加会出现错误
CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站的恶意利用。