/**
* @Auther: King
* @Date: 2021/11/30 - 11 - 30 - 14:54
* @Description: com.jd.hilms.auth
* @version: 1.0
*/
@Component
public class IdentityInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(IdentityInterceptor.class);
private List<String> whiteList = Arrays.asList(
"放不拦截的链接"
);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
LOGGER.debug("进入拦截器IdentityInterceptor:{}", request.getServletPath());
String servletPath = request.getServletPath().replaceFirst("/", "");
LOGGER.debug("进入拦截器拦截的url", servletPath);
String bestMatchingPattern = request.getAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern").toString();
if (this.inWhiteList(servletPath, bestMatchingPattern)) {
return true;
}
String token = "这里放token";
if (token == null){
response.setContentType("application/json;charset=UTF-8");
response.setStatus(401);
response.getWriter().println("xxxxx");
}
return true;
}
public boolean inWhiteList(String servletPath, String bestMatchingPattern) {
return this.whiteList.contains(servletPath) || this.whiteList.contains(bestMatchingPattern.replaceFirst("/", ""));
}
}
Java三大器之拦截器(Interceptor)的代码示例
最新推荐文章于 2024-09-16 19:25:26 发布