Spring Security 5:没有为id“null”映射PasswordEncoder。 [英] Spring Security 5 : There is no PasswordEncoder mapped for the id "null"

查看:1988
本文介绍了Spring Security 5:没有为id“null”映射PasswordEncoder。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Spring Boot 1.4.9迁移到Spring Boot 2.0以及Spring Security 5,我正在尝试通过OAuth 2进行身份验证。但是我收到了这个错误:

I am migrating from Spring Boot 1.4.9 to Spring Boot 2.0 and also to Spring Security 5 and I am trying to do authenticate via OAuth 2. But I am getting this error:


java.lang.IllegalArgumentException:没有为idnull

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null



来自的文档Spring Security 5 ,我知道密码的
存储格式已经改变。

From the documentation of Spring Security 5, I get to know that storage format for password is changed.

在我当前的代码中,我创建了我的密码编码器bean as:

In my current code I have created my password encoder bean as:

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

然而它给了我以下错误:

However it was giving me below error:


编码密码看起来不像BCrypt

所以我按照 Spring Security 5 文档:

@Bean
public PasswordEncoder passwordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

现在,如果我能在数据库中看到密码,那就是存储为

Now if I can see password in database it is storing as

{bcrypt}$2a$10$LoV/3z36G86x6Gn101aekuz3q9d7yfBp3jFn7dzNN/AL5630FyUQ

随着第一个错误消失,现在当我尝试进行身份验证时,我收到以下错误:

With that 1st error gone and now when I am trying to do authentication I am getting below error:


java.lang.IllegalArgumentException:没有为idnull

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null

映射的PasswordEncoder为了解决这个问题,我尝试了所有的来自Stackoverflow的问题:

To solve this issue I tried all the below questions from Stackoverflow:

Spring Oauth2。密码编码器未在DaoAuthenticationProvider中设置

她e是一个类似于我的但没有回答的问题:

Here is a question similar to mine but not answerd:

  • Spring Security 5 - Password Migration

注意:我已经存储了数据库中加密的密码,因此无需再次在 UserDetailsS​​ervice 中进行编码。

NOTE: I am already storing encrypted password in database so no need to encode again in UserDetailsService.

他们建议您提供的Spring security 5 文档使用以下方法处理此异常:

In the Spring security 5 documentation they suggested you can handle this exception using:


DelegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(PasswordEncoder)

DelegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(PasswordEncoder)

如果这是修复,那么我应该把它放在哪里?我试图将它放在 PasswordEncoder bean中,如下所示,但它无法正常工作:

If this is the fix then where should I put it? I have tried to put it in PasswordEncoder bean like below but it wasn't working:

DelegatingPasswordEncoder def = new DelegatingPasswordEncoder(idForEncode, encoders);
def.setDefaultPasswordEncoderForMatches(passwordEncoder);

MyWebSecurity类

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {

        web
                .ignoring()
                .antMatchers(HttpMethod.OPTIONS)
                .antMatchers("/api/user/add");
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

MyOauth2配置

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;


    @Bean
    public TokenEnhancer tokenEnhancer() {
        return new CustomTokenEnhancer();
    }

    @Bean
    public DefaultAccessTokenConverter accessTokenConverter() {
        return new DefaultAccessTokenConverter();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints
                .tokenStore(tokenStore())
                .tokenEnhancer(tokenEnhancer())
                .accessTokenConverter(accessTokenConverter())
                .authenticationManager(authenticationManager);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("test")
                .scopes("read", "write")
                .authorities(Roles.ADMIN.name(), Roles.USER.name())
                .authorizedGrantTypes("password", "refresh_token")
                .secret("secret")
                .accessTokenValiditySeconds(1800);
    }
}

请指导我解决这个问题。我花了好几个小时来解决这个问题,但无法修复。

Please guide me with this issue. I have spend hours to fix this but not able to fix.

推荐答案

配置 ClientDetailsS​​erviceConfigurer时,您还必须应用新的密码存储格式到客户端密码。

When you are configuring the ClientDetailsServiceConfigurer, you have to also apply the new password storage format to the client secret.

.secret("{noop}secret")

这篇关于Spring Security 5:没有为id“null”映射PasswordEncoder。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆