Spring security OAuth2 problems encountered in the integration process
1、There is no PasswordEncoder mapped for the id “null”
spring security 5 password format from the format: {id} encodedPassword this id is an identifier, which is used to locate The PasswordEncoder, your password is encrypted format corresponding PasswordEncoder. encodedPassword refers to the original password encryption). id must start password, id must be added before and after the {}. If you can not find the id, id will be empty.
When integrated spring security oauth2 secret must also be {id} encodedPassword
@Configuration
public class MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception{
clients.inMemory().withClient("trying").secret("{noop}secret").authorizedGrantTypes("refresh_token","password","client_credentials").scopes("webclient","mobileclient");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpointsConfigurer) throws Exception{
endpointsConfigurer.authenticationManager((authenticationManager)).userDetailsService(userDetailsService);
}
}
@Configuration
public class MyWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception{
return super.authenticationManagerBean();
}
@Override
@Bean
public UserDetailsService userDetailsService() {
UserDetailsService userDetailsService = null;
try {
userDetailsService = super.userDetailsServiceBean();
} catch (Exception e) {
e.printStackTrace();
}
return userDetailsService;
}
@Override
protected void configure(AuthenticationManagerBuilder builder) throws Exception{
builder.userDetailsService(userDetailsService()).passwordEncoder(getPasswordEncoder());
builder.inMemoryAuthentication().passwordEncoder(getPasswordEncoder()).withUser("test").password(getPasswordEncoder().encode("123456")).roles("USER").and().withUser("trying").password(getPasswordEncoder().encode("123456")).roles("USER","ADMIN");
}
/**
* @Title:
* @Description: 获取加密对象
* @param
* @return
* @author huxx
* @date 2019/11/16 下午2:19
* @update
*/
private PasswordEncoder getPasswordEncoder(){
return new BCryptPasswordEncoder();
}
}