smalllove 2020-03-27
顺序: Filter-->Servlet-->Interceptor-->Controller
何时使用拦截器?何时使用过滤器?
两种方式:
1、使用spring boot提供的FilterRegistrationBean注册Filter
2、使用原生servlet注解定义Filter
两种方式的本质都是一样的,都是去FilterRegistrationBean注册自定义Filter
package com.theeternity.common.baseFilter; import javax.servlet.Filter; /** * @program: ApiBoot * @description: 封装Filter * @author: TheEternity Zhang * @create: 2019-02-17 13:08 */ public interface MappingFilter extends Filter { String[] addUrlPatterns(); int order(); }
package com.theeternity.beans.filterConfig; import com.theeternity.common.baseFilter.MappingFilter; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.*; import javax.servlet.FilterConfig; import java.io.IOException; /** * @program: ApiBoot * @description: 权限过滤器 * @author: TheEternity Zhang * @create: 2019-02-17 13:14 */ public class AuthFilter implements MappingFilter { @Override public String[] addUrlPatterns() { return new String[]{"/*"}; } @Override public int order() { return 0; } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
package com.theeternity.beans.filterConfig; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @program: ApiBoot * @description: 注册过滤器 * @author: TheEternity Zhang * @create: 2019-02-17 13:10 */ @Configuration public class FilterConfig { @Bean public FilterRegistrationBean registFilter() { FilterRegistrationBean registration = new FilterRegistrationBean(); AuthFilter authFilter=new AuthFilter(); registration.setFilter(authFilter); registration.addUrlPatterns(authFilter.addUrlPatterns()); registration.setOrder(authFilter.order()); registration.setName("AuthFilter"); return registration; } }
package com.theeternity.common.baseInterceptor; import org.springframework.web.servlet.HandlerInterceptor; /** * @program: ApiBoot * @description: 封装Interceptor * @author: TheEternity Zhang * @create: 2019-02-15 17:49 */ public interface MappingInterceptor extends HandlerInterceptor { String[] addPathPatterns(); String[] excludePathPatterns(); int order(); }
package com.theeternity.beans.interceptorConfig; import com.theeternity.common.baseInterceptor.MappingInterceptor; import org.springframework.stereotype.Component; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @program: BoxApi * @description: 跨域拦截器 * @author: tonyzhang * @create: 2018-12-21 14:44 */ @Component public class CrossOriginInterceptor implements MappingInterceptor { @Override public String[] addPathPatterns() { return new String[]{"/**"}; } @Override public String[] excludePathPatterns() { return new String[0]; } @Override public int order() { return 0; } @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.info("允许的头信息"+request.getHeader("Origin")); response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "*"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); //是否允许浏览器携带用户身份信息(cookie) response.setHeader("Access-Control-Allow-Credentials","true"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,Exception ex) throws Exception { } }
package com.theeternity.beans.interceptorConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @program: ApiBoot * @description: 拦截器注册 * @author: TheEternity Zhang * @create: 2019-02-15 17:55 */ @Configuration public class InterceptorConfig implements WebMvcConfigurer { @Autowired private CrossOriginInterceptor crossOriginInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(crossOriginInterceptor).addPathPatterns(crossOriginInterceptor.addPathPatterns()); } }
https://blog.csdn.net/heweimingming/article/details/79993591