基于Spring Boot 3 + Spring Security6 + JWT + Redis实现接口资源鉴权

紧接上一篇文章,基于Spring Boot 3 + Spring Security6 + JWT + Redis实现接口资源鉴权

系列文章指路👉
系列文章-基于SpringBoot3创建项目并配置常用的工具和一些常用的类

项目源码👉
/shijizhe/boot-test

1. 修改 UserDetailsService

采用RBCA模型进行权限控制.

简化后我们有三个表: 用户表、用户角色关联表、角色权限关联表(表结构源码中有:src/main/resources/static/数据结构.sql)

修改取用户的权限列表

     <select id="listAuthorityById" resultType="java.lang.String">
          SELECT pe.permission_id
          FROM ya_user_role ro,
               ya_role_permission pe
          WHERE ro.role_id = pe.role_id
          AND ro.user_id = #{userId}
     </select>

将权限列表放入UserDetail中

在这里插入图片描述

2. 新增认证和鉴权异常统一处理程序

做这一步的目的是:程序遇到未知的错误,仍可以返回json形式的错误信息,可以帮助排查问题。

实现AuthenticationEntryPoint

/**
 * <p>
 *  认证异常处理
 * </p>
 *
 * @author Ya Shi
 * @since 2024/3/28 16:01
 */
@Component
@Slf4j
public class YaAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("YaAuthenticationEntryPoint commence 认证过程中遇到异常:{}", authException.toString());
        ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_UNAUTHORIZED.code, "Security auth failed."));
    }
}

实现AccessDeniedHandler

/**
 * <p>
 * 鉴权异常处理
 * </p>
 *
 * @author Ya Shi
 * @since 2024/3/28 16:06
 */
@Component
@Slf4j
public class YaAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        log.error("YaAccessDeniedHandler handle 鉴权过程中遇到异常:{}", accessDeniedException.toString());
        ServletUtils.renderResult(response, new BaseResult<>(ResultEnum.FAILED_FORBIDDEN.code, "鉴权失败:" + accessDeniedException.getMessage()));
    }
}

修改Security配置 YaSecurityConfig.securityFilterChain

  1. 允许方法安全授权
@EnableMethodSecurity
  1. securityFilterChain新增异常统一处理
 .exceptionHandling()
      .authenticationEntryPoint(authenticationEntryPoint)
      .accessDeniedHandler(accessDeniedHandler)
      .and()

剩余重复代码就不贴了,上篇文章有。
在这里插入图片描述
在这里插入图片描述

简单测试

给方法加权限控制

@PreAuthorize("hasAuthority('ya_fruit_list')")
    @GetMapping("/testApi")
    @Operation(summary = "testApi", description = "测试使用-直接返回成功")
    @PreAuthorize("hasAuthority('ya_fruit_list')")
    public Object testApi(){
        String userId = UserAuthUtils.getUserId();
        System.out.println(userId);
        return BaseResult.success(userId);
    }

调用测试(事先已登录)

在这里插入图片描述
请求一个没有权限的接口:

    @GetMapping("/testApi2")
    @Operation(summary = "testApi2", description = "测试使用2-直接返回成功")
    @PreAuthorize("hasAuthority('test_test_123456')")
    public Object testApi2(){
        String userId = UserAuthUtils.getUserId();
        System.out.println(userId);
        return BaseResult.success(userId);
    }

在这里插入图片描述

相关推荐

  1. 基于SpringCloudGateway实现接口

    2024-03-30 07:48:01       36 阅读
  2. springboot项目jwt认证(企业级实现方案)

    2024-03-30 07:48:01       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 07:48:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 07:48:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 07:48:01       87 阅读
  4. Python语言-面向对象

    2024-03-30 07:48:01       96 阅读

热门阅读

  1. VUE——mixins混入

    2024-03-30 07:48:01       43 阅读
  2. 如何避免公网IP安全风险

    2024-03-30 07:48:01       41 阅读
  3. MongoDB聚合运算符:$last

    2024-03-30 07:48:01       44 阅读
  4. Linux内网提权

    2024-03-30 07:48:01       44 阅读
  5. 机器学习:scikit-learn库的主要组件

    2024-03-30 07:48:01       39 阅读
  6. Leetcode 15. 三数之和

    2024-03-30 07:48:01       39 阅读
  7. 美团二面极差体验

    2024-03-30 07:48:01       40 阅读
  8. 接口和抽象类有什么区别?

    2024-03-30 07:48:01       39 阅读
  9. 实现公网数据传输给内网(使用frp)

    2024-03-30 07:48:01       41 阅读
  10. DM Mysql Oracle 日期函数 dameng

    2024-03-30 07:48:01       37 阅读
  11. 学生管理系统本地化存储版

    2024-03-30 07:48:01       45 阅读
  12. Redis Scan指令解析与使用示例

    2024-03-30 07:48:01       43 阅读
  13. MongoDB聚合运算符:$linearFill

    2024-03-30 07:48:01       37 阅读
  14. C# 命名空间的两种定义哦写法与区别

    2024-03-30 07:48:01       37 阅读