I want to read a list of hosts from a yaml file (application.yml), the file looks like this:
cors:
hosts:
allow:
- http://foo1/
It's easy, the answer is in this doc and also in this one
So, you have a yaml like this:
cors:
hosts:
allow:
- http://foo1/
- http://foo2/
- http://foo3/
Then you first bind the data
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix="cors.hosts")
public class AllowedHosts {
private List<String> HostNames; //You can also bind more type-safe objects
}
Then in another component you just do
@Autowired
private AllowedHosts allowedHosts;
And you are done!
I met the same problem, But solved, give you my solution
exclude-url: >
/management/logout,
/management/user/login,
/partner/logout,
/partner/user/login
success in the version of Spring boot 2.1.6.RELEASE
I have been able to read list from properties like below way-
Properties-
cors.hosts.allow[0]=host-0
cors.hosts.allow[1]=host-1
Read property-
@ConfigurationProperties("cors.hosts")
public class ReadProperties {
private List<String> allow;
public List<String> getAllow() {
return allow;
}
public void setAllow(List<String> allow) {
this.allow = allow;
}
}
use comma separated values in application.yml
corsHostsAllow: http://foo1/, http://foo2/, http://foo3/
java code for access
@Value("${corsHostsAllow}")
String[] corsHostsAllow
I tried and succeeded ;)