F12看看你的请求要访问哪些静态资源,然后用/**/*.xx 的格式写即可
如果拦截器已经写好了,就不用看下面这个拦截器代码了
@Component
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {
@Value("${delivery.auth.activate}")
private boolean activate;
@Autowired
private HttpSession sess;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
if (request.getSession().getAttribute("userId") == null) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try (PrintWriter out = response.getWriter()) {
Message msg = Message.fail("用户未登录");
out.append(JSON.toJSONString(msg));
return false;
} catch (Exception e) {
log.error(e.getMessage());
response.sendError(500);
return false;
}
}
return true;
}
}
配置类中加上url规则
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private AuthInterceptor interceptor;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor)
.excludePathPatterns("/user/**", "/**/*.html", "/**/*.css", "/**/*.js", "/**/*.woff", "/custom/**");
}
}
|