写一个不用配置文件的springmvc拦截器,用于拦截所有/jftzc/*,拦截到的请求需从请求头中获取参数uid,当uid=12345时放行,否则返回401.
时间: 2024-06-01 11:11:14 浏览: 124
防止SpringMVC拦截器拦截js等静态资源文件的解决方法
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestUri = request.getRequestURI();
if (requestUri.startsWith("/jftzc/")) {
String uid = request.getHeader("uid");
if ("12345".equals(uid)) {
return true;
} else {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
return false;
}
}
return true;
}
}
阅读全文