zoukankan      html  css  js  c++  java
  • springboot拦截器中获取配置文件值

    package com.zhx.web.interceptor;
    
    import com.zhx.util.other.IpUtil;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * @author SimonHu
     * @Description:
     * @Created on 2018/1/26 9:37
     */
    public class LoginInterceptor implements HandlerInterceptor {
        private Logger log = LoggerFactory.getLogger(String.valueOf(LoginInterceptor.class));
        @Value("${spring.profiles.active}")//取配置文件的值
        private  String environment;
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
            String ip = IpUtil.getIpAddr(request);
            log.info("###################IP访问来自{}#################运行环境{}",ip,environment);
            if (ip.equals("####.##.##.##") || "dev".equals(environment)){
                return true;
            }
            return false;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
            if(response.getStatus()==500){
                modelAndView.setViewName("error");
            }else if(response.getStatus()==404){
                modelAndView.setViewName("404");
            }
        }
    
        @Override
        public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    
        }
    }

    自定义拦截器:对指定ip进行限制

    package com.zhx;
    
    import com.zhx.config.ApiServerConfig;
    import com.zhx.config.SmsConfig;
    import com.zhx.web.interceptor.LoginInterceptor;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @EnableTransactionManagement
    @SpringBootApplication
    @EnableConfigurationProperties({SmsConfig.class,ApiServerConfig.class})
    public class Application extends WebMvcConfigurerAdapter {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
        /**
         * 配置拦截器
         * @param registry
         */
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            /**
             * 加入ip验证拦截器
             */
            registry.addInterceptor(loginInterceptor()).addPathPatterns("/**");
            super.addInterceptors(registry);
        }
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer(){
            return new EmbeddedServletContainerCustomizer() {
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    container.setSessionTimeout(1800);
                }
            };
        }
        @Bean //将自定义拦截器注册到spring bean中
        public  LoginInterceptor loginInterceptor(){
            return new LoginInterceptor();
        }
    
    
    }

    这里注册拦截器时要以bean的方式注册进去,这样启动时拦截器才能取到配置文件的值。

    配置文件中的配置:

  • 相关阅读:
    如何输出高精度时间差
    GetThreadTimes获取其它线程cpu时间
    12-Python基础之类与面向对象
    10-Python函数之递归
    09-Python基础之内置函数与匿名函数
    08-Python基础之迭代器与生成器
    07-Python基础之装饰器
    06-Python基础之函数进阶(嵌套,作用域)
    05-Python基础之函数基础
    04-Python基础之文件操作基础
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/8360701.html
Copyright © 2011-2022 走看看