Skip to content

Commit

Permalink
Fix circular passwordencoder dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
blootsvoets committed Jan 18, 2022
1 parent 0cde3c4 commit c72b61d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
Expand Down Expand Up @@ -59,6 +60,9 @@ public class OAuth2ServerConfiguration {
@Autowired
private DataSource dataSource;

@Autowired
private PasswordEncoder passwordEncoder;

@Configuration
@Order(-20)
protected static class LoginConfig extends WebSecurityConfigurerAdapter {
Expand Down Expand Up @@ -87,7 +91,7 @@ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
@Bean
public JdbcClientDetailsService jdbcClientDetailsService() {
JdbcClientDetailsService clientDetailsService = new JdbcClientDetailsService(dataSource);
clientDetailsService.setPasswordEncoder(new BCryptPasswordEncoder());
clientDetailsService.setPasswordEncoder(passwordEncoder);
return clientDetailsService;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
Expand All @@ -39,21 +38,30 @@
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationManagerBuilder authenticationManagerBuilder;
private final AuthenticationManagerBuilder authenticationManagerBuilder;

@Autowired
private UserDetailsService userDetailsService;
private final UserDetailsService userDetailsService;

private final ApplicationEventPublisher applicationEventPublisher;
private final PasswordEncoder passwordEncoder;

@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder,
UserDetailsService userDetailsService,
ApplicationEventPublisher applicationEventPublisher,
PasswordEncoder passwordEncoder) {
this.authenticationManagerBuilder = authenticationManagerBuilder;
this.userDetailsService = userDetailsService;
this.applicationEventPublisher = applicationEventPublisher;
this.passwordEncoder = passwordEncoder;
}

@PostConstruct
public void init() {
try {
authenticationManagerBuilder
.userDetailsService(userDetailsService)
.passwordEncoder(passwordEncoder())
.passwordEncoder(passwordEncoder)
.and()
.authenticationProvider(new RadarAuthenticationProvider())
.authenticationEventPublisher(
Expand All @@ -73,13 +81,8 @@ public Http401UnauthorizedEntryPoint http401UnauthorizedEntryPoint() {
return new Http401UnauthorizedEntryPoint();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Override
public void configure(WebSecurity web) throws Exception {
public void configure(WebSecurity web) {
web.ignoring()
.antMatchers(HttpMethod.OPTIONS, "/**")
.antMatchers("/app/**/*.{js,html}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
Expand Down Expand Up @@ -126,6 +128,11 @@ public CorsFilter corsFilter() {
return new CorsFilter(source);
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

/**
* Initializes H2 console.
*/
Expand Down

0 comments on commit c72b61d

Please sign in to comment.