JAVA-8794: Fix circular dependency issue in spring-security-web-thymeleaf

This commit is contained in:
Krzysiek 2021-12-15 19:53:30 +01:00
parent de6bc0de3e
commit d72347f195
3 changed files with 22 additions and 8 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.springsecuritythymeleaf;
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 PasswordEncoderConfiguration {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}

View File

@ -8,6 +8,7 @@ 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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration @Configuration
@ -32,19 +33,15 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
} }
@Autowired @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { public void configureGlobal(AuthenticationManagerBuilder auth, PasswordEncoder passwordEncoder) throws Exception {
auth.inMemoryAuthentication() auth.inMemoryAuthentication()
.withUser("user") .withUser("user")
.password(passwordEncoder().encode("password")) .password(passwordEncoder.encode("password"))
.roles("USER") .roles("USER")
.and() .and()
.withUser("admin") .withUser("admin")
.password(passwordEncoder().encode("admin")) .password(passwordEncoder.encode("admin"))
.roles("ADMIN"); .roles("ADMIN");
} }
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
} }

View File

@ -9,11 +9,13 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@WebMvcTest @WebMvcTest
@Import(PasswordEncoderConfiguration.class)
public class ViewControllerIntegrationTest { public class ViewControllerIntegrationTest {
@Autowired @Autowired