spring-cloud-oauth2升级版本遇到的认证报bad credentials,Encoded password does not look likebcrypt的问题

升级Spring Cloud至Finchley版本后,解决OAuth2认证报错'badcredentials'及'Encodedpassworddoesnotlooklikebcrypt'问题。通过调整BCryptPasswordEncoder使用及密码加密方式,实现与原有数据库密码格式兼容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring-cloud-oauth2升级版本遇到的认证报bad credentials,Encoded password does not look likebcrypt的问题

 

初始版本为

springboot 1.5.9.RELEASE
springcloud Dalston.SR1

升级为

springboot 2.0.3.RELEASE
springcloude finchley.RELEASE

升级改造完成之后,服务运行正常,但是请求认证的时候报错:

http://localhost:9000/oauth/token?grant_type=password&scope=app&client_id=client_2&client_secret=123456&username=user&password=123456

回复

{
“error”: “invalid_client”,
“error_description”: “Bad client credentials”
}

查看后端代码log

2018-09-12 00:49:40.910 WARN 519 — [nio-9000-exec-2] o.s.s.c.bcrypt.BCryptPasswordEncoder : Encoded password does not look like BCrypt

改了各种配置。看各种配置文档之后在优快云找到一篇有用的博客
https://blog.youkuaiyun.com/smollsnail/article/details/78934188
根据这个修改了两处代码之后可以运行也不报错了

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

这里.secret(bCryptPasswordEncoder.encode(“123456”))也要加密

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    //配置两个客户端,一个用于password认证一个用于client认证
    clients.inMemory()
            .withClient("client_2")
            .resourceIds(DEMO_RESOURCE_ID)
            .authorizedGrantTypes("password", "refresh_token")
            .scopes("app")
            .authorities("ROLE_APP")
            .secret(bCryptPasswordEncoder.encode("123456"))
            .accessTokenValiditySeconds(60 * 30)
            .refreshTokenValiditySeconds(60 * 60);
}
  •  

但是这样数据库存储的密码的样子就变化了

{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
  •  

原本的系统里面的数据就要修改。 以及一些其他的问题。不能修改原来数据库的数据。

于是猜想把加密模式换回直接bCrypt加密类,居然真的成功了,并没有强制要用新加的工厂模式

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

之后可能还需要再看下源码,猜想是以前在
.secret(bCryptPasswordEncoder.encode(“123456”))
这里不用加密,现在这里也要默认加密匹配。所以最终的修改方案仅仅需要把原本

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    //配置两个客户端,一个用于password认证一个用于client认证
    clients.inMemory()
            .withClient("client_2")
            .resourceIds(DEMO_RESOURCE_ID)
            .authorizedGrantTypes("password", "refresh_token")
            .scopes("app")
            .authorities("ROLE_APP")
            .secret("123456")
            .accessTokenValiditySeconds(60 * 30)
            .refreshTokenValiditySeconds(60 * 60);
}
  •  

修改后

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    //配置两个客户端,一个用于password认证一个用于client认证
    clients.inMemory()
            .withClient("client_2")
            .resourceIds(DEMO_RESOURCE_ID)
            .authorizedGrantTypes("password", "refresh_token")
            .scopes("app")
            .authorities("ROLE_APP")
            .secret(bCryptPasswordEncoder.encode("123456"))
            .accessTokenValiditySeconds(60 * 30)
            .refreshTokenValiditySeconds(60 * 60);
}
  •  

就解决了升级之后认证报错的问题。虽然最后的解决方案仅仅改动了一行代码。但是前后花了3个多小时才搞定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值