1. 程式人生 > 遊戲 >由於過於“友好” 《堡壘之夜》熊抱動作被下架

由於過於“友好” 《堡壘之夜》熊抱動作被下架

原文地址:https://www.cnblogs.com/cchilei/p/13685606.html

方式一(精細配置)

在需要跨域的整個Controller或者單個方法上新增@CrossOrigin註解

方式二(全域性配置)

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .maxAge(3600)
                .allowCredentials(true);
    }
}

方式三(通過filter)

@Component
@WebFilter(urlPatterns = "/*", filterName = "authFilter") //這裡的“/*” 表示的是需要攔截的請求路徑
public class PassHttpFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException { }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;
        httpResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
        httpResponse.addHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8080");
        filterChain.doFilter(servletRequest, httpResponse);
    }
    @Override
    public void destroy() { }