From 5507c8cc2f3c093f4ae6632141bc0f6d231bfe7b Mon Sep 17 00:00:00 2001 From: Quentin Date: Sat, 20 May 2023 15:21:50 +0200 Subject: [PATCH 1/2] Spring-boot 3.1.0 --- .../resources/generator/dependencies/pom.xml | 2 +- .../SecurityConfiguration.java.mustache | 45 +++++++------- .../SecurityConfiguration.java.mustache | 62 +++++++++---------- 3 files changed, 51 insertions(+), 58 deletions(-) diff --git a/src/main/resources/generator/dependencies/pom.xml b/src/main/resources/generator/dependencies/pom.xml index 7987be39fc6..22878720778 100644 --- a/src/main/resources/generator/dependencies/pom.xml +++ b/src/main/resources/generator/dependencies/pom.xml @@ -17,7 +17,7 @@ 3.24.2 5.3.1 7.3 - 3.0.6 + 3.1.0 2022.0.0 4.0.1 2.1.0 diff --git a/src/main/resources/generator/server/springboot/mvc/security/jwt/authentication/main/infrastructure/primary/SecurityConfiguration.java.mustache b/src/main/resources/generator/server/springboot/mvc/security/jwt/authentication/main/infrastructure/primary/SecurityConfiguration.java.mustache index cbaa275744e..f1522c88f0e 100644 --- a/src/main/resources/generator/server/springboot/mvc/security/jwt/authentication/main/infrastructure/primary/SecurityConfiguration.java.mustache +++ b/src/main/resources/generator/server/springboot/mvc/security/jwt/authentication/main/infrastructure/primary/SecurityConfiguration.java.mustache @@ -14,6 +14,8 @@ import org.springframework.security.config.annotation.method.configuration.Enabl import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @@ -59,26 +61,20 @@ class SecurityConfiguration { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // @formatter:off - return http - .csrf() - .disable() - .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class) - .headers() - .contentSecurityPolicy(properties.getContentSecurityPolicy()) - .and() - .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN) - .and() - .permissionsPolicy().policy("camera=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), sync-xhr=()") - .and() - .frameOptions() - .deny() - .and() - .formLogin().disable() - .httpBasic().disable() - .sessionManagement() - .sessionCreationPolicy(SessionCreationPolicy.STATELESS) - .and() - .authorizeHttpRequests() + http + .csrf(csrf -> csrf.disable()) + .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class) + .headers(headers -> headers + .contentSecurityPolicy(csp -> csp.policyDirectives(properties.getContentSecurityPolicy())) + .frameOptions(FrameOptionsConfig::deny) + .referrerPolicy(referrer -> referrer.policy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)) + .permissionsPolicy(permissions -> + permissions.policy("camera=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), sync-xhr=()")) + ) + .formLogin(AbstractHttpConfigurer::disable) + .httpBasic(AbstractHttpConfigurer::disable) + .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) + .authorizeHttpRequests(authz -> authz .requestMatchers("/api/authenticate").permitAll() .requestMatchers("/api/register").permitAll() .requestMatchers("/api/activate").permitAll() @@ -92,10 +88,11 @@ class SecurityConfiguration { .requestMatchers("/management/prometheus").permitAll() .requestMatchers("/management/**").hasAuthority(Role.ADMIN.key()) .anyRequest().authenticated() - .and() - .apply(new JWTConfigurer(authenticationTokenReader())) - .and() - .build(); + ); + + JWTConfigurer jwtConfigurer = new JWTConfigurer(authenticationTokenReader()); + http.apply(jwtConfigurer); + return http.build(); // @formatter:on } diff --git a/src/main/resources/generator/server/springboot/mvc/security/oauth2/core/main/infrastructure/primary/SecurityConfiguration.java.mustache b/src/main/resources/generator/server/springboot/mvc/security/oauth2/core/main/infrastructure/primary/SecurityConfiguration.java.mustache index d9ac221f9c9..04091749da6 100644 --- a/src/main/resources/generator/server/springboot/mvc/security/oauth2/core/main/infrastructure/primary/SecurityConfiguration.java.mustache +++ b/src/main/resources/generator/server/springboot/mvc/security/oauth2/core/main/infrastructure/primary/SecurityConfiguration.java.mustache @@ -15,6 +15,7 @@ import org.springframework.security.config.annotation.method.configuration.Enabl import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; +import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; @@ -35,6 +36,8 @@ import org.springframework.web.filter.CorsFilter; import {{packageName}}.authentication.domain.Role; import {{packageName}}.common.domain.Generated; +import static org.springframework.security.config.Customizer.withDefaults; + @Configuration @EnableWebSecurity @EnableMethodSecurity(prePostEnabled = true, securedEnabled = true) @@ -75,40 +78,33 @@ public class SecurityConfiguration { public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // @formatter:off return http - .csrf() - .disable() + .csrf(csrf -> csrf.disable()) .addFilterBefore(corsFilter, CsrfFilter.class) - .headers() - .contentSecurityPolicy(applicationSecurityProperties.getContentSecurityPolicy()) - .and() - .referrerPolicy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN) - .and() - .permissionsPolicy().policy("camera=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), sync-xhr=()") - .and() - .frameOptions() - .sameOrigin() - .and() - .authorizeHttpRequests() - .requestMatchers("/api/authenticate").permitAll() - .requestMatchers("/api/auth-info").permitAll() - .requestMatchers("/api/admin/**").hasAuthority(Role.ADMIN.key()) - .requestMatchers("/api/**").authenticated() - .requestMatchers("/management/health").permitAll() - .requestMatchers("/management/health/**").permitAll() - .requestMatchers("/management/info").permitAll() - .requestMatchers("/management/prometheus").permitAll() - .requestMatchers("/management/**").hasAuthority(Role.ADMIN.key()) - .anyRequest().authenticated() - .and() - .oauth2Login() - .and() - .oauth2ResourceServer() - .jwt() - .jwtAuthenticationConverter(authenticationConverter()) - .and() - .and() - .oauth2Client() - .and().build(); + .headers(headers -> headers + .contentSecurityPolicy(csp -> csp.policyDirectives(applicationSecurityProperties.getContentSecurityPolicy())) + .frameOptions(FrameOptionsConfig::sameOrigin) + .referrerPolicy(referrer -> referrer.policy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN)) + .permissionsPolicy(permissions -> + permissions.policy("camera=(), fullscreen=(self), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), sync-xhr=()")) + ) + .authorizeHttpRequests(authz -> authz + .requestMatchers("/api/authenticate").permitAll() + .requestMatchers("/api/auth-info").permitAll() + .requestMatchers("/api/admin/**").hasAuthority(Role.ADMIN.key()) + .requestMatchers("/api/**").authenticated() + .requestMatchers("/management/health").permitAll() + .requestMatchers("/management/health/**").permitAll() + .requestMatchers("/management/info").permitAll() + .requestMatchers("/management/prometheus").permitAll() + .requestMatchers("/management/**").hasAuthority(Role.ADMIN.key()) + .anyRequest().authenticated() + ) + .oauth2Login(withDefaults()) + .oauth2ResourceServer(oauth2 -> oauth2 + .jwt(jwt -> jwt.jwtAuthenticationConverter(authenticationConverter())) + ) + .oauth2Client(withDefaults()) + .build(); // @formatter:on } From edf28c763ed8055f26ba591678e5b2b27d845996 Mon Sep 17 00:00:00 2001 From: Pascal Grimaud Date: Sun, 21 May 2023 16:11:47 +0200 Subject: [PATCH 2/2] Fix mongodb uri with database name --- .../database/mongodb/domain/MongoDbModuleFactory.java | 2 +- .../database/mongodb/domain/MongoDbModuleFactoryTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactory.java index 9038e4ce514..a5a83e16130 100644 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactory.java +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactory.java @@ -57,7 +57,7 @@ public JHipsterModule buildModule(JHipsterModuleProperties properties) { .and() .springMainProperties() .set(propertyKey("spring.data.mongodb.database"), propertyValue(properties.projectBaseName().get())) - .set(propertyKey("spring.data.mongodb.uri"), propertyValue("mongodb://localhost:27017")) + .set(propertyKey("spring.data.mongodb.uri"), propertyValue("mongodb://localhost:27017/" + properties.projectBaseName().get())) .and() .springTestProperties() .set(propertyKey("spring.data.mongodb.uri"), propertyValue("${TEST_MONGODB_URI}")) diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactoryTest.java index af0bf8a39e0..30d9a096920 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/database/mongodb/domain/MongoDbModuleFactoryTest.java @@ -90,7 +90,7 @@ void shouldBuildModule() { .and() .hasFile("src/main/resources/config/application.properties") .containing("spring.data.mongodb.database=jhipster") - .containing("spring.data.mongodb.uri=mongodb://localhost:27017") + .containing("spring.data.mongodb.uri=mongodb://localhost:27017/jhipster") .and() .hasFile("src/test/resources/config/application.properties") .containing("spring.data.mongodb.uri=${TEST_MONGODB_URI}")