第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

為 CORS 配置 Spring

為 CORS 配置 Spring

蕪湖不蕪 2022-05-12 18:42:55
我正在嘗試為 CORS 配置 Spring 以使用 Angular Web UI:我試過這個(gè):@Configuration@ComponentScan("org.datalis.admin.config")public class AppConfig {    @Bean    public static PropertySourcesPlaceholderConfigurer propertyConfigurer() {        PropertySourcesPlaceholderConfigurer conf = new PropertySourcesPlaceholderConfigurer();        conf.setLocation(new ClassPathResource("application.properties"));        return conf;    }    @Bean    public FilterRegistrationBean<CorsFilter> corsFilter() {        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        CorsConfiguration config = new CorsConfiguration();        config.setAllowCredentials(true);        config.addAllowedOrigin("127.0.0.1");        config.addAllowedHeader("*");        config.addAllowedMethod("*");        source.registerCorsConfiguration("/**", config);        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<CorsFilter>(new CorsFilter(source));        bean.setOrder(0);        return bean;    }}帶有 Angular FE 的 Apache 服務(wù)器與 Wildly 服務(wù)器在同一臺(tái)服務(wù)器上運(yùn)行,因此我為源配置了 127.0.0.1。但我仍然得到:Access to XMLHttpRequest at 'http://123.123.123.123:8080/api/oauth/token' from origin 'http://123.123.123.123' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.auth:1 Failed to load resource: the server responded with a status of 404 (Not Found)你知道我該如何解決這個(gè)問題嗎?
查看完整描述

3 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

您允許的來源是 127.0.0.1,但您的客戶端具有 ip 123.123.123.123。嘗試改變這一點(diǎn):

config.addAllowedOrigin("127.0.0.1");

對此:

config.addAllowedOrigin("123.123.123.123");


查看完整回答
反對 回復(fù) 2022-05-12
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

您需要告訴Spring Security使用您創(chuàng)建的 CORS 配置。


在我的項(xiàng)目中,我Spring Security以這種方式配置:


@Override

protected void configure(HttpSecurity http) throws Exception

{

    http

        .authorizeRequests()

        .antMatchers("/rest/protected/**")

        .authenticated()

     //Other spring sec configruation and then:

    .and()

        .cors()

        .configurationSource(corsConfigurationSource())


}

在哪里corsConfigurationSource():


@Bean

    CorsConfigurationSource corsConfigurationSource() {

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();


        boolean abilitaCors = new Boolean(env.getProperty("templating.oauth.enable.cors"));

        if( abilitaCors )

        {

            if( logger.isWarnEnabled() )

            {

                logger.warn("CORS ABILITATI! Si assume ambiente di sviluppo");

            }

            CorsConfiguration configuration = new CorsConfiguration();

            configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200","http://localhost:8080", "http://localhost:8180"));

            configuration.setAllowedMethods(Arrays.asList(  RequestMethod.GET.name(),

                    RequestMethod.POST.name(), 

                    RequestMethod.OPTIONS.name(), 

                    RequestMethod.DELETE.name(),

                    RequestMethod.PUT.name()));

            configuration.setExposedHeaders(Arrays.asList("x-auth-token", "x-requested-with", "x-xsrf-token"));

            configuration.setAllowedHeaders(Arrays.asList("X-Auth-Token","x-auth-token", "x-requested-with", "x-xsrf-token"));

            source.registerCorsConfiguration("/**", configuration);

        }

        return source;

    }


查看完整回答
反對 回復(fù) 2022-05-12
?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是我@Configuration處理僅在開發(fā)環(huán)境中使用的 CORS 請求的工作班。


@Configuration

//@Profile(PROFILE_DEV)

  public class CorsConfiguration {


  @Bean

  public WebMvcConfigurer corsConfigurer() {

      return new WebMvcConfigurer() {

          @Override

          public void addCorsMappings(CorsRegistry registry) {

              registry.addMapping("/**")

                  .allowedOrigins("*")

                  .allowedHeaders("*")

                  .allowedMethods("*");

          }

      };

  }

}

您還必須配置 Spring Security 以忽略HttpMethod.OPTIONS預(yù)檢請求使用的(作為您提到的例外)


@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)

public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  //...

    @Override

    public void configure(WebSecurity web) throws Exception {

      web.ignoring()

            //others if you need

            .antMatchers(HttpMethod.OPTIONS, "/**");


    }


    @Override

    public void configure(HttpSecurity http) throws Exception {

        http

            .csrf()

            .disable()

            .exceptionHandling()

            .and()

            .headers()

            .frameOptions()

            .disable()

            .and()

            .authorizeRequests()

            .antMatchers("/api/register").permitAll()

            .antMatchers("/api/activate").permitAll()

            .antMatchers("/api/authenticate").permitAll()

            .antMatchers("/api/**").authenticated();

    }


}

因?yàn)楫?dāng)您使用 cors 時(shí),您有觸發(fā)一個(gè)簡單請求和預(yù)檢請求HttpMethod.OPTIONS


查看完整回答
反對 回復(fù) 2022-05-12
  • 3 回答
  • 0 關(guān)注
  • 140 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)