基于SSM实现过滤器及拦截器功能,并实现角色权限功能
程序员文章站
2024-01-21 20:31:34
...
第一部分 过滤器及拦截器功能的实现
一、导入jar包
在pom.xml文件下写入相应的依赖项,Maven即可自动下载。
1、在<properties></properties>内添加版本号:
<spring.security.version>5.0.1.RELEASE</spring.security.version>
2、在<dependencies></dependencies>内添加依赖项:
<!--Security-->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
二、添加过滤器
1、在webapp目录下的web.xml文件中添加过滤器
<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>
2、将spring-security.xml文件导入至resources目录下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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">
<security:global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"></security:global-method-security>
<!-- 配置不拦截的资源 -->
<security:http pattern="/login.jsp" security="none"/>
<security:http pattern="/failer.jsp" security="none"/>
<security:http pattern="/css/**" security="none"/>
<security:http pattern="/img/**" security="none"/>
<security:http pattern="/plugins/**" security="none"/>
<!--
配置具体的规则
auto-config="true" 不用自己编写登录的页面,框架提供默认登录页面
use-expressions="false" 是否使用SPEL表达式(没学习过)
-->
<!--FIXME 以下是重点-->
<security:http auto-config="true" use-expressions="true">
<!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
<security:intercept-url pattern="/**" access="hasAnyRole('ROLE_USER','ROLE_ADMIN')"/>
<security:form-login login-page="/login.jsp"
login-processing-url="/login.do"
default-target-url="/index.jsp"
authentication-failure-url="/failer.jsp"
authentication-success-forward-url="/pages/main.jsp"/>
<!-- 关闭跨域请求 -->
<security:csrf disabled="true"/>
<!--退出并跳转到首页-->
<security:logout invalidate-session="true" logout-url="/logout.do" logout-success-url="/login.jsp"></security:logout>
</security:http>
<!-- 切换成数据库中的用户名和密码 -->
<security:authentication-manager>
<security:authentication-provider user-service-ref="userService">
<!-- 配置加密的方式
<security:password-encoder ref="passwordEncoder"/> -->
</security:authentication-provider>
</security:authentication-manager>
<!-- 配置加密类 -->
<!--<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>-->
<!-- <bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />-->
<!-- 提供了入门的方式,在内存中存入用户名和密码
<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
-->
</beans>
3、重新配置web.xml下配置的加载类路径
<!-- 配置加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml,classpath*:spring-security.xml</param-value>
</context-param>
运行Tomcat,进入用户信息管理系统,可实现对非法请求的过滤和拦截
第二部分 角色权限功能的实现
1、在数据库中创建表tb_role与表tb_user_role
2、在bean下创建一个实体类Role.java
package com.zhongruan.bean;
public class Role {
private int id;
private String roleName;
private String roleDesc;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleDesc() {
return roleDesc;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
'}';
}
}
3、创捷接口RoleDao.java
package com.zhongruan.dao;
import com.zhongruan.bean.Role;
import java.util.List;
public interface RoleDao {
// 在role表中根据userId查询出当前的的用户
List<Role> findRoleByUserId(int userId);
}
4、UserService.java接口继承UserDetailsService并在UserInfoServiceImpl下实现
public interface UserInfoService extends UserDetailsService
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//1.查询当前登录的用户信息
UserInfo userInfo = userDao.doLogin(username);
//2.查询当前的用户有多少角色
List<Role> roleList = roleDao.findRoleByUserId(userInfo.getId());
//3.需要把角色给放入用户中
userInfo.setRoleList(roleList);
//4.把查询到的User和Role数据给到Spring-security中的内置对象User来管理
User user=new User(userInfo.getUsername(),"{noop}"+userInfo.getPassword(),getAuthority(userInfo.getRoleList()));
return user;
}
private Collection<? extends GrantedAuthority> getAuthority(List<Role> roleList) {
List<SimpleGrantedAuthority> list=new ArrayList<>();
for (Role role:roleList){
list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
}
return list;
}
5、编写roleMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.UserDao" >
<select id="findRoleByUserId" parameterType="java.lang.Integer" resultType="com.zhongruan.bean.Role">
select * from tb_Role where id in (select roleId from tb_user_role where userId=#{userId} )
</select>
</mapper>
7、改写aside.jsp
<security:authorize access="hasRole('ADMIN')">
<a
href="${pageContext.request.contextPath}/user/finAll.do?page=1&size=5"> <i
class="fa fa-circle-o"></i> 用户管理
</a>
</security:authorize>
8、运行Tomcat,检验到权限功能已实现。
上一篇: Windows启用Linux环境并安装.NET SDK
下一篇: Ionic IOS适配的那些坑