1. springmvc进行错误跳转配置如下
<!-- shiro为集成springMvc 拦截异常 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<!-- 这里你可以根据需要定义N多个错误异常转发 -->
<prop key="org.apache.shiro.authz.AuthorizationException">redirect:/shiro/401</prop>
</props>
</property>
</bean>
2.controller的写法
@RequestMapping("/401")
public String authorizationException(ModelMap modelMap, HttpServletRequest request) {
String requestType = request.getHeader("X-Requested-With");
// ajax 请求
if (requestType != null && requestType.equals("XMLHttpRequest")) {
return "redirect:/shiro/asyn401";
} else {
return "redirect:/shiro/syn401";
}
}
// 异步
@RequestMapping("/asyn401")
public void asyn401(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
ResultDto resultDto = new ResultDto();
resultDto.setResult(ResultDto.resultCode_401);
try {
super.writeJSON(response, resultDto);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 同步
@RequestMapping("/syn401")
public String syn401(ModelMap modelMap, HttpServletRequest request) {
return "/shiro/401";
}