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