重庆传智播客吧 关注:15贴子:75
  • 0回复贴,共1

认证鉴权与API权限控制在微服务架构中的

只看楼主收藏回复

概述在之前的系列文章认证鉴权与API权限控制在微服务架构中的设计与实现中,我们有四篇文章讲解了微服务下的认证鉴权与API权限控制的实现。当时基于的Spring Cloud版本为Dalston.SR4,当前最新的Spring Cloud版本为Finchley.SR1,对应的Spring Boot也升级到了2.0.x。Spring Cloud版本为Finchley和Spring Boot2.0相对之前的版本有较大的变化,至于具体的changes,请参见官网。本次会将项目升级到最新版本,下面具体介绍其中的变化。与使用之前的版本,请切换到1.0-RELEASE。升级依赖将Spring Boot的依赖升级为2.0.4.RELEASE。<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version></parent>复制代码升级dependencyManagement中的spring-cloud依赖为Finchley.RELEASE。<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>复制代码删除spring-cloud-starter-oauth2依赖,只留下spring-cloud-starter-security依赖。工具升级flyway我们在项目中,引入了flyway的依赖,用以初始化数据库的增量脚本,具体可以参见数据库版本管理工具Flyway应用。docker容器为了更加简便的体验本项目,笔者在项目中提供了docker compose脚本。在本地安装好docker compose的情况下,进入项目根目录执行docker-compose up命令。即可启动我们所需要的mysql和redis。Mybatis和HikariCP在Spring Boot 2.0.X版本中,选择了HikariCP作为默认数据库连接池。所以我们并不需要额外配置DataSource。Mybatis的mapper和config-location配置也通过配置文件的形式,因此DatasourceConfig大大简化。application.ymlspring: flyway: baseline-on-migrate: true locations: classpath:db datasource: hikari: connection-test-query: SELECT 1 minimum-idle: 1 maximum-pool-size: 5 pool-name: dbcp1 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/auth?autoReconnect=true&useSSL=false username: ${AUTH_DB_PWD:root} password: ${AUTH_DB_USER:_123456_}# schema[0]: classpath:/auth.sql# initialization-mode: ALWAYS type: com.zaxxer.hikari.HikariDataSource redis: database: 0 host: localhost port: 6379mybatis: mapper-locations: classpath:/mybatis/mapper/*Mapper.xml config-location: classpath:/mybatis/config.xml复制代码配置类升级AuthenticationManagerConfig弃用,由于循环依赖的问题,将AuthenticationManager的配置放置到WebSecurityConfig中。WebSecurityConfig添加了来自AuthenticationManagerConfig的AuthenticationManager配置。由于Spring Security5默认PasswordEncoder不是NoOpPasswordEncoder,需要手动指定。原来的auth项目中没有对密码进行加密,NoOpPasswordEncoder已经被废弃,只适合在测试环境中使用,本次我们使用SCryptPasswordEncoder密码加密器对密码进行加解密,更贴近产线的使用。其他的算法还有Pbkdf2PasswordEncoder和BCryptPasswordEncoder。关于Scrpyt算法,可以肯定的是其很难被攻击。Scrpyt算法是由著名的FreeBSD黑客 Colin Percival为他的备份服务 Tarsnap开发的,当初的设计是为了降低CPU负荷,尽量少的依赖cpu计算,利用CPU闲置时间进行计算,因此scrypt不仅计算所需时间长,而且占用的内存也多,使得并行计算多个摘要异常困难,因此利用rainbow table进行暴力攻击更加困难。Scrpyt没有在生产环境中大规模应用,并且缺乏仔细的审察和广泛的函数库支持。所以Scrpyt一直没有推广开,但是由于其内存依赖的设计特别符合当时对抗专业矿机的设计,成为数字货币算法发展的一个主要应用方向。而BCrypt相对出现的时间更久,也很安全。Spring Security中的BCryptPasswordEncoder方法采用SHA-256 + 随机盐 + 密钥对密码进行加密。SHA系列是Hash算法,不是加密算法,使用加密算法意味着可以解密(这个与编码/解码一样),但是采用Hash处理,其过程是不可逆的。加密(encode):注册用户时,使用SHA-256+随机盐+密钥把用户输入的密码进行hash处理,得到密码的hash值,然后将其存入数据库中。密码匹配(matches):用户登录时,密码匹配阶段并没有进行密码解密(因为密码经过Hash处理,是不可逆的),而是使用相同的算法把用户输入的密码进行hash处理,得到密码的hash值,然后将其与从数据库中查询到的密码hash值进行比较。如果两者相同,说明用户输入的密码正确。关于怎么初始化密码呢,和注册用户的时候怎么给密码加密,我们可以在初始化密码时调用如下的方法:SCryptPasswordEncoder sCryptPasswordEncoder = new SCryptPasswordEncoder();sCryptPasswordEncoder.encode("frontend");复制代码此时需要对数据库中的client_secret进行修改,如把frontend修改为:$e0801$65x9sjjnRPuKmqaFn3mICtPYnSWrjE7OB/pKzKTAI4ryhmVoa04cus+9sJcSAFKXZaJ8lcPO1I9H22TZk6EN4A==$o+ZWccaWXSA2t7TxE5VBRvz2W8psujU3RPPvejvNs4U=复制代码并修改配置如下: @autowired CustomAuthenticationProvider customAuthenticationProvider; @Autowired CodeAuthenticationProvider codeAuthenticationProvider; @override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(customAuthenticationProvider); auth.authenticationProvider(codeAuthenticationProvider); } @bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Bean public PasswordEncoder passwordEncoder(){ return new SCryptPasswordEncoder(); }复制代码ResourceServerConfig弃用,auth项目不启用资源服务器的功能。OAuth2Config由于当前版本的spring-boot-redis中的RedisConnection缺少#set方法,直接使用RedisTokenStore会出现以下异常:java.lang.NoSuchMethodError: org.springframework.data.redis.connection.RedisConnection.set([B[B)V复制代码因此自定义CustomRedisTokenStore类,与RedisTokenStore代码一致,只是将RedisConnection#set方法的调用替换为RedisConnection#stringCommands#set,如下所示: conn.stringCommands().set(accessKey, serializedAccessToken); conn.stringCommands().set(authKey, serializedAuth); conn.stringCommands().set(authToAccessKey, serializedAccessToken);复制代码完整代码见文末的GitHub地址。结果验证经过如上的升级改造,我们将验证如下的API端点:password模式获取token:/oauth/token?grant_type=password刷新token:/oauth/token?grant_type=refresh_token&refresh_token=...检验token:/oauth/check_token登出:/logout授权:/oauth/authorize授权码模式获取token:/oauth/token?grant_type=authorization_code结果就不展示了,都可以正常使用。小结OAuth鉴权服务是微服务架构中的一个基础服务,项目公开之后得到了好多同学的关注,好多同学在加入QQ群之后也提出了自己关于这方面的疑惑或者建议,一起讨论和解决疑惑的地方。随着Spring Boot和Spring Cloud的版本升级,笔者也及时更新了本项目,希望能够帮到一些童鞋。笔者筹划的一本关于Spring Cloud应用的书籍,本月即将出版面世,其中关于Spring Cloud Security部分,有着详细的解析,各位同学可以支持一下正版。


1楼2018-09-14 09:04回复