|
| 1 | +--- /dev/null |
| 2 | ++++ configserver/src/main/java/io/steeltoe/docker/configserver/BasicOrNoAuthConfig.java 2025-08-15 13:15:18.461432100 -0500 |
| 3 | +@@ -0,0 +1,62 @@ |
| 4 | ++package io.steeltoe.docker.configserver; |
| 5 | ++ |
| 6 | ++import org.springframework.beans.factory.annotation.Value; |
| 7 | ++import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 8 | ++import org.springframework.boot.autoconfigure.security.SecurityProperties; |
| 9 | ++import org.springframework.context.annotation.Bean; |
| 10 | ++import org.springframework.context.annotation.Configuration; |
| 11 | ++import org.springframework.core.annotation.Order; |
| 12 | ++import org.springframework.security.config.Customizer; |
| 13 | ++import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 14 | ++import org.springframework.security.core.userdetails.User; |
| 15 | ++import org.springframework.security.core.userdetails.UserDetailsService; |
| 16 | ++import org.springframework.security.crypto.password.NoOpPasswordEncoder; |
| 17 | ++import org.springframework.security.crypto.password.PasswordEncoder; |
| 18 | ++import org.springframework.security.provisioning.InMemoryUserDetailsManager; |
| 19 | ++import org.springframework.security.web.SecurityFilterChain; |
| 20 | ++ |
| 21 | ++@Configuration |
| 22 | ++public class BasicOrNoAuthConfig { |
| 23 | ++ |
| 24 | ++ @Bean |
| 25 | ++ @ConditionalOnProperty(prefix = "auth", name = "enabled", havingValue = "true") |
| 26 | ++ public UserDetailsService userDetailsService( |
| 27 | ++ @Value("${auth.username:devuser}") String username, |
| 28 | ++ @Value("${auth.password:devpassword}") String password) { |
| 29 | ++ |
| 30 | ++ return new InMemoryUserDetailsManager( |
| 31 | ++ User.withUsername(username) |
| 32 | ++ .password(password) |
| 33 | ++ .roles("USER") |
| 34 | ++ .build()); |
| 35 | ++ } |
| 36 | ++ |
| 37 | ++ @SuppressWarnings("deprecation") |
| 38 | ++ @Bean |
| 39 | ++ @ConditionalOnProperty(prefix = "auth", name = "enabled", havingValue = "true") |
| 40 | ++ public PasswordEncoder passwordEncoder() { |
| 41 | ++ // For dev/test only — plaintext password |
| 42 | ++ return NoOpPasswordEncoder.getInstance(); |
| 43 | ++ } |
| 44 | ++ |
| 45 | ++ @Bean |
| 46 | ++ @ConditionalOnProperty(prefix = "auth", name = "enabled", havingValue = "true") |
| 47 | ++ @Order(SecurityProperties.BASIC_AUTH_ORDER) |
| 48 | ++ public SecurityFilterChain basicAuthChain(HttpSecurity http) throws Exception { |
| 49 | ++ http |
| 50 | ++ .authorizeHttpRequests(auth -> auth.anyRequest().authenticated()) |
| 51 | ++ .httpBasic(Customizer.withDefaults()) |
| 52 | ++ .csrf(csrf -> csrf.disable()); |
| 53 | ++ return http.build(); |
| 54 | ++ } |
| 55 | ++ |
| 56 | ++ @Bean |
| 57 | ++ @ConditionalOnProperty(prefix = "auth", name = "enabled", havingValue = "false", matchIfMissing = true) |
| 58 | ++ @Order(SecurityProperties.BASIC_AUTH_ORDER) |
| 59 | ++ public SecurityFilterChain permitAllChain(HttpSecurity http) throws Exception { |
| 60 | ++ http |
| 61 | ++ .authorizeHttpRequests(auth -> auth.anyRequest().permitAll()) |
| 62 | ++ .csrf(csrf -> csrf.disable()); |
| 63 | ++ return http.build(); |
| 64 | ++ } |
| 65 | ++} |
0 commit comments