1.创建spring-security-demo项目
2.修改pom.xml
<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.ronybo.demo</groupId>
<artifactId>spring-security-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.2.4.RELEASE</spring.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>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<!-- 引入安全框架结束 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>9090</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.在webapp
下创建WEB-INF
目录,创建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>
<!-- 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.在webapp
下创建index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Spring-Security-Demo</title>
</head>
<body>
欢迎进入SpringSecurity
</body>
</html>
5.在resources
目录下创建spring-security.xml
文件
名字随便起,但是需要和web.xml
里面的<param-value>classpath:spring-security.xml</param-value>
名称相同
<?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">
<!-- 页面的拦截规则 use-expressions:是否启用SPEL表达式 -->
<!-- 如果不配use-expressions,则access需要写成hasRole('ROLE_USER') -->
<http use-expressions="false">
<!-- 拦截的URL,这里配置了一个拦截规则 -->
<!-- 当前用户必须拥有ROLE_USER的角色 才可以访问根目录及所属子目录的资源 ROLE_固定 后面自定义 -->
<intercept-url pattern="/**" access="ROLE_USER"/><!-- /*只能拦截根目录下的文件,/**能拦截根目录下的子目录下的文件 -->
<!-- 开启表单登陆功能 -->
<form-login/>
</http>
<!-- 认证管理器 -->
<authentication-manager>
<!-- 认证提供者 -->
<authentication-provider>
<user-service>
<!-- 用来配置当前系统的用户,authorities:这个用户属于哪一个角色 -->
<!-- admin只能访问ROLE_USER角色配置的pattern下的资源,如果当前没有登录,那么这些资源就无法访问 -->
<user name="admin" password="123456" authorities="ROLE_USER"/>
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
和之前springmvc.xml
结构不同,之前springmvc.xml
父标签是beans
,因为beans
是默认的。在这里security
是默认的,默认的不需要加前缀,所以在这里beans
需要使用beans:beans
前缀。因为security
不带前缀的比较多比如:http
,header
,headers
,hpkp
等,而beans
就一个bean
不带前缀,所以让security
座位默认的,便于开发。
6.运行项目
SpringSecurity
自动生成登录页面,因为<form-login/>
这里配置了。
7.总结
<intercept-url pattern="/**" access="ROLE_USER"/>
<user name="admin" password="123456" authorities="ROLE_USER"/>
8.自定义登录页面
<form-login login-page="/login.html"
default-target-url="/index.html"
authentication-failure-url="/login_error.html" />
login-page
:用户没有登录时重定向到的页面
default-target-url
:成功页面
authentication-failure-url
:错误页面
9.设置页面不登录也可以访问
<http pattern="/login.html" security="none" />
<http pattern="/login_error.html" security="none" />
10.关闭CSRF
<csrf disabled="true"/>
超链接提交表单
<a onclick="document:loginform.submit()">提交</a>
loginform
为form
的id
。
SpringSecurity默认将iframe拦截,需要在配置文件中开启
<headers>
<frame-options policy="SAMEORIGIN"/>
</headers>
如果没有登录无论访问哪个页面都跳转到登录页面,如果访问其他页面,SpringSecurity会跳转到登录页面,登陆成功后或跳转到刚才访问的页面,这样适用于前台页面,比如淘宝购物车,如果没有登录,会跳转到登录页面,登陆成功后再跳转到购物车,但是在后台页面,无论访问哪个页面都应该跳转到主页面。这时需要在<form-login>
标签中添加这样一条属性使页面总是跳转到主页
<form-login always-use-default-target="true"/>
退出登录
spring-security.xml ↓↓↓↓
<logout/>
index.html ↓↓↓↓
<a href="../logout">注销</a>