SpringBoot2.X配置登录拦截器及常见错误

SpringBoot2.X配置登录拦截器

由于SpringBoot从1.X升级为2.X之后的WebMvcConfigurerAdapter已过时,所以SpingBoot2.X直接实现WebMvcConfigurer接口。

新建WebConfigurer类

import org.springframework.beans.factory.annotation.Autowired;
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.WebMvcConfigurer;

@Configuration
public class WebConfigurer implements WebMvcConfigurer {
 
  @Autowired
  private LoginInterceptor loginInterceptor;
 
 
  //配置放行静态资源
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
  //磁盘图片映射
   registry.addResourceHandler("/images/**").addResourceLocations("file:D:/images/");
   //静态资源映射
    registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
   
  }
  
  // 注册拦截器
  public void addInterceptors(InterceptorRegistry registry) {
	  
    // addPathPatterns("/**") 表示拦截所有的请求
    // excludePathPatterns("/login", "/register") 登陆与注册放行,对其不拦截
    //放行的接口及静态资源
    String[] excludes = new String[]{"/tologin","/toforgetpassword","/register","/static/**"};
    registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns(excludes);
   
  }
}

新建LoginInterceptor类

import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
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;
 

@Component
public class LoginInterceptor implements HandlerInterceptor {
	
    private HttpServletRequest servletRequest;

	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    	
    	HttpSession session = request.getSession();
    	//当账号密码正确时加入session,如果存在,说明有登录者信息,继续往下进行
       if (session.getAttribute("user")!=null) {
  			 return true;
    	 
        }else {
        //跳转至指定界面,这里测试跳转至百度
        	response.sendRedirect("http://www.baidu.com");
            return false;
        }     
     
    } 
 
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
 
    }
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
 
    }

}

测试结果

对/tologin放行
SpringBoot2.X配置登录拦截器及常见错误_第1张图片
输入账号密码正确以后继续往下请求
SpringBoot2.X配置登录拦截器及常见错误_第2张图片
当访问/toindex,拦截器拦截,跳转至百度
SpringBoot2.X配置登录拦截器及常见错误_第3张图片
SpringBoot2.X配置登录拦截器及常见错误_第4张图片

拦截器不执行常见问题

1.检查是否加入@Component,@Configuration注解。
2.检查目录结构,其它层和启动类处于同一级或是启动类的下一级。
3.检查放行接口和controller对应。
SpringBoot2.X配置登录拦截器及常见错误_第5张图片

你可能感兴趣的:(spring,java)