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

配置文件中的配置:

最新文章

  1. Sudoku 数独游戏
  2. Bootstrap Chart组件使用分享
  3. MySQL数据库中字符集的问题
  4. 关闭log4j 输出 DEBUG org.apache.commons.beanutils.*
  5. Eclipse调试按钮消失问题
  6. C语言 动态库简单开发
  7. 【C语言学习】-05 二维数组、字符串数组、多维数组
  8. PHPExcel读取excel的多个sheet存入数据库
  9. 如何使用CSS实现小三角形效果
  10. Linux中kettle自动化部署脚本
  11. Python之路-Linux命令基础(1)
  12. 转载-Oracle ORACLE的sign函数和DECODE函数
  13. 芝麻HTTP:Ajax结果提取
  14. Swift基础之实现下拉变大和OC下拉变大上拉缩小Demo
  15. zepto的extend
  16. JS_高程6.面向对象的程序设计(1)理解对象
  17. jmeter如何玩?
  18. android JNI调用(Android Studio 3.0.1)(转)
  19. WPF StoryBoard用法
  20. web测试通用要点大全(Web Application Testing Checklist)

热门文章

  1. IOS 静态库 和 动态库
  2. 创建表空间、新增用户、给用户赋予DBA权限 、删除用户下的上有数据表
  3. Selenium WebDriver中鼠标事件
  4. xampp 安装以及相关问题
  5. 【python】控制台输出颜色
  6. tail -f -n 0 /var/log/messages
  7. 利用vue-cli创建新项目
  8. spring cloud 声明式rest客户端feign调用远程http服务
  9. 即时通讯协议之XMPP
  10. 计蒜客31452 Supreme Number(找规律)