oAuth2 升级Spring Cloud Finchley.RELEASE踩坑分享

背景

6.19号,spring团队发布了期待已久的 Spring Cloud Finchley.RELEASE 版本。 重要变化:

  • 基于Spring Boot 2.0.X
  • 不兼容 Spring Boot 1.5.X

期间踩过几个坑,分享出来给大伙,主要是关于 Spring Cloud oAuth 部分

目标

基于现有Spring Cloud 脚手架pig开始动手升级。

关于pig:

基于Spring Cloud、oAuth2.0开发基于Vue前后分离的开发平台,支持账号、短信、SSO等多种登录,提供配套视频开发教程。

码云地址:gitee.com/log4j/pig

版本变化

                 +------------------+
                 |                  |
                 |  1.5.12.RELEASE  |
                 |                  |
                 +--------+---------+
                          |
Spring Boot               |
                          v

                 +------------------+
                 |                  |
                 |  2.0.3.RELEASE   |
                 |                  |
                 +------------------+

复制代码
                 +------------------+
                 |                  |
                 |  Edgware.SR3     |
                 |                  |
                 +--------+---------+
                          |
Srping Cloud              |
                          v

                 +------------------+
                 |                  |
                 |  Finchley.RELEASE|
                 |                  |
                 +------------------+

复制代码

问题总结

PasswordEncoder 变化

直接使用原有代码报错:

passwordencoder mapped for the id null
复制代码
// 旧
@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}
 
// 新
@Bean
public PasswordEncoder passwordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

复制代码

在 Finchley 版本中, 出于安全性的原因,修改了PasswordEncoder 的生成和使用方法。
在注入bean 的时候不能显示指定PasswordEncoder的实现类,类比旧方法。只能通过工厂类来创建

PasswordEncoderFactories.createDelegatingPasswordEncoder();
复制代码

通过传入密码的特征码,动态去获取密码匹配器,这也就意味着保存在同一个库中密码可以使用多种加密方式。

{bcrypt}$2a$10$p0JC.ofpM8RxVTSubkKLDOUloGrQAX.lx/67HwnnyumATT69mwYm2
复制代码

第一部分为加密方式的特征码,支持的类型如上图,第二部分为密文。

附上官方文档介绍:https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated

RedisTokenStore bug

当授权Auth-Server 配置token 保存在redis 时,报了下面的错误。

NoSuchMethodError.RedisConnection.set([B[B)V #16
复制代码

Finchley.RELEASE 依赖的版本为 2.2.X版本。

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.2.X</version>
</dependency>
复制代码

升级到 2.3.3版本即可解决Redis操作问题

<!--spring security 、oauth、jwt依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-security</artifactId>
    <exclusions>
        <!--旧版本 redis操作有问题-->
        <exclusion>
            <artifactId>spring-security-oauth2</artifactId>
            <groupId>org.springframework.security.oauth</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
    <version>2.3.3.RELEASE</version>
</dependency>
复制代码

Spring Boot Admin 2.0.1

Spring Boot Admin 监控组件也发布了 兼容Finchley.RELEASE的 2.0.1版本,相较之前版本不同,当前版本需要和***spring security***配合使用 客户端:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码
/**
 * @author lengleng
 * @date 2018/6/22
 * 针对监控模块。全部放行
 */
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll()
            .and().csrf().disable();
    }
}
复制代码

详细使用我会再分享一篇关于 spring boot admin 2.0.X版本

写在最后

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值