springmvc的拦截器,怎么设置不拦截的url

2024-10-31 10:16:25
有1个网友回答
网友(1):

代码示例

// 不需要拦截的URL
private static final String[] EXCLUDED_URL = {"/login.html", "/doLogin.html", "/register.html", "/doRegister.html"};

/**
* 请求之前的处理方法

*
* @param request 请求request
* @param response 响应response
* @param handler 处理器
* @return 处理的结果
* @throws Exception 抛出的异常
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
boolean flag = false;
String requestedUrl = request.getRequestURI().toString();
// 检查请求的URL是否在过滤列表里
for (String url : EXCLUDED_URL) {
if (requestedUrl.contains(url)) {
flag = true;
break;
}
}

if (!flag) {
User user = (User) request.getSession().getAttribute(Constants.USER_CONTEXT);
if (user != null) {
flag = true;
} else {
throw new AuthorizationException();
}
}
return flag;
}