1.创建拦截器
1.1 创建一个拦截器类,实现Spring的HandlerInterceptor接口
1.2 在preHandle方法里编写允许跨域业务代码
1.3 在此拦截器类上添加@Component注解,代码如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class Interceptor implements HandlerInterceptor {
public static Logger log = LoggerFactory.getLogger(Interceptor.class);
// 在Controller方法执行前被执行
// 登入拦截、权限认证等等
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
log.info("preHandle");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type,Access-Token");
response.setHeader("Access-Control-Expose-Headers", "*");
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
String requestURI = request.getRequestURI();
log.info("请求地址:"+requestURI);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
throws Exception {
log.info("afterCompletion");
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
log.info("postHandle");
}
}
2.创建一个配置类
2.1 创建一个配置类,继承Spring的WebMvcConfigurerAdapter类,并实现ApplicationContextAware接口
2.2 在addInterceptors方法里的注册器里添加刚刚创建的拦截器类
2.3 .在此配置类上添加@Configuration注解,代码如下:
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration(value = "webConfig")
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
public WebConfig(){
super();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor());//添加拦截器
super.addInterceptors(registry);
}
//实例化拦截器
@Bean
public Interceptor interceptor(){
return new Interceptor();
}
}