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>
相关推荐
scdifsn2 小时前
动手学深度学习12.7. 参数服务器-笔记&练习(PyTorch)
pytorch·笔记·深度学习·分布式计算·数据并行·参数服务器
DevSecOps选型指南2 小时前
2025软件供应链安全最佳实践︱证券DevSecOps下供应链与开源治理实践
网络·安全·web安全·开源·代码审计·软件供应链安全
ABB自动化2 小时前
for AC500 PLCs 3ADR025003M9903的安全说明
服务器·安全·机器人
恰薯条的屑海鸥2 小时前
零基础在实践中学习网络安全-皮卡丘靶场(第十六期-SSRF模块)
数据库·学习·安全·web安全·渗透测试·网络安全学习
阿部多瑞 ABU4 小时前
主流大语言模型安全性测试(三):阿拉伯语越狱提示词下的表现与分析
人工智能·安全·ai·语言模型·安全性测试
jackson凌5 小时前
【Java学习笔记】SringBuffer类(重点)
java·笔记·学习
huangyuchi.6 小时前
【Linux】LInux下第一个程序:进度条
linux·运维·服务器·笔记·进度条·c/c++
moongoblin6 小时前
行业赋能篇-2-能源行业安全运维升级
运维·安全·协作
Fortinet_CHINA6 小时前
引领AI安全新时代 Accelerate 2025北亚巡展·北京站成功举办
网络·安全
忠于明白6 小时前
Spring AI 核心工作流
人工智能·spring·大模型应用开发·spring ai·ai 应用商业化