如何在Spring Boot中以Spring Security级别启用CORS

如何在Spring Boot中以Spring Security级别启用CORS,第1张

如何在Spring Boot中以Spring Security级别启用CORS

这是一种使Spring Security 4.1通过Spring BOOT 1.5支持CROS的方法

  @Configurationpublic class WebConfig extends WebMvcConfigurerAdapter {    @Override    public void addCorsMappings(CorsRegistry registry) {        registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");    }}

@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {//        http.csrf().disable();        http.cors();    }    @Bean    public CorsConfigurationSource corsConfigurationSource() {        final CorsConfiguration configuration = new CorsConfiguration();        configuration.setAllowedOrigins(ImmutableList.of("*"));        configuration.setAllowedMethods(ImmutableList.of("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"));        configuration.setAllowCredentials(true);        configuration.setAllowedHeaders(ImmutableList.of("Authorization", "Cache-Control", "Content-Type"));        final UrlbasedCorsConfigurationSource source = new UrlbasedCorsConfigurationSource();        source.registerCorsConfiguration("/**", configuration);        return source;    }}


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

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

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存