最近在使用springboot时,前几天正常访问的页面,突然间访问不到js、css、图片等静态资源。经过排查发现是由于我发现程序中提示webmvcconfigureradapter已过期,所以改成了WebMvcConfigurationSupport
而WebMvcConfigurationSupport在 springboot的web自动配置类 WebMvcAutoConfiguration 上有条件注解,这个注解的意思是在项目类路径中缺少 WebMvcConfigurationSupport类型的bean时改自动配置类才会生效
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
所以当我们使用拦截器并继承WebMvcConfigurationSupport这个类的时候需要重写addInterceptors这个方法
所以拦截器的配置应该如下
代码1:
package com.example.syscloud.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebSecurityConfig extends WebMvcConfigurationSupport {
@Bean
public SessionInterceptor getSessionInterceptor() {
return new SessionInterceptor();
}
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
/*调用我们创建的SessionInterceptor。
* addPathPatterns("/**)的意思是这个链接下的都要进入到SessionInterceptor里面去执行
* excludePathPatterns("/login")的意思是login的url可以不用进入到SessionInterceptor中,直接
* 放过执行。
* 注意:如果像注释那样写是不可以的。这样等于是创建了多个Interceptor。而不是只有一个Interceptor
*
* */
SessionInterceptor sessionInterceptor=new SessionInterceptor();
registry.addInterceptor(sessionInterceptor).addPathPatterns("/**")
.excludePathPatterns("/user/login","/userAPI/login","/user/defaultKaptcha","/publicPage/login","/css/**","/js/**","/img/**","/static/**","/mapper/**");
super.addInterceptors(registry);
}
}
代码2:
package com.example.syscloud.Interceptor;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.syscloud.bean.templeBean.UserTokenBean;
import com.example.syscloud.token.TokenUse;
import com.example.syscloud.unit.ReturnCode;
import com.example.syscloud.unit.ReturnData;
import com.example.syscloud.unit.UserTokenMap;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
public class SessionInterceptor implements HandlerInterceptor {
/*在执行Controller的任务之前判断是否有Session信息
如果有Session信息就往下执行,去调用Controller。
如果没有Session就跳转到登录页面
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
response.setCharacterEncoding("utf-8");
String token=request.getHeader("token");
String android=request.getHeader("android");
if (token!=null){
boolean bo= TokenUse.tokenVerify(token);
int userid=TokenUse.getUserID(token);
UserTokenBean userTokenBean =UserTokenMap.getMap(userid);
if (userTokenBean!=null) {
if (userTokenBean.getToken().equals(token) && bo) {
Map<Integer,UserTokenBean> map=new HashMap<>();
map=UserTokenMap.getMap();
map.put(userid,userTokenBean);
UserTokenMap.setMap(map);
return true;
}
}
}
HttpSession session=request.getSession();
if(session.getAttribute("LoginName")!=null){
return true;
}
if(android!=null){
ReturnData returnData=new ReturnData();
returnData.setCode(ReturnCode.Timeout.value());
returnData.setMessage(ReturnCode.Timeout.getDesc());
String str=JSON.toJSONString(returnData);
JSONObject jsonObject=JSON.parseObject(str);
response.getWriter().print(jsonObject);
}else {
//拦截后返回的页面
String url = "/publicPage/login";
// response.sendRedirect(url);
response.getWriter().print("<script> top.window.location.href = '"+url+"?r='+Math.random() ;</script>");
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
第二段代码是通过验证session与token实现拦截与放行功能,我写一起了,可根据需要删除部分内容