springboot关于拦截器强转类型错误
程序员文章站
2022-07-09 18:18:58
...
springboot关于拦截器强转类型错误
interceptor
springboot关于拦截器 代码片
.
// An highlighted block
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod;
if (errorCodeList.contains(response.getStatus())){
/*request.getRequestDispatcher("/blog/error/"+response.getStatus()).forward(request,response);*/
response.sendRedirect("/blog/error/"+response.getStatus());
return false;
}else {
if(handler instanceof ResourceHttpRequestHandler) {
}else if(handler instanceof HandlerMethod){
handlerMethod=(HandlerMethod)handler;
Users user= (Users) request.getSession().getAttribute("loginUser");
if ("/blog/user/login".equals(request.getRequestURI())){
return true;
}else {
if (user != null) {
String methodName = handlerMethod.getMethod().getName();
String className = handlerMethod.getMethod().getDeclaringClass().getName();
List<AccModuleMore> accModuleMores = accModuleMoreMapper.selectAccModuleMoreByUserId(user.getId());
for (AccModuleMore accModuleMore : accModuleMores) {
if (accModuleMore.getModule().getClassname().contains(className)) {
logger.info("class:" + accModuleMore.getModule().getClassname());
logger.info("找到相同类");
if (accModuleMore.getModule().getMoethod().contains(methodName)) {
logger.info("method:" + accModuleMore.getModule().getMoethod());
logger.info("找到方法了");
return true;
}
}
}
logger.info("没有该权限");
request.setAttribute("msg", "操作失败,没有权限!");
request.getRequestDispatcher("/main.html").forward(request, response);
return false;
}
}
}
return true;
}
}
免费云服务器【三丰云】https://www.sanfengyun.com