广告
返回顶部
首页 > 资讯 > 精选 >shiro与spring security怎么用自定义异常处理401错误
  • 777
分享到

shiro与spring security怎么用自定义异常处理401错误

2023-06-21 20:06:16 777人浏览 独家记忆
摘要

这篇文章主要介绍shiro与spring security怎么用自定义异常处理401错误,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!shiro与spring security自定义异常处理401背景现在是

这篇文章主要介绍shirospring security怎么用自定义异常处理401错误,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

shiro与spring security自定义异常处理401

背景

现在是前后端分离的时代,后端必然要统一处理返回结果,比如定义一个返回对象

public class ResponseData<T> {        public String rtnCode;        public String rtnMsg;        public T rtnData;

对于所有异常都有对应的rtnCode对应,而不需要框架默认处理如返回

shiro与spring security怎么用自定义异常处理401错误

这时候前端同学就不开心了,都已经有rtnCode了,为啥Http的status还要弄个401而不是200。

解决方案

一般的业务异常在SpringBoot项目中新建一个统一处理类去处理即可,如

@ControllerAdvicepublic class DefaultExceptionHandler {        @ExceptionHandler({Exception.class})    @ResponseStatus(httpstatus.OK)    @ResponseBody    public ResponseData allException(Exception e) {

大部分情况都能捕获到从而如期返回JSON对象数据,但是某些权限框架抛出的异常如401等等,不会被拦截到,这时候就需要再建一个类去处理这种情况,代码如下

package com;import com.vo.ResponseData;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.WEB.ErrorAttributes;import org.springframework.boot.autoconfigure.web.ErrorController;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Map;@RestControllerpublic class CustomErrorController implements ErrorController {private static final String PATH = "/error";    @Autowired    private ErrorAttributes errorAttributes;    @RequestMapping(value = PATH)    ResponseData error(HttpServletRequest request, HttpServletResponse response) {        // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring.         // Here we just define response body.    Map<String, Object> errORMap = getErrorAttributes(request);        ResponseData d= new ResponseData(response.getStatus()+"", errorMap.get("message").toString());        response.setStatus(HttpServletResponse.SC_OK);        return d;    }    @Override    public String getErrorPath() {        return PATH;    }    private Map<String, Object> getErrorAttributes(HttpServletRequest request) {        RequestAttributes requestAttributes = new ServletRequestAttributes(request);        return errorAttributes.getErrorAttributes(requestAttributes, false);    }}

SpringBoot整合Shiro自定义filter报错

No SecurityManager accessible to the calling code...

最近在用springboot整合shiro,在访问时出现了No SecurityManager accessible to the calling code…

报错:

shiro与spring security怎么用自定义异常处理401错误

产生原因

shiro与spring security怎么用自定义异常处理401错误

自定义的SysUserFilter加载顺序在ShiroFilter之前,导致出现No SecurityManager accessible to the calling code…

解决办法

shiro与spring security怎么用自定义异常处理401错误

shiroFilter()的加载先于自定义的SysUserFilter

以上是“shiro与spring security怎么用自定义异常处理401错误”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!

--结束END--

本文标题: shiro与spring security怎么用自定义异常处理401错误

本文链接: https://www.lsjlt.com/news/300441.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • shiro与spring security用自定义异常处理401错误
    目录shiro与spring security自定义异常处理401背景解决方案SpringBoot整合Shiro自定义filter报错No SecurityManager acces...
    99+
    2022-11-12
  • shiro与spring security怎么用自定义异常处理401错误
    这篇文章主要介绍shiro与spring security怎么用自定义异常处理401错误,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!shiro与spring security自定义异常处理401背景现在是...
    99+
    2023-06-21
  • 基于spring-security 401 403错误自定义处理方案
    spring-security 401 403错误自定义处理 为了返回给前端统一的数据格式, 一般所有的数据都会以类似下面的方式返回: public class APIResul...
    99+
    2022-11-12
  • 基于spring-security出现401 403错误自定义处理的示例分析
    这篇文章将为大家详细讲解有关基于spring-security出现401 403错误自定义处理的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。spring-security 401 403错误自定...
    99+
    2023-06-20
  • Spring Boot 中自定义异常怎么处理
    这篇文章将为大家详细讲解有关Spring Boot 中自定义异常怎么处理,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。在 Spring Boot 项目中 ,异常统一处理,可以使用 Spring 中 @Co...
    99+
    2023-06-02
  • Spring Cloud gateway自定义错误处理Handler怎么实现
    本文小编为大家详细介绍“Spring Cloud gateway自定义错误处理Handler怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“Spring Cloud gateway自定义错误处...
    99+
    2023-07-05
  • thinkphp5.1中怎么使用自定义异常处理类进行接管
    小编给大家分享一下thinkphp5.1中怎么使用自定义异常处理类进行接管,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!配置文件修改config/app.php自定义异常接管类出处'exception_handle...
    99+
    2023-06-20
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作