Spring Boot CORS过滤器-CORS预检通道未成功

Spring Boot CORS过滤器-CORS预检通道未成功,第1张

Spring Boot CORS过滤器-CORS预检通道未成功

我已经通过创建新的CORS过滤器解决了此问题:

@Componentpublic class CorsFilter extends oncePerRequestFilter {    @Override    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {        response.setHeader("Access-Control-Allow-Origin", "*");        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");        response.setHeader("Access-Control-Max-Age", "3600");        response.setHeader("Access-Control-Allow-Headers", "authorization, content-type, xsrf-token");        response.addHeader("Access-Control-Expose-Headers", "xsrf-token");        if ("OPTIONS".equals(request.getMethod())) { response.setStatus(HttpServletResponse.SC_OK);        } else {  filterChain.doFilter(request, response);        }    }}

并将其添加到安全配置中:

.addFilterBefore(new CorsFilter(), ChannelProcessingFilter.class)

更新-如今,我使用了更现代的方式切换到:

@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http .cors()        .and()        ...    }    @Bean    public CorsConfigurationSource corsConfigurationSource() {        CorsConfiguration configuration = new CorsConfiguration();        configuration.setAllowedOrigins(Arrays.asList("*"));        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));        configuration.setExposedHeaders(Arrays.asList("x-auth-token"));        UrlbasedCorsConfigurationSource source = new UrlbasedCorsConfigurationSource();        source.registerCorsConfiguration("/**", configuration);        return source;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5017751.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存