方式一:在ShiroConfig中注入SimpleMappingExceptionResolver类,然后设置相关属性
/**
* 当没有权限时跳转到403,配置异常后跳转的页面
* @return
*/
@Bean
public SimpleMappingExceptionResolver simpleMappingExceptionResolver(){
SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
properties.setProperty("UnauthorizedException","/403.html");
simpleMappingExceptionResolver.setExceptionMappings(properties);
return simpleMappingExceptionResolver;
}
方式二:用spring mvc的统一异常处理类HandlerExceptionResolver,然后判断UnauthorizedException异常即可
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
if (ex instanceof UnauthorizedException){
ModelAndView mv = new ModelAndView("/system/index/login.html");
return mv;
}
return null;
}
}
然后,在启动类中注册该bean
@SpringBootApplication
public class WarehouseApplication {
public static void main(String[] args) {
SpringApplication.run(WarehouseApplication.class, args);
}
@Bean
public MyExceptionResolver myExceptionResolver(){
return new MyExceptionResolver();
}
}