微服务 教程
1085个小节阅读:195.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
使用Spring Security时经常会看见403(无权限),这样的页面很不友好,我们可以自定义403异常处理方案:
编写权限不足页面noPermission.html
xxxxxxxxxx
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>权限不足</title>
</head>
<body>
<h1>您的权限不足,请联系管理员!</h1>
</body>
</html>
编写权限不足处理类
xxxxxxxxxx
public class MyAccessDeniedHandler implements AccessDeniedHandler {
@Override
public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
response.sendRedirect("/noPermission.html");
}
}
在Spring Security配置文件中配置异常处理
xxxxxxxxxx
//异常处理
http.exceptionHandling().
accessDeniedHandler(new MyAccessDeniedHandler());
实时效果反馈
1. Spring Security整合Thymeleaf时,在标签中添加属性可以让标签根据权限展示
A sec:authorize
B th:authorize
C sec:authentication
D th:authorize
2. 在Spring Security中,自定义权限不足处理类需要实现
A AccessHandler
B AccessDeniedHandler
C DeniedHandler
D AccessDenied
答案
1=>A 2=>B