From 7f34e20c354511d079c3cc711041137b98439145 Mon Sep 17 00:00:00 2001 From: Nasruddin Date: Sun, 18 Feb 2024 13:37:21 +0530 Subject: [PATCH] Few more updates for spring 3 upgrade --- .../course-composite-service/pom.xml | 17 +++--- microservices/course-service/pom.xml | 2 +- microservices/search-service/pom.xml | 2 +- microservices/student-service/pom.xml | 2 +- microservices/vote-service/pom.xml | 2 +- .../configuration/SecurityConfig.java | 54 +++++++++++-------- .../gateway/configuration/SecurityConfig.java | 24 +++++---- .../src/main/resources/application.properties | 2 +- .../util/UtilApplicationTests.java | 7 +-- 9 files changed, 63 insertions(+), 49 deletions(-) diff --git a/microservices/course-composite-service/pom.xml b/microservices/course-composite-service/pom.xml index 4f260f6..8108fde 100644 --- a/microservices/course-composite-service/pom.xml +++ b/microservices/course-composite-service/pom.xml @@ -15,7 +15,7 @@ Demo project for Spring Boot 17 - 2021.0.1 + 2022.0.1 @@ -26,15 +26,15 @@ org.springframework.boot spring-boot-starter-webflux - + org.springdoc - springdoc-openapi-webflux-ui - 1.5.10 + springdoc-openapi-starter-webflux-ui + 2.0.4 org.springframework.boot @@ -107,6 +107,11 @@ pom import + diff --git a/microservices/course-service/pom.xml b/microservices/course-service/pom.xml index 822382f..c4485f7 100644 --- a/microservices/course-service/pom.xml +++ b/microservices/course-service/pom.xml @@ -16,7 +16,7 @@ 17 1.16.2 - 2021.0.1 + 2022.0.1 diff --git a/microservices/search-service/pom.xml b/microservices/search-service/pom.xml index 79ae347..1608701 100644 --- a/microservices/search-service/pom.xml +++ b/microservices/search-service/pom.xml @@ -16,7 +16,7 @@ 17 1.16.2 - 2021.0.1 + 2022.0.1 diff --git a/microservices/student-service/pom.xml b/microservices/student-service/pom.xml index 69fd621..e4ab3a7 100644 --- a/microservices/student-service/pom.xml +++ b/microservices/student-service/pom.xml @@ -16,7 +16,7 @@ 17 1.16.2 - 2021.0.1 + 2022.0.1 diff --git a/microservices/vote-service/pom.xml b/microservices/vote-service/pom.xml index da99384..4484d1e 100644 --- a/microservices/vote-service/pom.xml +++ b/microservices/vote-service/pom.xml @@ -16,7 +16,7 @@ 17 1.16.2 - 2021.0.1 + 2022.0.1 diff --git a/spring-cloud/eureka-server/src/main/java/io/javatab/springcloud/eurekaserver/configuration/SecurityConfig.java b/spring-cloud/eureka-server/src/main/java/io/javatab/springcloud/eurekaserver/configuration/SecurityConfig.java index 42dd982..7eb2c64 100644 --- a/spring-cloud/eureka-server/src/main/java/io/javatab/springcloud/eurekaserver/configuration/SecurityConfig.java +++ b/spring-cloud/eureka-server/src/main/java/io/javatab/springcloud/eurekaserver/configuration/SecurityConfig.java @@ -1,42 +1,52 @@ package io.javatab.springcloud.eurekaserver.configuration; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.Customizer; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.crypto.password.NoOpPasswordEncoder; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; +import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { - @Value("${app.eureka-username}") - private String username; + private final String username; + private final String password; - @Value("${app.eureka-password}") - private String password; + @Autowired + public SecurityConfig( + @Value("${app.eureka-username}") String username, + @Value("${app.eureka-password}") String password + ) { + this.username = username; + this.password = password; + } - public void configure(AuthenticationManagerBuilder auth) throws Exception { - auth.inMemoryAuthentication() - .passwordEncoder(NoOpPasswordEncoder.getInstance()) - .withUser(username).password(password) - .authorities("USER"); + @Bean + public InMemoryUserDetailsManager userDetailsService() { + UserDetails user = User.withDefaultPasswordEncoder() + .username(username) + .password(password) + .roles("USER") + .build(); + return new InMemoryUserDetailsManager(user); } - protected void configure(HttpSecurity http) throws Exception { + @Bean + public SecurityFilterChain configure(HttpSecurity http) throws Exception { http - // Disable CSRF to allow services to register themselves with Eureka + // Disable CRCF to allow services to register themselves with Eureka .csrf() .disable() - .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> { - try { - authorizationManagerRequestMatcherRegistry - .anyRequest().authenticated().and().httpBasic(Customizer.withDefaults()); - } catch (Exception e) { - e.printStackTrace(); - } - }); + .authorizeRequests() + .anyRequest().authenticated() + .and() + .httpBasic(); + return http.build(); } } diff --git a/spring-cloud/gateway/src/main/java/io/javatab/springcloud/gateway/configuration/SecurityConfig.java b/spring-cloud/gateway/src/main/java/io/javatab/springcloud/gateway/configuration/SecurityConfig.java index c257b6a..712e765 100644 --- a/spring-cloud/gateway/src/main/java/io/javatab/springcloud/gateway/configuration/SecurityConfig.java +++ b/spring-cloud/gateway/src/main/java/io/javatab/springcloud/gateway/configuration/SecurityConfig.java @@ -3,10 +3,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.web.server.SecurityWebFilterChain; +@Configuration @EnableWebFluxSecurity public class SecurityConfig { @@ -17,18 +19,18 @@ SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) throws http .csrf().disable() .authorizeExchange() - .pathMatchers("/config/**").permitAll() - .pathMatchers("/actuator/**").permitAll() - .pathMatchers("/eureka/**").permitAll() - .pathMatchers("/oauth2/**").permitAll() - .pathMatchers("/login/**").permitAll() - .pathMatchers("/error/**").permitAll() - .pathMatchers("/openapi/**").permitAll() - .pathMatchers("/webjars/**").permitAll() - .anyExchange().authenticated() - .and() + .pathMatchers("/actuator/**").permitAll() + .pathMatchers("/eureka/**").permitAll() + .pathMatchers("/oauth2/**").permitAll() + .pathMatchers("/login/**").permitAll() + .pathMatchers("/error/**").permitAll() + .pathMatchers("/openapi/**").permitAll() + .pathMatchers("/webjars/**").permitAll() + .pathMatchers("/config/**").permitAll() + .anyExchange().authenticated() + .and() .oauth2ResourceServer() - .jwt(); + .jwt(); return http.build(); } diff --git a/util/src/main/resources/application.properties b/util/src/main/resources/application.properties index cbe617e..1c421cf 100644 --- a/util/src/main/resources/application.properties +++ b/util/src/main/resources/application.properties @@ -1 +1 @@ -server.port=0 +server.port=0 \ No newline at end of file diff --git a/util/src/test/java/io/javatab/microservices/util/UtilApplicationTests.java b/util/src/test/java/io/javatab/microservices/util/UtilApplicationTests.java index dc083f1..41743ce 100644 --- a/util/src/test/java/io/javatab/microservices/util/UtilApplicationTests.java +++ b/util/src/test/java/io/javatab/microservices/util/UtilApplicationTests.java @@ -1,13 +1,10 @@ package io.javatab.microservices.util; import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest class UtilApplicationTests { @Test - void contextLoads() { - } + void testFlux() { + } }