zoukankan      html  css  js  c++  java
  • springboot拦截器

    1.spring boot拦截器默认有HandlerInterceptorAdapter、 AbstractHandlerMapping、UserRoleAuthorizationInterceptor、LocaleChangeInterceptor、ThemeChangeInterceptor 
    

     1、拦截器代码(需要springboot扫描到注解)

    package com.yb.fw.app.interceptor;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.yb.fw.core.helper.FwConstant;
    
    @Component
    public class AuthorityInterceptor implements HandlerInterceptor {
    
        //@Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                Object handler) throws Exception {        
            String url = request.getRequestURI();
    //        System.out.println("AuthorityInterceptor preHandle="+url);
            if(url.indexOf("/app/login.do")>=0) {
                return true;
            }else if(url.indexOf("/app/land.do")>=0) {
                return true;
            }else if(url.indexOf("/app/land2.do")>=0) {
                return true;
            }else if(url.indexOf("/app/check.do")>=0) {
                return true;
            }else if(url.indexOf("/app/checkpage.do")>=0) {
                return true;
            }else if(url.indexOf("/app/utility/getVerifyCode.do")>=0) {
                return true;
            }
            HttpSession session = request.getSession();
            if(session.getAttribute(FwConstant.SESSION_USERINFO) == null) {
                response.sendRedirect(request.getContextPath()+"/app/land.do");
                return false;
            }else return true;
        }
        
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response,
                Object handler, ModelAndView mv) throws Exception {
        }
        
        @Override
        public void afterCompletion(HttpServletRequest request,
                HttpServletResponse response, Object handler, Exception exception)
                throws Exception {
        }
    }

    2、拦截器配置类(需要springboot扫描到注解)

    package com.yb.fw.config;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.persistence.EntityManagerFactory;
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.orm.jpa.JpaTransactionManager;
    import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import org.springframework.web.servlet.DispatcherServlet;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import com.yb.fw.app.interceptor.AuthorityInterceptor;
    import com.yb.fw.app.interceptor.FrontAuthorityInterceptor;
    import com.yb.fw.app.interceptor.UserOpInterceptor;
    import com.yb.fw.core.helper.BaseRepositoryFactoryBean;
    
    @Configuration
    public class WebAppConfig extends WebMvcConfigurerAdapter{
    
    
        @Autowired
        UserOpInterceptor userOpInterceptor;
        
        @Autowired
        AuthorityInterceptor authorityInterceptor;
        
        @Autowired
        FrontAuthorityInterceptor frontAuthorityInterceptor;
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //多个拦截器组成一个拦截器链
            // addPathPatterns用于添加拦截规则
            // excludePathPatterns用户排除拦截
            registry.addInterceptor(frontAuthorityInterceptor).addPathPatterns("/store/**"); //根据这个判断它要不要进入自己定义的拦截器
            registry.addInterceptor(authorityInterceptor).addPathPatterns("/app/**"); //对来自/** 全路径请求进行拦截
           registry.addInterceptor(userOpInterceptor).addPathPatterns("/app/**"); //对来自/** 全路径请求进行拦截       
            super.addInterceptors(registry);
        }
        
    
    }
  • 相关阅读:
    Python爬取暴走漫画动态图
    ADB server didn't ACK 的解决方法
    安装APK时报Local path doesn't exist错误
    当Web Services遇到Android(初步接触时可能遇到的错误)
    eclipse启动时出现Incompatible JVM Version [###] of the JVM is not suitable for this product ...
    两个Activity之间的切换和响应
    关于不同Android手机适配的几个问题(转)
    ERROR: Unknown command 'crunch' 解决方法
    eclipse无法启动的常见原因
    获取手机屏幕的宽和高
  • 原文地址:https://www.cnblogs.com/zouhong/p/11652758.html
Copyright © 2011-2022 走看看