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>
相关推荐
张彦峰ZYF1 小时前
从智能体到生产系统:企业 Agent 规模化的治理、记忆、知识与安全
人工智能·安全·架构·operatingsystem·agent platform·agentcontrol·agent mesh
Space655361 小时前
常见组学技术笔记:从 DNA 基因组学到 Multi-omics
笔记
xianghongtao01161 小时前
麦肯锡2026技术趋势06_网络安全与可信系统_研究解读
人工智能·安全·web安全
lcj25113 小时前
【Linux】 Makefile 详解
linux·笔记·面试
迪康Defender3 小时前
终端安全事后追溯能力解析:本地审计模块设计思路与落地实践
运维·开发语言·人工智能·安全·php·安全威胁分析·运维开发
常州晟凯电子科技3 小时前
sigmaStar SSC305DE/SSC308开发笔记之开发环境搭建和SDK编译
人工智能·笔记·嵌入式硬件·物联网·性能优化·视频编解码
2401_868534783 小时前
安全故障分析
服务器·网络·安全
开开心心就好3 小时前
视频压缩工具推荐,绿色版免安装双击即用
java·前端·人工智能·spring·智能手机·intellij-idea·excel
专业程序开发源3 小时前
springboot古城景区管理系统88564-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计
wdfk_prog3 小时前
LWIP教程 00:从网线上的电信号到 lwIP——PHY、MAC、DMA 与 `netif` 的完整边界
运维·网络·笔记·学习·ip