07 登陆拦截器
3)、登陆
开发期间模板引擎页面修改以后,要实时生效
1)、禁用模板引擎的缓存
# 禁用缓存
spring.thymeleaf.cache=false
2)、页面修改完成以后ctrl+f9:重新编译;
登陆错误消息的显示
4)、拦截器进行登陆检查
拦截器
/**
* 登陆检查,
*/publicclassLoginHandlerInterceptorimplementsHandlerInterceptor{//目标方法执行之前@OverridepublicbooleanpreHandle(HttpServletRequest
request, HttpServletResponse response, Object handler)throwsException{
Object user =
request.getSession().getAttribute("loginUser");if(user
==null){//未登陆,返回登陆页面request.setAttribute("msg","没有权限请先登陆");
request.getRequestDispatcher("/index.html").forward(request,response);returnfalse;
}else{//已登陆,放行请求returntrue; }
}@OverridepublicvoidpostHandle(HttpServletRequest request,
HttpServletResponse response, Object handler, ModelAndView
modelAndView)throwsException{
}@OverridepublicvoidafterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception
ex)throwsException{ }}
注册拦截器
//所有的WebMvcConfigurerAdapter组件都会一起起作用@Bean//将组件注册在容器publicWebMvcConfigurerAdapterwebMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter =newWebMvcConfigurerAdapter()
{@OverridepublicvoidaddViewControllers(ViewControllerRegistry
registry){
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}//注册拦截器@OverridepublicvoidaddInterceptors(InterceptorRegistry
registry){//super.addInterceptors(registry);//静态资源; *.css ,
*.js//SpringBoot已经做好了静态资源映射registry.addInterceptor(newLoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login"); }
};returnadapter; }