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


    目录

    一、RABC表的设计

    二、查询权限并添加Security中

    三、通过注解进行授权

    四、授权进行前端访问


    一、RABC表的设计

    基本概念就是五个表:

    用户表:users

    角色表:role

    权限表:permission

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

    用户_角色表

    角色_权限表

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

    二、查询权限并添加Security中

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

    1. "1.0" encoding="UTF-8"?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.gq.springsecuritydemo1.mapper.UsersMapper">
    6. <select id="findByUsername" resultType="com.gq.springsecuritydemo1.pojo.users" parameterType="string">
    7. select * from users where username=#{username}
    8. select>
    9. <select id="FindPermissionByUsername" parameterType="string" resultType="com.gq.springsecuritydemo1.pojo.permission">
    10. select distinct permission.pid,permission.permission,permission.url from users left join
    11. users_role on users.uid=users_role.uid left join
    12. role on users_role.rid=role.rid left join
    13. role_permission on role.rid=role_permission.rid left join
    14. permission on permission.pid=role_permission.pid
    15. where username=#{username}
    16. select>
    17. mapper>

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

    1. //自定义认证服务
    2. @Service
    3. public class MyUserDetailsService implements UserDetailsService {
    4. @Autowired
    5. private UsersMapper usersMapper;
    6. @Override
    7. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    8. users s=usersMapper.findByUsername(username);
    9. //查询用户的权限
    10. if(s==null){
    11. return null;
    12. }
    13. List permissionList=usersMapper.FindPermissionByUsername(username);
    14. //将自定义的权限集合转换为Security的权限类型集合
    15. List grantedAuthorities=new ArrayList<>();
    16. //开始遍历添加权限
    17. for(permission permission:permissionList){
    18. grantedAuthorities.add(new SimpleGrantedAuthority(permission.getUrl()));
    19. }
    20. //封装为UserDetailsService对象
    21. UserDetails userDetailsService= User.withUsername(s.getUsername())
    22. .password(s.getPassword())
    23. .authorities(grantedAuthorities)
    24. .build();
    25. return userDetailsService;
    26. }
    27. }

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

    三、通过注解进行授权

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

    @EnableMethodSecurity
    

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

    1. //测试权限的方法
    2. @PreAuthorize("hasAnyAuthority('/search')")
    3. @RequestMapping("/search")
    4. public String s1(){
    5. return "查询权限";
    6. }
    7. @PreAuthorize("hasAnyAuthority('/update')")
    8. @RequestMapping("/update")
    9. public String u1(){
    10. return "修改权限";
    11. }

    四、授权进行前端访问

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

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.thymeleaf.extrasgroupId>
    4. <artifactId>thymeleaf-extras-springsecurity5artifactId>
    5. dependency>
    6. <dependency>
    7. <groupId>org.springframework.bootgroupId>
    8. <artifactId>spring-boot-starter-webartifactId>
    9. dependency>
    10. <dependency>
    11. <groupId>mysqlgroupId>
    12. <artifactId>mysql-connector-javaartifactId>
    13. <version>8.0.29version>
    14. <scope>runtimescope>
    15. dependency>
    16. <dependency>
    17. <groupId>org.projectlombokgroupId>
    18. <artifactId>lombokartifactId>
    19. <optional>trueoptional>
    20. dependency>
    21. <dependency>
    22. <groupId>junitgroupId>
    23. <artifactId>junitartifactId>
    24. <scope>testscope>
    25. dependency>
    26. <dependency>
    27. <groupId>com.baomidougroupId>
    28. <artifactId>mybatis-plus-boot-starterartifactId>
    29. <version>3.5.0version>
    30. dependency>
    31. <dependency>
    32. <groupId>org.springframework.bootgroupId>
    33. <artifactId>spring-boot-starter-thymeleafartifactId>
    34. dependency>
    35. <dependency>
    36. <groupId>org.springframework.bootgroupId>
    37. <artifactId>spring-boot-starter-securityartifactId>
    38. dependency>
    39. <dependency>
    40. <groupId>org.springframework.bootgroupId>
    41. <artifactId>spring-boot-starter-testartifactId>
    42. <scope>testscope>
    43. dependency>
    44. dependencies>

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

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org"
    3. xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
    4. <head>
    5. <meta charset="UTF-8">
    6. <title>Titletitle>
    7. head>
    8. <body>
    9. <h3 sec:authorize="hasAnyAuthority('/search')">查询h3>
    10. <h3 sec:authorize="hasAnyAuthority('/update')">修改h3>
    11. <h3 sec:authorize="hasAnyAuthority('/delete')">删除h3>
    12. <h3 sec:authorize="hasAnyAuthority('/insert')">添加h3>
    13. body>
    14. html>

     

  • 相关阅读:
    探讨NLP对行业大量数据信息抽取的技术实现
    Electron 结合 nginx 配置自动更新
    HashMap原理
    sheng的学习笔记-【中英】【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第四周测验
    金融开放度指数-世界银行三位数字编码、ISO Alpha-3 Code等多指标数据
    C语言实现——简易通讯录
    七夕给女朋友准备的小惊喜网站制作(html+css+js)
    Linux 查看 CPU核数 及 内存
    两个全景vr网站 比百度要好
    Spring系列十:Spring MVC深度学习
  • 原文地址:https://blog.csdn.net/gaoqiandr/article/details/138164875