利用springboot如何实现一个过滤器功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
拦截器定义:
@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; } }
看完上述内容,你们掌握利用springboot如何实现一个过滤器功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。