SpringBoot捕获AccessDeniedException
2024-08-30 23:00:01
自定义AccessDeniedHandler
/**
* @Author: jialing xu
* @Description: xvjialing@outlook.com
* @Date: 17:24 2018/8/7
*/
@Service
public class CustomAccessDeniedHandler implements AccessDeniedHandler {
@Autowired
private ObjectMapper objectMapper;
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
response.setContentType("application/json;charset=UTF-8");
Map map = new HashMap();
map.put("code", "403");
map.put("msg", accessDeniedException.getMessage());
map.put("data","");
response.setContentType("application/json");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write(objectMapper.writeValueAsString(map));
}
}
将CustomAccessDeniedHandler加到configure中
@Autowired
CustomAccessDeniedHandler accessDeniedHandler;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.requestMatchers().anyRequest()
.and()
.authorizeRequests()
.antMatchers("/oauth/**").permitAll()
.antMatchers("/actuator","/actuator/**").permitAll()
.and()
.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
}
}
最新文章
- Ioc和Ao使用扩展
- Android 框架学习之 第一天 okhttp &; Retrofit
- 清北学堂模拟赛day7 数字碰撞
- OSG透明代码
- IBatis.Net学习笔记五--常用的查询方式
- Vue.js学习 Item1 --快速入门
- [MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
- Amazon Alexa 语音识别2 : 设置
- jquery.validate 以alert方式显示错误方法
- linux下写脚本时-gt是什么意思
- Linux 下安装配置 JDK1.7
- 从一个微型例子看“C/C++的内存分配机制”和“数组变量名与指针变量名”(转)
- 一 .isinstance(obj,cls)和issubclass(sub,super)
- vue :基本语法格式
- day10.函数进阶
- Qt-Designer打不开
- autoconf配置的项目,编译debug版本
- ORA-00600: internal error code, arguments: [4193]问题解决
- Linux 安装 iptables防火墙
- vue 学习笔记1
热门文章
- javascript基础修炼(12)——手把手教你造一个简易的require.js
- 什么是StatefulSet
- ASP.NET MVC教程八:_ViewStart.cshtml
- WPF TreeView 虚拟化-设置滚动到选中项
- 【SDUT】【链表】2120 - 数据结构实验之链表五:单链表的拆分
- Locust压测结果准确性验证
- Node JS爬虫: 阮老师网站背景图
- thinkphp 5.x No input file specified 解决
- USB免驱RFID读写器编程解析之一:智能卡篇
- 039.[转] 基于 Kubernetes 和 Spring Cloud 的微服务化实践