本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
基于spring-security 401 403错误自定义处理方案
我是陈晓 · 240浏览 · 发布于2021-07-26 +关注

这篇文章主要介绍了基于spring-security 401 403错误自定义处理方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

spring-security 401 403错误自定义处理

为了返回给前端统一的数据格式,

一般所有的数据都会以类似下面的方式返回:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class APIResultDto<T> {

    /**

     * 状态码:-1代表成功,具体参考APIErrorCode类

     */

    private int er;

  

    /**

     * 状态描述,可以自行设置或使用APIErrorCode类中默认描述

     */

    private String erMessage;

  

    /**

     * 实际返回实体,isSuccess()返回true时该字段有效

     */

    private T items;

}

但是一些框架,比如本文要说的spring-security是不按照我们自定义规范处理的,幸运的是spring-security框架给了我们可以定制化的地方,只需继承

1

ResourceServerConfigurerAdapter

重写

1

public void configure(ResourceServerSecurityConfigurer resources) throws Exception

方法即可,在里面添加自定义的针对授权时返回的401以及403错误码,

具体如下:

1

2

3

4

5

6

7

8

9

10

@Autowired

    private AccessDeniedHandler accessDeniedHandler;

    @Autowired

    private AuthenticationEntryPoint authenticationEntryPoint;

  

    @Override

    public void configure(ResourceServerSecurityConfigurer resources) {

        resources.authenticationEntryPoint(authenticationEntryPoint);

        resources.accessDeniedHandler(accessDeniedHandler);

    }

里面涉及到的AccessDeniedHandler以及AuthenticationEntryPoint

如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

@Component

public class CustomizedAuthenticationEntryPoint implements AuthenticationEntryPoint {

  

    @Override

    public void commence(HttpServletRequest request, HttpServletResponse response,

                         AuthenticationException authException) throws IOException, ServletException {

        response.setContentType("application/json;charset=UTF-8");

         

         //按照系统自定义结构返回授权失败

response.getWriter().write(JSON.toJSONString(APIResultDto.failed(APIErrorCode.AUTH_FAILED)));

    }

}

@Component

public class CustomAccessDeniedHandler implements AccessDeniedHandler {

  

    @Override

    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {

        response.setContentType("application/json;charset=UTF-8");

         

          //按照系统自定义结构返回授权失败

 response.getWriter().write(JSON.toJSONString(APIResultDto.failed(APIErrorCode.AUTH_FAILED)));

    }

}

关于状态码401与403区别

401 表示用户没有权限(令牌,用户名,密码错误)

403 表示用户有权限,只是访问是被禁止的(可以理解为,用户有权限,但是某些目录禁止访问)


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 962浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
我是一名在上海一家互联网公司上班,专注技术开发工作等。
小鸟云服务器
扫码进入手机网页