Spring Security工程搭建
1. 工程搭建
创建maven工程,打包方式为war,在pom.xml中添加依赖
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
2. 配置web.xml
在web.xml中主要配置SpringMVC的DispatcherServlet和用于整合第三方框架的DelegatingFilterProxy(代理过滤器,真正的过滤器在spring的配置文件),用于整合Spring Security。
<?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的代理过滤器
1.要整合SpringSecurity,必须要配置这个过滤器DelegatingFilterProxy
2. 名字还不能乱写,一定要写成 springSecurityFilterChain
3. 权限的控制|过滤,一般是针对所有的请求。当然后续我们可以网开一面,针对某些
特定的请求,可以让它不过滤。
-->
<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>
<!--配置前端控制器 DispatcherServlet-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-security03.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
3. 配置spring-security.xml
在spring-security.xml中主要配置Spring Security的拦截规则和认证管理器。
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--
1. 权限的规则:配置哪些地址|连接|请求,需要什么权限才可以访问!
security:http :用于配置权限规则
auto-config : 是否自动配置,帮我们提供登录的页面
true : 默认会提供一个登录的页面
false : 一定要自己写一个登录的页面,并且要做配置。
use-expressions : 是否使用表达式 用于控制下面的标签intercept-url 里面的
access属性是否必须写成表达式的写法 hasRole('Role_xxxx')
use-expressions = "true" , 那么 access :必须写成有表达式的写法hasRole('Role_xxxx')
use-expressions="false", 那么 access :只需要写角色的权限关键字即可,但是一定要以Role_打头
-->
<security:http auto-config="true" use-expressions="false">
<!--
intercept-url: 用来设置什么样的地址,要有什么样的角色(身份)才能访问
pattern : 用来定义访问的地址的匹配表达式 ,如果希望所有的请求都需要权限
access : 指定角色(权限)
access="ROLE_ADMIN" 表示只有管理员的权限,才能访问所有的请求|地址。
-->
<security:intercept-url pattern="/**" access="ROLE_ADMIN"/>
</security:http>
<!--
2. 认证管理:配置哪些用户拥有什么样的角色(权限)
authentication-manager : 认证管理员
authentication-provider : 由它提供认证的规则
user-service : 具体的认证的用户服务
security:user : 提供具体的扫描样的用户对应什么样的权限
name : 用户名
password: 密码 ,如果是明文的密码,需要在前面加上 {noop}
authorities : 代表什么样的权限(角色)
-->
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="{noop}123" authorities="ROLE_ADMIN"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
<mvc:default-servlet-handler/>
</beans>
{noop}:表示当前使用的密码为明文。表示当前密码不需要加密。
4. 访问页面
- 自动跳转到登录页面(springSecurity自动提供的)
- 输入错误用户名和密码
- 输入正确用户名和密码(admin/admin)
注意事项
- 所有的url路径配置的,必须以/开始,否则启动报错
- 过滤器名字不能改(springSecurityFilterChain)
- 明文,没有使用密码加密器时,配置密码的时候的加{noop}
- use-expressions=false, access必须是以ROLE_开始始,否则启动报错