shiro與spring?security用自定義異常處理401錯(cuò)誤
shiro與spring security自定義異常處理401
背景
現(xiàn)在是前后端分離的時(shí)代,后端必然要統(tǒng)一處理返回結(jié)果,比如定義一個(gè)返回對(duì)象
public class ResponseData<T> { /** * 統(tǒng)一返回碼 */ public String rtnCode; /** * 統(tǒng)一錯(cuò)誤消息 */ public String rtnMsg; /** * 結(jié)果對(duì)象 */ public T rtnData;
對(duì)于所有異常都有對(duì)應(yīng)的rtnCode對(duì)應(yīng),而不需要框架默認(rèn)處理如返回
這時(shí)候前端同學(xué)就不開心了,都已經(jīng)有rtnCode了,為啥http的status還要弄個(gè)401而不是200。
解決方案
一般的業(yè)務(wù)異常在springboot項(xiàng)目中新建一個(gè)統(tǒng)一處理類去處理即可,如
@ControllerAdvice public class DefaultExceptionHandler { /** * 異常統(tǒng)一處理 */ @ExceptionHandler({Exception.class}) @ResponseStatus(HttpStatus.OK) @ResponseBody public ResponseData allException(Exception e) {
大部分情況都能捕獲到從而如期返回json對(duì)象數(shù)據(jù),但是某些權(quán)限框架拋出的異常如401等等,不會(huì)被攔截到,這時(shí)候就需要再建一個(gè)類去處理這種情況,代碼如下
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; /** * spring security 異常處理 */ @RestController public 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報(bào)錯(cuò)
No SecurityManager accessible to the calling code...
最近在用springboot整合shiro,在訪問時(shí)出現(xiàn)了No SecurityManager accessible to the calling code…
報(bào)錯(cuò):
產(chǎn)生原因
自定義的SysUserFilter加載順序在ShiroFilter之前,導(dǎo)致出現(xiàn)No SecurityManager accessible to the calling code…
解決辦法
shiroFilter()的加載先于自定義的SysUserFilter
小結(jié)一下
出現(xiàn)No SecurityManager accessible to the calling code…問題的原因可能有很多,而我這個(gè)是因?yàn)閷⒆远x的Filter在ShiroFitler之前加載。
ShiroFilter 是整個(gè) Shiro 的入口點(diǎn),用于攔截需要安全控制的請(qǐng)求進(jìn)行處理,當(dāng)自定義的filter先于shiroFilter加載,而shiroFilter里又使用到該自定義filter時(shí),就會(huì)導(dǎo)致調(diào)用該自定義filter進(jìn)行預(yù)處理時(shí)訪問不到SecurityManager,也就是文中所出現(xiàn)的錯(cuò)誤。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis-plus配置日志兩種實(shí)現(xiàn)方式
這篇文章主要給大家介紹了關(guān)于mybatis-plus配置日志兩種實(shí)現(xiàn)方式的相關(guān)資料,Mybatis-plus集成了日志框架,可以將程序運(yùn)行時(shí)產(chǎn)生的日志進(jìn)行記錄,方便開發(fā)人員進(jìn)行問題排查,需要的朋友可以參考下2023-09-09SpringCloud?Feign使用ApacheHttpClient代替默認(rèn)client方式
這篇文章主要介紹了SpringCloud?Feign使用ApacheHttpClient代替默認(rèn)client方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03