写一个不用配置文件的springmvc拦截器,用于拦截所有/jftzc/*,拦截到的请求需从请求头中获取参数uid,当uid=12345时放行,否则返回401.
时间: 2024-06-01 21:11:14 浏览: 133
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;
}
}
相关问题
写一个springmvc拦截器,用于拦截所有/jftzc/*,拦截到的请求需从请求头中获取参数uid,当uid=12345时放行,否则返回401.
```
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String uri = request.getRequestURI();
if (uri.startsWith("/jftzc/")) {
String uid = request.getHeader("uid");
if ("12345".equals(uid)) {
return true;
} else {
response.setStatus(401);
return false;
}
}
return true;
}
}
```
需要在Spring MVC配置文件中注册该拦截器:
```
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/jftzc/**"/>
<bean class="com.example.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
```
阅读全文