1. 程式人生 > >springboot攔截器中獲取配置文件值

springboot攔截器中獲取配置文件值

main ica res 文件 implement slf4j obj factor transacti

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攔截器中獲取配置文件值