spring security OAuth2集成过程中遇到的问题

spring security OAuth2集成过程中遇到的问题

      • 1、There is no PasswordEncoder mapped for the id "null"

1、There is no PasswordEncoder mapped for the id “null”

spring security 5中密码格式修改为格式:{id}encodedPassword 这id是一个标识符,用于查找是哪个PasswordEncoder,也就是你密码加密的格式所对应的PasswordEncoder。encodedPassword是指原始加密后的密码)。id必须在密码的开始,id前后必须加{}。如果id找不到,id则会为空。
集成spring security oauth2时secret也必须为{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();
    }

}

你可能感兴趣的:(java技术)