Allow runtime configuration of acceptable domains for CORS

This commit is contained in:
William Brawner 2020-02-15 11:51:30 -07:00
parent 0321af50ce
commit d2bd1ac301
2 changed files with 9 additions and 2 deletions

View file

@ -5,6 +5,7 @@ import com.wbrawner.budgetserver.user.UserRepository
import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.core.env.Environment import org.springframework.core.env.Environment
import org.springframework.core.env.get
import org.springframework.security.authentication.dao.DaoAuthenticationProvider import org.springframework.security.authentication.dao.DaoAuthenticationProvider
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity
@ -26,7 +27,8 @@ open class SecurityConfig(
private val datasource: DataSource, private val datasource: DataSource,
private val userRepository: UserRepository, private val userRepository: UserRepository,
private val passwordResetRequestRepository: PasswordResetRequestRepository, private val passwordResetRequestRepository: PasswordResetRequestRepository,
private val userDetailsService: JdbcUserDetailsService private val userDetailsService: JdbcUserDetailsService,
private val environment: Environment
) : WebSecurityConfigurerAdapter() { ) : WebSecurityConfigurerAdapter() {
open val userDetailsManager: JdbcUserDetailsManager open val userDetailsManager: JdbcUserDetailsManager
@ -64,7 +66,11 @@ open class SecurityConfig(
.and() .and()
.cors() .cors()
.configurationSource { .configurationSource {
CorsConfiguration().applyPermitDefaultValues() with(CorsConfiguration()) {
applyPermitDefaultValues()
allowedOrigins = environment.get("twigs.cors.domains")?.split(",")
this
}
} }
.and() .and()
.csrf() .csrf()

View file

@ -8,3 +8,4 @@ spring.session.jdbc.initialize-schema=always
spring.datasource.testWhileIdle=true spring.datasource.testWhileIdle=true
spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.validationQuery=SELECT 1 spring.datasource.validationQuery=SELECT 1
twigs.cors.domains=*