SpringSecurity授权流程(自己做笔记用的)

目录

一、RABC表的设计

二、查询权限并添加Security中

三、通过注解进行授权

四、授权进行前端访问


一、RABC表的设计

基本概念就是五个表:

用户表:users

角色表:role

权限表:permission

还需要两种关系表,才能通过users知道他的权限

用户_角色表

角色_权限表

用户对应角色,角色对应着权限

二、查询权限并添加Security中

基本的创建mapper,自定义方法(通过用户名来查询权限)

html 复制代码
<?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.gq.springsecuritydemo1.mapper.UsersMapper">
    <select id="findByUsername" resultType="com.gq.springsecuritydemo1.pojo.users" parameterType="string">
        select * from users where username=#{username}
    </select>
    <select id="FindPermissionByUsername" parameterType="string" resultType="com.gq.springsecuritydemo1.pojo.permission">
        select distinct permission.pid,permission.permission,permission.url from users left join
            users_role on users.uid=users_role.uid left join
            role on users_role.rid=role.rid left join
            role_permission on role.rid=role_permission.rid left join
            permission on permission.pid=role_permission.pid
             where username=#{username}
    </select>
</mapper>

查询到权限后,接着在UserDetails中将自定义的权限集合转换为Security中的权限集合

java 复制代码
//自定义认证服务
@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private UsersMapper usersMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        users s=usersMapper.findByUsername(username);
        //查询用户的权限
        if(s==null){
            return null;
        }
        List<permission> permissionList=usersMapper.FindPermissionByUsername(username);
        //将自定义的权限集合转换为Security的权限类型集合
        List<GrantedAuthority> grantedAuthorities=new ArrayList<>();
        //开始遍历添加权限
        for(permission permission:permissionList){
            grantedAuthorities.add(new SimpleGrantedAuthority(permission.getUrl()));
        }
        //封装为UserDetailsService对象
        UserDetails userDetailsService= User.withUsername(s.getUsername())
                .password(s.getPassword())
                .authorities(grantedAuthorities)
                .build();
        return userDetailsService;
    }
}

这里的权限你可以写url路径来进行认证,也可以自己随便写点别的,反正作为标识即可。

三、通过注解进行授权

在控制器中使用注解来进行鉴权前,我们需要再启动类中添加注解

复制代码
@EnableMethodSecurity

接着通过@PreAuthorize注解进行鉴权

java 复制代码
 //测试权限的方法
    @PreAuthorize("hasAnyAuthority('/search')")
    @RequestMapping("/search")
    public String s1(){
        return "查询权限";
    }
    @PreAuthorize("hasAnyAuthority('/update')")
    @RequestMapping("/update")
    public String u1(){
        return "修改权限";
    }

四、授权进行前端访问

在进行前端访问前,我们还需要引入thymeleaf和security的整合包,这里我直接把大致需要的所有依赖都写了(毕竟有时候版本出现问题是真烦)

html 复制代码
<dependencies>
<!--        thymeleaf和security的整合包-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
<!--        springboot起步-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
            <scope>runtime</scope>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
<!--        junit单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
<!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.0</version>
        </dependency>
<!--        thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!--        springsecurity-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

在thymeleaf中前端鉴权时,别忘了写约束

html 复制代码
<!DOCTYPE html>
<!-- 约束-->
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h3 sec:authorize="hasAnyAuthority('/search')">查询</h3>
<h3 sec:authorize="hasAnyAuthority('/update')">修改</h3>
<h3 sec:authorize="hasAnyAuthority('/delete')">删除</h3>
<h3 sec:authorize="hasAnyAuthority('/insert')">添加</h3>
</body>
</html>
相关推荐
齐生12 天前
iOS 知识点 - 渲染机制、动画、卡顿小集合
笔记
用户962377954482 天前
VulnHub DC-3 靶机渗透测试笔记
安全
用户962377954482 天前
VulnHub DC-1 靶机渗透测试笔记
笔记·测试
叶落阁主3 天前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
齐生13 天前
iOS 知识点 - IAP 是怎样的?
笔记
tingshuo29174 天前
D006 【模板】并查集
笔记
tingshuo29175 天前
S001 【模板】从前缀函数到KMP应用 字符串匹配 字符串周期
笔记
用户962377954485 天前
DVWA 靶场实验报告 (High Level)
安全
数据智能老司机5 天前
用于进攻性网络安全的智能体 AI——在 n8n 中构建你的第一个 AI 工作流
人工智能·安全·agent