zoukankan      html  css  js  c++  java
  • springboot项目配置拦截器,进行登陆等拦截

    新建拦截类:

    public class LoginInterceptor implements HandlerInterceptor{
    	private static Log logger = LogFactory.getLog(LoginInterceptor.class);
    	//handle 前
    	@Override
    	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    			throws Exception {
    		
    		SysUser user  = (SysUser) request.getSession().getAttribute("user");
    		logger.info("***********************执行了拦截器*************************");
    		if(user==null) {
                  //未登陆向前端返回数据 JsonResult jsonResult = new JsonResult("未登陆"); jsonResult.setFlag(0); String jsonString = JSONObject.toJSONString(jsonResult); PrintWriter writer = null; response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=utf-8"); try { writer = response.getWriter(); writer.print(jsonString); } catch (IOException e) { logger.info("未登陆",e); } finally { if (writer != null) writer.close(); } return false; }else { return true; } }     
        //post 时
    @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub }
        //controller 后 @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }

      

    新建配置类:

    对拦截器进行配置 因为springboot本来就是很大程度降低配置的繁琐,所以我觉得再去写配置文件去控制拦截器是否开启就没必要,就在类里面配了一个变量去控制是否开启。

    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    @Configuration
    public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
    	//tag 为true 则为开启状态,为false 则为关闭状态
    	private boolean tag=false;
    
    	@Override
    	public void addInterceptors(InterceptorRegistry registry) {
    		
    		// addPathPatterns 用于添加拦截规则
            // excludePathPatterns 配置某些路径不拦截
    		if(tag==true) {
    			InterceptorRegistration addInterceptor  = registry.addInterceptor(new LoginInterceptor());
    			addInterceptor.excludePathPatterns("/test");
    			addInterceptor.addPathPatterns("/**");
    		}else {
    			
    		}
    		
            //如果有多个拦截器
            //InterceptorRegistration addInterceptor2  = registry.addInterceptor(new MyInterceptor2());
    		
    		super.addInterceptors(registry);
    	}
    }
    

      

  • 相关阅读:
    Ubuntu 安装 NTP 服务
    Packer 如何将 JSON 的配置升级为 HCL2
    WinRM 如何设置 TrustedHosts
    Windows 10 如何设置网络属性为私有
    Windows 使用 PowerShell 来管理另外一台 Windows 机器
    Windows PowerShell ISE 是什么和 PowerShell 有什么区别
    Spring事务传播属性和隔离级别
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})注解作用
    杂文 | 如何在演讲中讲个好故事
    2.2 思考框架:什么样的代码才是高效的代码
  • 原文地址:https://www.cnblogs.com/blogwangwang/p/10684661.html
Copyright © 2011-2022 走看看