写一个springmvc拦截器,用于拦截所有/jftzc/*,拦截到的请求需从请求头中获取参数uid,当uid=12345时放行,否则返回401.
时间: 2024-05-08 13:16:22 浏览: 130
```
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>
```
阅读全文