mirror of https://gitee.com/stylefeng/guns
【7.0.3】增加跨域拦截器
parent
6d6af15c06
commit
55c658b50f
|
@ -0,0 +1,36 @@
|
||||||
|
package cn.stylefeng.guns.config.web;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跨域拦截器
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/4/13 18:24
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class OptionsSecurityInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加跨域允许的header
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/4/13 18:24
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||||
|
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
|
||||||
|
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
|
||||||
|
response.setHeader("Access-Control-Max-Age", "3600");
|
||||||
|
response.setHeader("Access-Control-Allow-Headers", "Authorization");
|
||||||
|
response.setHeader("Access-Control-Allow-Credentials", "true");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -32,6 +32,9 @@ public class SpringMvcConfiguration implements WebMvcConfigurer {
|
||||||
@Resource
|
@Resource
|
||||||
private PermissionSecurityInterceptor permissionSecurityInterceptor;
|
private PermissionSecurityInterceptor permissionSecurityInterceptor;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private OptionsSecurityInterceptor optionsSecurityInterceptor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重写系统的默认错误提示
|
* 重写系统的默认错误提示
|
||||||
*
|
*
|
||||||
|
@ -78,6 +81,7 @@ public class SpringMvcConfiguration implements WebMvcConfigurer {
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
registry.addInterceptor(authJwtTokenSecurityInterceptor);
|
registry.addInterceptor(authJwtTokenSecurityInterceptor);
|
||||||
registry.addInterceptor(permissionSecurityInterceptor);
|
registry.addInterceptor(permissionSecurityInterceptor);
|
||||||
|
registry.addInterceptor(optionsSecurityInterceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue