hincky的主页 hincky的主页
  • 2023

    • nginx
    • prometheus
    • 小工具
    • 部署
  • 数据库

    • MySQL是怎么使用/运行的
    • Redis核心技术与实战
  • 极客时间

    • Web协议详解与抓包实战
    • SQL必知必会
    • MySQL45讲
个人日常
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)

Hincky

当有趣的人,做想做的事
  • 2023

    • nginx
    • prometheus
    • 小工具
    • 部署
  • 数据库

    • MySQL是怎么使用/运行的
    • Redis核心技术与实战
  • 极客时间

    • Web协议详解与抓包实战
    • SQL必知必会
    • MySQL45讲
个人日常
  • 分类
  • 标签
  • 归档
  • 随笔
GitHub (opens new window)
  • java

  • python

  • Spring

  • SpringMVC

  • SpringSecurity

    • SpringSecurity入门
      • 快速入门
        • 准备工作
        • 引入SpringSecurity
    • 认证
      • 登陆校验流程
      • 原理初探
        • SpringSecurity完整流程
        • 认证流程详解
      • 解决问题
        • 思路分析
        • 准备工作
        • 实现
        • 数据库校验用户
        • 准备工作
        • 核心代码实现
        • 密码加密存储
        • 登陆接口
        • 认证过滤器
        • 退出登陆
    • 授权
      • 3. 授权
        • 3.0 权限系统的作用
        • 3.1 授权基本流程
        • 3.2 授权实现
        • 3.2.1 限制访问资源所需权限
        • 3.2.2 封装权限信息
        • 3.2.3 从数据库查询权限信息
        • 3.2.3.1 RBAC权限模型
        • 3.2.3.2 准备工作
        • 3.2.3.3 代码实现
    • 自定义失败处理
    • 跨域
      • 5. 跨域
    • 其他拓展
      • 6. 遗留小问题
        • 其它权限校验方法
        • 自定义权限校验方法
        • 基于配置的权限控制
        • CSRF
        • 认证成功处理器
        • 认证失败处理器
        • 登出成功处理器
        • 其他认证方案畅想
  • Mybatis

  • 设计模式

  • Go

  • 生活记录
  • SpringSecurity
hincky
2022-11-09

自定义失败处理

​ 我们还希望在认证失败或者是授权失败的情况下也能和我们的接口一样返回相同结构的json,这样可以让前端能对响应进行统一的处理。要实现这个功能我们需要知道SpringSecurity的异常处理机制。

​ 在SpringSecurity中,如果我们在认证或者授权的过程中出现了异常会被ExceptionTranslationFilter捕获到。在ExceptionTranslationFilter中会去判断是认证失败还是授权失败出现的异常。

​ 如果是认证过程中出现的异常会被封装成AuthenticationException然后调用AuthenticationEntryPoint对象的方法去进行异常处理。

​ 如果是授权过程中出现的异常会被封装成AccessDeniedException然后调用AccessDeniedHandler对象的方法去进行异常处理。

​ 所以如果我们需要自定义异常处理,我们只需要自定义AuthenticationEntryPoint和AccessDeniedHandler然后配置给SpringSecurity即可。

①自定义实现类

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(HttpStatus.FORBIDDEN.value(), "权限不足");
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response,json);

    }
}

1
2
3
4
5
6
7
8
9
10
11
/**
 * @Author 三更  B站: https://space.bilibili.com/663528522
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        ResponseResult result = new ResponseResult(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录");
        String json = JSON.toJSONString(result);
        WebUtils.renderString(response,json);
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13

②配置给SpringSecurity

​

​ 先注入对应的处理器

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;
1
2
3
4
5

​ 然后我们可以使用HttpSecurity对象的方法去配置。

        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint).
                accessDeniedHandler(accessDeniedHandler);
1
2
编辑 (opens new window)
#SpringSecurity
授权
跨域

← 授权 跨域→

最近更新
01
集成chatgpt的工具
05-24
02
修改服务器ssh默认连接端口
05-22
03
阿里云免费证书
05-15
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Hincky | MIT License | 粤ICP备2022120427号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式