使用Sping Boot2.0 搭建权限拦截的时候 发现 与之前的版本不一样了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| ## 所有功能完成 配置登录认证
### 配置拦截器 ###### 在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截 - 新建包com.example.interceptor; - 创建login拦截类 ```java package com.example.interceptor;
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;
public class LoginInterceptor implements HandlerInterceptor {
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //请求进入这个拦截器 HttpSession session = request.getSession(); if(session.getAttribute("user") == null){ //判断session中有没有user信息 // System.out.println("进入拦截器"); if("XMLHttpRequest".equalsIgnoreCase(request.getHeader("X-Requested-With"))){ response.sendError(401); } response.sendRedirect("/"); //没有user信息的话进行路由重定向 return false; } return true; //有的话就继续操作 }
@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 {
} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.example;
import com.example.interceptor.LoginInterceptor; import com.example.interceptor.RightsInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*;
@Configuration public class WebAppConfigurer implements WebMvcConfigurer {
@Autowired RightsInterceptor rightsInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); registration.addPathPatterns("/**"); registration.excludePathPatterns("/","/login","/error","/static/**","/logout");
}
}
|
- 在WebAppConfigurer.java中增加内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.example;
import com.example.interceptor.LoginInterceptor; import com.example.interceptor.RightsInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.*;
@Configuration public class WebAppConfigurer implements WebMvcConfigurer {
@Autowired RightsInterceptor rightsInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor()); registration.addPathPatterns("/**"); registration.excludePathPatterns("/","/login","/error","/static/**","/logout");
InterceptorRegistration registration1 = registry.addInterceptor(rightsInterceptor); registration1.addPathPatterns("/**"); registration1.excludePathPatterns("/","/login","/error","/static/**","/logout"); }
}
|