赞
踩
package com.demo.bootweb.interceptor; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; import org.springframework.web.servlet.AsyncHandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Component public class DemoInterceptor implements AsyncHandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String uri = request.getServletPath(); //拦截器可以用于日志记录、监控、检查权限、以及存储请求头一些公用的信息等等 //这里只是一个测试。拦截pig请求并转发到dog下 if("/hello/pig".equals(uri)) { request.getRequestDispatcher("/hello/dog").forward(request, response); return false; } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { //这里可以解决一些跨域问题 response.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers")); response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH"); response.setHeader("Access-Control-Allow-Origin", "*"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { } }
package com.demo.bootweb.config; import com.demo.bootweb.interceptor.DemoInterceptor; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import javax.annotation.Resource; import java.nio.charset.StandardCharsets; @Configuration @EnableWebMvc public class WebMvcConfigureAdapter implements WebMvcConfigurer { @Resource private DemoInterceptor demoInterceptor; /** * 配置访问拦截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(demoInterceptor) //拦截路径设置 .addPathPatterns("/**") //不拦截的路径设置 .excludePathPatterns("/public/**") .excludePathPatterns("/error/**") .excludePathPatterns("/static/**") .excludePathPatterns("/logout") .excludePathPatterns("/login"); } /** * 配置访问静态资源 * 全部交由拦截器控制 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry){ registry.addResourceHandler("/**"); registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } @Bean public FilterRegistrationBean<CharacterEncodingFilter> filterRegistrationBean() { FilterRegistrationBean<CharacterEncodingFilter> registrationBean = new FilterRegistrationBean<>(); CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setForceEncoding(true); characterEncodingFilter.setEncoding("UTF-8"); registrationBean.setFilter(characterEncodingFilter); return registrationBean; } @Bean public HttpMessageConverter<String> responseBodyConverter() { return new StringHttpMessageConverter(StandardCharsets.UTF_8); } }
如图,访问pig被成功拦截到了dog。
晚安!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。