spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.63k stars 38.13k forks source link

Can't access setAllowedOriginPatterns method #26066

Closed crowmagnumb closed 3 years ago

crowmagnumb commented 4 years ago

Affects: 5.3.0


I use the following code to configure CORS...

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Component
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        String[] allowedOrigins = <origins>;
        CorsRegistration cors = registry.addMapping("/**").allowCredentials(true);
        cors.allowedOrigins(allowedOrigins);
    }
}

But I would like to use the setAllowedOriginPatterns that is on CorsConfiguration but CorsRegistration does not expose this so I can't. Why is this protected?

sbrannen commented 4 years ago

As of Spring Framework 5.3, CorsRegistration exposes an allowedOriginPatterns(String...) method which delegates to CorsConfiguration.setAllowedOriginPatterns(List<String>).

Does that meet your needs?

If not, why do you explicitly need to invoke setAllowedOriginPatterns(List<String>) instead of allowedOriginPatterns(String...)?

crowmagnumb commented 3 years ago

Ugh. OK, even though I am importing org.springframework.spring-web version 5.3.0 in one part of my code. I am using primarily org.springframework.boot.spring-boot-starter-web which is at version 2.3.5.RELEASE, well actually 2.4.0 was just released 3 hours ago, BUT that is on org.springframework:spring-web:5.2.2.RELEASE! So, my IDE is picking up that one and thus I don't see the method. Why is the boot code not more up-to-date with the core stuff?

crowmagnumb commented 3 years ago

I need to legitimately know how to keep these things all in sync so that I don't have duplicate spring-core/spring-web code multiple times in my classpath. So currently I use org.springframework.spring-web for my server to make http requests of other servers. I use org.springframework.boot.spring-boot-starter-web to create an api to connect to my server. And I use org.springframework.session.spring-session-jdbc to save session information of client connects to the server. I just tried to figure out a scenario of these three dependencies that all share a common spring-web version and could not. If I use version 2.3.5.RELEASE of spring-boot-starter-web it seems to use 5.2.10.RELEASE of spring-web. But spring-session-jdbc version 2.3.1 uses 5.2.9.RELEASE and the next version 2.4.0 uses 5.3.0 so there exists no version of that that uses the same version I need for boot. Is there a concerted effort to coordinate release schedules beteween these diffferent aspects of springframework?

crowmagnumb commented 3 years ago

Sorry, for the barrage here, but wanted to let you know that I set just cleared my local maven cache of all springframework code, reset my pom variables to...

        <springcore.version>5.3.0</springcore.version>
        <springboot.version>2.4.0</springboot.version>
        <springsession.version>2.4.1</springsession.version>

... and rebuilt everything. And now I only have spring-web version 5.3.0. I have both 5.3.0 and 5.3.1 of core, but my built jar file with all dependencies only contains 5.3.0. And now I see the method I was looking for in my IDE. Thank you for bearing with me.

rstoyanchev commented 3 years ago

No problem.