2017-05-09 10 views
2

Java config를 사용하여 글로벌 CORS 구성을 수행하는 데 어려움을 겪고 있습니다. 다른 사람도 같은 문제가 있기 때문에 알려진 문제인지 잘 모르겠어요 : 어쨌든 Spring Global CORS configuration not working but Controller level config doesJava Config를 사용하는 글로벌 CORS 구성이 작동하지 않습니다.

을, 나에게 내가 뭘하려고 오전 더욱 정교하게 : 나는 스프링 4.2 사용하고

. 3. 릴리스 (스프링 부트 없음). CORS 도메인 (allowed-Origin)에 @Value를 사용하여 속성 파일을 주입 할 수 있도록 Java 구성을 사용하여 전역 CORS 구성을 구성하려고합니다. 내 지식, 나는 그림 울부 짖는 소리로 허용 기원에 대한 MVC XML 네임 스페이스의 값을 삽입 할 수 없기 때문에 (이 문제에 대한 다른 접근 방법을 알고 있다면 알려 주시기 바랍니다) : 봄 참조 문서 here에서

<mvc:cors> 
     <mvc:mapping path="/**" allowed-origins="${vrm.cors.domain}" 
        allowed-methods="*" 
        allowed-headers="*" 
        allow-credentials="false" max-age="3600"/> 
</mvc:cors> 

, 문제를 해결하기 위해 Java 구성을 사용하여 Global CORS를 구성하려고합니다. 그러나 CORS는 작동하지 않습니다. 코드는 항상

를 돌려 응용 프로그램을 시작하는 동안 전화를하지만, 클라이언트에서 호출 않습니다 없음 '액세스 제어 - 허용 - 원산지'헤더가 요청 자원에 존재합니다. 이 답변에 따르면 원산지 ' http://localhost:3000'때문에 허용되지 않습니다 액세스

@Configuration 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter { 
    @Value("${vrm.cors.domain}") 
    //vrm.cors.domain=/** 
    private String corsDomain; 

    @Override 
    public void addCorsMappings(CorsRegistry registry) { 
     registry.addMapping("/**"). 
     .allowedOrigins(vrm.cors.domain); 
    } 
} 
+0

왜'vrm.cors.domain'이'/ **'으로 설정 되었습니까? 모든 것을 허용하려면'*'가되어서는 안됩니까? – jingx

+0

죄송합니다, 이것은 샘플 코드가 잘못되었습니다. 나는 같은 것을 위해 갱신했다. –

답변

0

https://stackoverflow.com/a/33823253

이 같은 고르를 활성화 할 수 있습니다

:

@EnableWebSecurity 
@Configuration 
class SecurityConfig extends WebSecurityConfigurerAdapter { 

    private final AppProperties appProperties; 

    @Autowired 
    public SecurityConfig(AppProperties appProperties) { 
    this.appProperties = appProperties; 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
    http 
     .addFilterBefore(corsFilter(), SessionManagementFilter.class) //adds your custom CorsFilter 
     .authorizeRequests() 
     .antMatchers("/**") 
     .permitAll() 
     .and() 
     .csrf() 
     .disable(); 
    } 

    private CorsFilter corsFilter() { 
    return new CorsFilterAdapter(
     appProperties.getClientUrls(), 
     appProperties.getHeaders(), 
     appProperties.getMethods()) 
     .corsFilter(); 
    } 
} 

CrosFilterAdapter이 같은 것입니다 경우

public class CorsFilterAdapter { 

    private final String[] clientUrls; 
    private final String[] headers; 
    private final String[] methods; 

    public CorsFilterAdapter(String clientUrls, String headers, String methods) { 
    this.clientUrls = clientUrls.split(","); 
    this.headers = headers.split(","); 
    this.methods = methods.split(","); 
    } 

    public CorsFilter corsFilter() { 
    CorsConfiguration config = new CorsConfiguration(); 
    config.setAllowCredentials(false); 
    for (String clientUrl : clientUrls) { 
     config.addAllowedOrigin(clientUrl); 
    } 
    for (String header: headers) { 
     config.addAllowedHeader(header); 
    } 
    for (String method : methods) { 
     config.addAllowedMethod(method); 
    } 
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); 
    source.registerCorsConfiguration("/**", config); 
    return new CorsFilter(source); 
    } 
}