Настроить Spring для CORS - PullRequest
       8

Настроить Spring для CORS

0 голосов
/ 18 января 2019

Я пытаюсь настроить Spring для CORS, чтобы использовать веб-интерфейс Angular:

Я пробовал это:

@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;
    }
}

Сервер Apache с Angular FE работает с сервером Wildly на том же сервере, поэтому я настроил 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)

Знаете ли вы, как я могу решить эту проблему?

Второй способ, который я попробовал:

@Configuration
@EnableResourceServer
public class ResourceSecurityConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId("resource_id").stateless(true);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/users/**").permitAll()
                .anyRequest().authenticated()
                .and()
        .cors().disable()
        .authorizeRequests()
        .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .anyRequest()
        .fullyAuthenticated()
        .and()
        .httpBasic()
        .and()
        .csrf().disable();
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSources() {
        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;
    }
}

Со второй конфигурацией я получаю 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)

Как лучше всего достичь этого результата?

Ответы [ 6 ]

0 голосов
/ 01 июля 2019

Попробуйте изменить имя вашего компонента на corsConfigurationSource, удалив "s"

Документация https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#cors

// по умолчанию используется Бин по имени corsConfigurationSource

0 голосов
/ 21 января 2019

Я рекомендую вам использовать WebMvcConfigurer, а в методе addCorsMappings задайте конфигурацию CORS.

Как-то так

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("http://localhost:9798")
            .allowedMethods("POST", "GET")
            //.allowedHeaders("header1", "header2", "header3")
            //.exposedHeaders("header1", "header2")
            .allowCredentials(true).maxAge(3600);
    }
}

Здесь есть ссылка с полнофункциональным проектом Spring с CORS, просто скачайте и запустите его.

https://github.com/reos79/spring-cors

У него есть html-страница (person.html), эта страница ничего не делает, кроме как вызывает службу в порту (9797).Таким образом, вам нужно загрузить этот проект дважды: один раз на порт 9797 для загрузки службы, а другой на порт (9798).Затем в браузере вы вызываете страницу person на сервере localhost: 9798, и он вызывает службу на localhost: 9797, в файле application.properties я настроил порт.

0 голосов
/ 21 января 2019

Это мой рабочий @Configuration класс для обработки запросов 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, используемого запросом предполетной проверки (как упомянутое вами исключение)

@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();
    }

}

Потому что, когда вы используете коры, у вас есть Простой запрос и Предварительный запрос , который вызывает HttpMethod.OPTIONS

0 голосов
/ 21 января 2019

Вы должны указать Spring Security использовать созданную вами конфигурацию CORS.

В моем проекте я настроил 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;
    }

Надеюсь, это полезно

Angelo

0 голосов
/ 18 января 2019

Ваш допустимый источник - 127.0.0.1, но у вашей клиентской стороны есть ip 123.123.123.123. Попробуйте изменить это:

config.addAllowedOrigin("127.0.0.1");

К этому:

config.addAllowedOrigin("123.123.123.123");
0 голосов
/ 18 января 2019

Вам нужно добавить @CrossOrigin уровень класса в вашем классе контроллера, как показано ниже

@CrossOrigin
public class SampleController {
    // Your code goes here
}

примечание к вашему контроллеру класса отдыха

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...