查看原文
其他

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

背景

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

  1. 基于Spring Boot 2.0.X

  2. 不兼容 Spring Boot 1.5.X

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

目标

基于现有Spring Cloud 脚手架

pig

开始动手升级。

关于pig:

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

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

版本变化

  1.                 +------------------+

  2.                 |                  |

  3.                 |  1.5.12.RELEASE  |

  4.                 |                  |

  5.                 +--------+---------+

  6.                          |

  7. Spring Boot               |

  8.                          v

  9.                 +------------------+

  10.                 |                  |

  11.                 |  2.0.3.RELEASE   |

  12.                 |                  |

  13.                 +------------------+

  1.                 +------------------+

  2.                 |                  |

  3.                 |  Edgware.SR3     |

  4.                 |                  |

  5.                 +--------+---------+

  6.                          |

  7. Srping Cloud              |

  8.                          v

  9.                 +------------------+

  10.                 |                  |

  11.                 |  Finchley.RELEASE|

  12.                 |                  |

  13.                 +------------------+

问题总结

PasswordEncoder 变化

直接使用原有代码报错:

  1. passwordencoder mapped for the id null

  1. // 旧

  2. @Bean

  3. public PasswordEncoder passwordEncoder() {

  4.    return new BCryptPasswordEncoder();

  5. }

  6. // 新

  7. @Bean

  8. public PasswordEncoder passwordEncoder() {

  9.    return PasswordEncoderFactories.createDelegatingPasswordEncoder();

  10. }

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

  1. PasswordEncoderFactories.createDelegatingPasswordEncoder();

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

  1. {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 时,报了下面的错误。

  1. NoSuchMethodError.RedisConnection.set([B[B)V #16

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

  1. <dependency>

  2.    <groupId>org.springframework.security.oauth</groupId>

  3.    <artifactId>spring-security-oauth2</artifactId>

  4.    <version>2.2.X</version>

  5. </dependency>

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

  1. <!--spring security 、oauth、jwt依赖-->

  2. <dependency>

  3.    <groupId>org.springframework.cloud</groupId>

  4.    <artifactId>spring-cloud-starter-security</artifactId>

  5.    <exclusions>

  6.        <!--旧版本 redis操作有问题-->

  7.        <exclusion>

  8.            <artifactId>spring-security-oauth2</artifactId>

  9.            <groupId>org.springframework.security.oauth</groupId>

  10.        </exclusion>

  11.    </exclusions>

  12. </dependency>

  13. <dependency>

  14.    <groupId>org.springframework.security.oauth</groupId>

  15.    <artifactId>spring-security-oauth2</artifactId>

  16.    <version>2.3.3.RELEASE</version>

  17. </dependency>

Spring Boot Admin 2.0.1

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

  1. <dependency>

  2.    <groupId>de.codecentric</groupId>

  3.    <artifactId>spring-boot-admin-starter-client</artifactId>

  4.    <version>2.0.1</version>

  5. </dependency>

  6. <dependency>

  7.    <groupId>org.springframework.boot</groupId>

  8.    <artifactId>spring-boot-starter-security</artifactId>

  9. </dependency>

  1. /**

  2. * @author lengleng

  3. * @date 2018/6/22

  4. * 针对监控模块。全部放行

  5. */

  6. @Configuration

  7. public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {

  8.    @Override

  9.    protected void configure(HttpSecurity http) throws Exception {

  10.        http.authorizeRequests().anyRequest().permitAll()

  11.            .and().csrf().disable();

  12.    }

  13. }

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

写在最后

  • 其他的升级步骤还挺顺风顺水,多看一下官方的文档即可。 http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/

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

  • 求求你别更新了,学不完了!



推荐:Dubbo x Cloud Native 服务架构长文总结(很全)

上一篇:译:微服务架构10条最佳实践

关注公众号




    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存