spring boot实现过滤器和 * demo
作者:玲珑骰子安红豆 时间:2023-08-24 07:15:01
整理文档,搜刮出一个spring boot实现过滤器和 * demo ,稍微整理精简一下做下分享。
* 定义:
@WebServlet
public class ActionInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用(Controller方法调用之前)");
// 获取系统时间
Calendar ca = Calendar.getInstance();
int hour = ca.get(Calendar.HOUR_OF_DAY);
// 设置限制运行时间 0-4点
if (hour < 4) {
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet
// 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");
}
}
* 使用: 关于注解 我使用的是@Component 其实也可能声明成配置
@Component
public class ApplicationConfig {extends WebMvcConfigurerAdapter
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个 * 组成一个 * 链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(new ActionInterceptor()).addPathPatterns("/service/extract/json/**");
super.addInterceptors(registry);
}
}
过滤器:
定义:
public class ActionFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// 获取系统时间
Calendar ca = Calendar.getInstance();
int hour = ca.get(Calendar.HOUR_OF_DAY);
// 设置限制运行时间 0-4点
if (hour < 4) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setCharacterEncoding("UTF-8");
httpResponse.setContentType("application/json; charset=utf-8");
// 消息
Map<String, Object> messageMap = new HashMap<>();
messageMap.put("status", "1");
messageMap.put("message", "此接口可以请求时间为:0-4点");
ObjectMapper objectMapper=new ObjectMapper();
String writeValueAsString = objectMapper.writeValueAsString(messageMap);
response.getWriter().write(writeValueAsString);
} else {
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
}
}
使用:
@Component
public class ApplicationConfig {
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
ActionFilter actionFilter = new ActionFilter();
registrationBean.setFilter(actionFilter);
List<String> urlPatterns = new ArrayList<String>();
urlPatterns.add("/service/extract/json/*");
registrationBean.setUrlPatterns(urlPatterns);
return registrationBean;
}
}
来源:http://www.cnblogs.com/erbiaoge/p/6422414.html
标签:spring,boot
0
投稿
猜你喜欢
Java中TypeReference用法详情说明
2023-11-24 19:05:05
C#实现冒泡排序和插入排序算法
2021-07-18 17:01:53
java实现把对象数组通过excel方式导出的功能
2022-07-01 13:20:09
Android中View跟随手指移动效果
2021-07-26 00:21:18
Dubbo实现分布式日志链路追踪
2023-08-23 21:00:54
Intellij IDEA创建spring-boot项目的图文教程
2022-06-15 01:09:59
Android布局中gravity与layout_gravity属性说明
2021-08-27 10:51:31
java设计模式之工厂方法模式
2022-08-29 05:14:10
android实现九宫格程序
2023-02-16 16:30:53
Kotlin协程flowOn与线程切换超详细示例介绍
2022-11-06 08:31:51
linphone-sdk-android版本号生成解析
2022-10-18 14:43:18
基于Java字符编码的使用详解
2023-02-26 23:13:01
java返回集合为null还是空集合及空集合的三种写法小结
2021-08-18 05:37:48
Java线程安全和锁Synchronized知识点详解
2023-01-23 03:59:38
Android组件TabHost实现页面中多个选项卡切换效果
2023-03-05 22:07:35
Mybatis环境配置及测试详解
2023-11-24 07:06:25
Android控件之GridView用法实例分析
2022-03-02 00:01:34
SpringBoot @PostConstruct原理用法解析
2022-02-02 18:05:46
Java编程技巧:if-else优化实践总结归纳
2022-04-14 09:04:20
Java中BufferedReader和BufferedWriter使用方式
2022-01-06 15:06:05