Using Spring Boot 2.6.4. Here is my SecurityConfig class:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
SecurityService securityService;
@Override
protected void configure(HttpSecurity http) throws Exception
{
http.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and()
.httpBasic();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception
{
auth.inMemoryAuthentication()
.withUser("admin")
.password("{noop}"+securityService.getApiKey())
.roles("ADMIN");
}
}
I'm entering here only when starting application. How to get to the configureGlobal
method after changing the password?
Here is how I change my password in the @RestController class (just store it in DB):
@PostMapping
public void update(@Valid @RequestBody SecurityDto dto) {
securityService.save(dto.getApiKey());
SecurityContextHolder.clearContext();
}
So my old password remains valid until restarting the application. Thats because I get to the SecurityConfig.configureGlobal method only when application starts. So how to change the password properly?
UPDATE: Resolved by implementing own UserDetailsService and using it instead of inMemoryAuthentication
auth.userDetailsService(userDetailsService);