目录
1. 搭建环境
1.1 导入相关依赖
<!-- SpringSecurity 对Web 应用进行权限管理-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.10.RELEASE</version>
</dependency>
<!-- SpringSecurity 配置-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.10.RELEASE</version>
</dependency>
<!-- SpringSecurity 标签库-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>4.2.10.RELEASE</version>
</dependency>
1.2 在web.xml中加入相关的过滤器
SpringSecurity的本质就是使用一组过滤器(Filter)来做权限相关的限制,Filter的过滤范围不仅仅是SpringMVC中的Handler的请求,还包括了整个Web应用的所有请求。
这也就是SpringSecurity的权限控制范围。
注意:springSecurityFilterChain过滤器名称不能更改,只有这个名称才能加载那一个过滤器。
要添加的过滤器:
<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>
1.3 添加配置类
@Configuration
@EnableWebSecurity
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {
……
}
@Configuration :标注此类为配置类
@EnableWebSecurity:启用Web环境下的权限控制
2. 用户登录认证
2.1 放行资源
放行的这些资源至少要包含静态资源,登录界面以及登录请求地址(登录的表单要提交的地址),如果登录请求地址不放行的话,SpringSecurity会认为登录请求地址也需要登录成功以后才能访问,而实际上我们要在登录请求地址对应的Handler中完成真正的登录操作。不放行就会陷入死循环。
放行资源的方法:
@Configuration
//启用Web环境下权限控制功能
@EnableWebSecurity
publ