當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringSecurity权限控制之异常处理方式三
生活随笔
收集整理的這篇文章主要介紹了
SpringSecurity权限控制之异常处理方式三
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
方式三:編寫異常處理器
攔截器和過(guò)濾器的區(qū)別
- 攔截器:可以在Spring中進(jìn)行使用
- 過(guò)濾器:只能在web.xml中進(jìn)行配置,也就是只能在web工程中使用
或者我們可以實(shí)現(xiàn)一個(gè)Spring給我們提供好的接口
@Component public class HandlerControllerException implements HandlerExceptionResolver {/*** @param httpServletRequest* @param httpServletResponse* @param o 出現(xiàn)異常的對(duì)象* @param e 出現(xiàn)的異常信息* @return ModelAndView*/@Overridepublic ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {ModelAndView mv = new ModelAndView();//將異常信息放入request域中,基本不用mv.addObject("errorMsg", e.getMessage());//指定不同異常跳轉(zhuǎn)的頁(yè)面if(e instanceof AccessDeniedException){mv.setViewName("redirect:/403.jsp");}else {mv.setViewName("redirect:/500.jsp");}return mv;} }下面一個(gè)更簡(jiǎn)單的方式,通過(guò)注解就相當(dāng)于我們實(shí)現(xiàn)了 HandlerExceptionResolver
@ControllerAdvice public class HandlerControllerAdvice{@ExceptionHandler(AccessDeniedException.class)public String handlerException(){return "redirect:/403.jsp";}@ExceptionHandler(RuntimeException.class)public String runtimeHandlerException(){return "redirect:/500.jsp";} }總結(jié)
以上是生活随笔為你收集整理的SpringSecurity权限控制之异常处理方式三的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringSecurity权限控制之异
- 下一篇: 集中式整合之加入springsecuri