前言
假设客户端在http请求中,已经加入了Header的认证信息,例如:
HttpPost post = new HttpPost("http://localhost:8990/sendMail");
StringEntity entity = new StringEntity(json, "utf-8");
entity.setContentType("application/json");
post.setEntity(entity);
// 设置验证头信息
post.addHeader("token", "WEFGYHJIKLTY4RE6DF29HNBCFD13ER87");
那么服务端怎么通过Filter,来验证客户端的token是否有效了?请接着往下看。
一、实现自定义Filter
1、实现Filter接口
我们要自定义Filter,只需实现Filter接口即可
2、覆写doFilter方法
根据业务逻辑,来覆写doFilter方法
示例如下:
@Slf4j
@Component
@WebFilter(urlPatterns={"/sendMail/*"}, filterName="tokenAuthorFilter")
public class TokenAuthorFilter implements Filter {
@Autowired
private AuthorizationRepository repository;
@Override