添加CrossConfig
This commit is contained in:
parent
f96a2622d5
commit
ce954232b5
|
|
@ -1,21 +1,21 @@
|
|||
package com.gitlink.softbot.config;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@Component
|
||||
public class AdminInterceptor extends OncePerRequestFilter {
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
|
||||
httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
|
||||
httpServletResponse.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD,PATCH");
|
||||
filterChain.doFilter(httpServletRequest, httpServletResponse);
|
||||
}
|
||||
}
|
||||
//package com.gitlink.softbot.config;
|
||||
//
|
||||
//import org.springframework.stereotype.Component;
|
||||
//import org.springframework.web.filter.OncePerRequestFilter;
|
||||
//
|
||||
//import javax.servlet.FilterChain;
|
||||
//import javax.servlet.ServletException;
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//import javax.servlet.http.HttpServletResponse;
|
||||
//import java.io.IOException;
|
||||
//
|
||||
//@Component
|
||||
//public class AdminInterceptor extends OncePerRequestFilter {
|
||||
//
|
||||
// @Override
|
||||
// protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
|
||||
// httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
|
||||
// httpServletResponse.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT, HEAD,PATCH");
|
||||
// filterChain.doFilter(httpServletRequest, httpServletResponse);
|
||||
// }
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package com.gitlink.softbot.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
/**
|
||||
* ClassName CrossConfig
|
||||
*
|
||||
* @author ZengWei
|
||||
* Date 2021/7/15
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class CrossConfig {
|
||||
private CorsConfiguration buildConfig() {
|
||||
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
corsConfiguration.addAllowedOrigin("*");
|
||||
corsConfiguration.addAllowedHeader("*");
|
||||
corsConfiguration.addAllowedMethod("*");
|
||||
corsConfiguration.setMaxAge(3600L); // 预检请求的有效期,单位为秒。
|
||||
corsConfiguration.setAllowCredentials(true);// 是否支持安全证书(必需参数)
|
||||
return corsConfiguration;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", buildConfig());
|
||||
return new CorsFilter(source);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Loading…
Reference in New Issue