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