码迷,mamicode.com
首页 > 编程语言 > 详细

springboot拦截器中获取配置文件值

时间:2018-01-26 20:39:43      阅读:931      评论:0      收藏:0      [点我收藏+]

标签:com   response   java   eem   beans   run   equals   pat   gis   

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的方式注册进去,这样启动时拦截器才能取到配置文件的值。

配置文件中的配置:

技术分享图片

springboot拦截器中获取配置文件值

标签:com   response   java   eem   beans   run   equals   pat   gis   

原文地址:https://www.cnblogs.com/SimonHu1993/p/8360701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!