可以对URL路径进行拦截,可以用于权限验证、解决乱码、操作日志记录、性能监控、异常处理等。
SpringBoot拦截器实现登录拦截
pom.xml:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
com.wyj
springboot-interceptor01
0.0.1-SNAPSHOT
springboot-interceptor01
springboot拦截器
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-thymeleaf
springboot-interceptor01
org.springframework.boot
spring-boot-maven-plugin
WebMvcConfigurer:继承WebMvcConfigurationSupport类,重写addInterceptors方法
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
//
// registry.addInterceptor(new AuthorityInterceptor())
// .addPathPatterns("/user
// @Override
// public void addInterceptors(InterceptorRegistry registry) {
//
// registry.addInterceptor(new AuthorityInterceptor())
// .addPathPatterns("/user
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new AuthorityInterceptor())
.addPathPatterns("/user
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object object) throws Exception {
String uri = request.getRequestURI();
if (NOT_INTERCEPT_URI.contains(uri)) {
log.info("不拦截" + uri);
return true;
}
log.info("拦截" + uri);
HttpSession session = request.getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("user_info_in_the_session");
if (userInfo == null) {
throw new RuntimeException("用户未登陆");
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView mv) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception ex) throws Exception {
}
}
UserController:
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/login.html")
public String index() {
return "login";
}
@RequestMapping(value = "/login")
public String login(User user) {
//查询数据库,我这里直接写死
User dbUser = new User(1, "zhangsan", "123456", "admin");
if (dbUser.getPassword().equals(user.getPassword())) {
UserInfo userInfo = new UserInfo(dbUser.getId(), dbUser.getUsername(), dbUser.getRole());
HttpSession session = getRequest().getSession();
session.setAttribute("user_info_in_the_session", userInfo);
return "admin";
}
return "login";
}
@RequestMapping(value = "/userInfo")
@ResponseBody
public String userInfo() {
HttpSession session = getRequest().getSession();
UserInfo userInfo = (UserInfo) session.getAttribute("user_info_in_the_session");
return userInfo.toString();
}
private HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
}
}
User:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private int id;
private String username;
private String password;
private String role;
}
UserInfo: 用于存在用户信息储存在session中
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserInfo implements Serializable {
private int id;
private String username;
private String role;
}
login.html:只是一个很简单的登录表单
登陆页面
admin.html:
首页