From 8fd644c45027b34fd31e68b1f212bdc93e4fe1c1 Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Tue, 14 Dec 2021 13:22:46 +0100 Subject: [PATCH] JAVA-8794: Fix circural dependencies in spring-security-web-boot-2 --- .../h2/config/PasswordEncoderConfig.java | 15 ++++++++++ .../h2/config/SecurityConfiguration.java | 28 ++++++++----------- 2 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/PasswordEncoderConfig.java diff --git a/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/PasswordEncoderConfig.java b/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/PasswordEncoderConfig.java new file mode 100644 index 0000000000..4c21b315fb --- /dev/null +++ b/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/PasswordEncoderConfig.java @@ -0,0 +1,15 @@ +package com.baeldung.jdbcauthentication.h2.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class PasswordEncoderConfig { + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } +} diff --git a/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/SecurityConfiguration.java b/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/SecurityConfiguration.java index 50dc5b6958..49804e8458 100644 --- a/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/SecurityConfiguration.java +++ b/spring-security-modules/spring-security-web-boot-2/src/main/java/com/baeldung/jdbcauthentication/h2/config/SecurityConfiguration.java @@ -1,19 +1,18 @@ package com.baeldung.jdbcauthentication.h2.config; -import javax.sql.DataSource; - import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; +import javax.sql.DataSource; + @Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter { + @Override protected void configure(HttpSecurity httpSecurity) throws Exception { httpSecurity.authorizeRequests() @@ -32,20 +31,15 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { } @Autowired - private DataSource dataSource; - - @Autowired - public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + public void configureGlobal(AuthenticationManagerBuilder auth, + DataSource dataSource, + PasswordEncoder passwordEncoder) throws Exception { auth.jdbcAuthentication() - .dataSource(dataSource) - .withDefaultSchema() - .withUser(User.withUsername("user") - .password(passwordEncoder().encode("pass")) - .roles("USER")); + .dataSource(dataSource) + .withDefaultSchema() + .withUser(User.withUsername("user") + .password(passwordEncoder.encode("pass")) + .roles("USER")); } - @Bean - public PasswordEncoder passwordEncoder() { - return new BCryptPasswordEncoder(); - } } \ No newline at end of file