mirror of https://gitee.com/stylefeng/guns
更新CORS过滤器实现
parent
8323365725
commit
5da3081451
|
@ -1,36 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -3,6 +3,7 @@ package cn.stylefeng.guns.config.web;
|
||||||
import cn.stylefeng.guns.core.error.CustomErrorAttributes;
|
import cn.stylefeng.guns.core.error.CustomErrorAttributes;
|
||||||
import cn.stylefeng.guns.core.security.AuthJwtTokenSecurityInterceptor;
|
import cn.stylefeng.guns.core.security.AuthJwtTokenSecurityInterceptor;
|
||||||
import cn.stylefeng.guns.core.security.PermissionSecurityInterceptor;
|
import cn.stylefeng.guns.core.security.PermissionSecurityInterceptor;
|
||||||
|
import cn.stylefeng.roses.kernel.security.cors.CorsFilterConfiguration;
|
||||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -21,7 +22,7 @@ import javax.annotation.Resource;
|
||||||
* @date 2020/4/11 10:23
|
* @date 2020/4/11 10:23
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import({cn.hutool.extra.spring.SpringUtil.class})
|
@Import({cn.hutool.extra.spring.SpringUtil.class, CorsFilterConfiguration.class})
|
||||||
public class SpringMvcConfiguration implements WebMvcConfigurer {
|
public class SpringMvcConfiguration implements WebMvcConfigurer {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
|
@ -30,9 +31,6 @@ public class SpringMvcConfiguration implements WebMvcConfigurer {
|
||||||
@Resource
|
@Resource
|
||||||
private PermissionSecurityInterceptor permissionSecurityInterceptor;
|
private PermissionSecurityInterceptor permissionSecurityInterceptor;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private OptionsSecurityInterceptor optionsSecurityInterceptor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重写系统的默认错误提示
|
* 重写系统的默认错误提示
|
||||||
*
|
*
|
||||||
|
@ -65,7 +63,6 @@ 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