当前位置: 首页 > news >正文

SpringSecurity抛出异常但AccessDeniedHandler不生效

文章目录

    • 复现
    • 原因

复现

@Beanpublic SecurityFilterChain securedFilterChain(HttpSecurity http) throws Exception {//...//异常http.exceptionHandling(except -> {except.authenticationEntryPoint(new SecurityAuthenticationEntryPoint());except.accessDeniedHandler((request, response, e) -> { //请求未授权的接口//创建结果对象HashMap result = new HashMap();result.put("code", -1);result.put("message", "没有权限");//转换成json字符串String json = JSON.toJSONString(result);//返回响应response.setContentType("application/json;charset=UTF-8");response.getWriter().println(json);});//...});

还是抛出异常

org.springframework.security.access.AccessDeniedException: Access Deniedat org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor.attemptAuthorization(AuthorizationManagerBeforeMethodInterceptor.java:256) ~[spring-security-core-6.2.1.jar:6.2.1]at org.springframework.security.authorization.method.AuthorizationManagerBeforeMethodInterceptor.invo

原因

@RestControllerAdvice
全局异常拦截到了直接返回,注释掉
或者采用

import org.springframework.security.access.AccessDeniedException
//...
@ExceptionHandler(AccessDeniedException.class)
public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException {throw e;
}
//...
http://www.lryc.cn/news/517134.html

相关文章:

  • 高清绘画素材3600多张动漫线稿线描上色练习参考插画原画
  • EXCEL技巧
  • python制作翻译软件
  • ollama+FastAPI部署后端大模型调用接口
  • BERT:深度双向Transformer的预训练用于语言理解
  • 【AI-23】深度学习框架中的神经网络3
  • 网站运营数据pv、uv、ip
  • 高阶知识库搭建实战五、(向量数据库Milvus安装)
  • 【TR369】RTL8197FH-VG+RTL8812F增加TR369 command节点
  • FPGA实现UART对应的电路和单片机内部配合寄存器实现的电路到底有何区别?
  • 数据库模型全解析:从文档存储到搜索引擎
  • 【Java基础】Java异常捕捉,throws/throw、finally、try、catch关键字的含义与运用
  • Android Studio 安装配置(个人笔记)
  • 计算机网络——数据链路层-介质访问控制
  • pytest日志显示
  • 【信息系统项目管理师】第15章:项目风险管理过程详解
  • Diffusers 使用 LoRA
  • 云安全博客阅读(二)
  • SpringCloud系列教程:微服务的未来(六)docker教程快速入门、常用命令
  • Vue 快速入门:开启前端新征程
  • UVM:uvm_component methods configure
  • LLM 训练中存储哪些矩阵:权重矩阵,梯度矩阵,优化器状态
  • 大模型思维链推理的进展、前沿和未来分析
  • NLP 技术的突破与未来:从词嵌入到 Transformer
  • 嵌入式中QT实现文本与线程控制方法
  • 云备份项目--服务端编写
  • Node.js——fs(文件系统)模块
  • SAP BC 同服务器不同client之间的传输SCC1
  • CentOS: RPM安装、YUM安装、编译安装(详细解释+实例分析!!!)
  • linux音视频采集技术: v4l2