微服务下统一认证解决方案 Spring Cloud OAuth2 + JWT

逃离我推掉我的手 2022-11-05 13:58 719阅读 0赞

微服务架构下统⼀认证思路

传统的服务认证方案有Session(基于cookie),以及token等方案。

  • 基于Session的认证⽅式
    在分布式的环境下,基于session的认证会出现⼀个问题,每个应⽤服务都需要在session中存储⽤户身份信息,通过负载均衡将本地的请求分配到另⼀个应⽤服务需要将session信息带过去,否则会重新认证。我们可以使⽤Session共享、Session黏贴等⽅案。
    Session⽅案也有缺点,⽐如基于cookie,移动端不能很好的使⽤。
  • 基于token的认证⽅式

基于token的认证⽅式,服务端不⽤存储认证数据,易维护扩展性强, 客户端可以把token 存在任意地⽅,并且可以实现web和app统⼀认证机制。其缺点也很明显,token由于⾃包含信息,因此⼀般数据量较⼤,⽽且每次请求 都需要传递,因此⽐较占带宽。另外,token的签名验签操作也会给cpu带来额外的处理负担。

微服务下统一认证解决方案 Spring Cloud OAuth2 + JWT

OAuth2开放授权协议/标准

OAuth(开放授权)是⼀个开放协议/标准,允许⽤户授权第三⽅应⽤访问他们存储在另外的服务提供者上的信息,⽽不需要将⽤户名和密码提供给第三⽅应⽤或分享他们数据的所有内容。

允许⽤户授权第三⽅应⽤访问他们存储在另外的服务提供者上的信息,⽽不需要将⽤户名和密码提供给第三⽅应⽤或分享他们数据的所有内容

OAuth2协议⻆⾊和流程

Boss直聘需要开发使⽤QQ登录这个功能、Boss直聘是需要到QQ平台进⾏登记注册的;

在这里插入图片描述

大致流程是:当我们使用 Boss直聘,开始登陆,然后请求到QQ平台,QQ通过之后将一些参数返回给Boss,然后进行授权登陆,会涉及到这么几个参数。

client_id :客户端id(QQ 相当于⼀个认证授权服务器,Boss就相当于⼀个客户端了,所以会给⼀个客户端id),相当于账号,secret:相当于密码。

  • 资源所有者(Resource Owner):可以理解为⽤户⾃⼰
  • 客户端(Client):我们想登陆的⽹站或应⽤,⽐如boss,一些认证登录的网站。
  • 认证服务器(Authorization Server):可以理解为微信或者QQ
  • 资源服务器(Resource Server):可以理解为微信或者QQ

什么情况下需要使⽤OAuth2?

一些第三方登陆场景: 我们不想注册自己账号,比如可以微信,QQ等授权登陆,是典型的 OAuth2 使⽤场景。

单点登录的场景:如果项⽬中有很多微服务或者公司内部有很多服务,可以专⻔做⼀个认证中⼼(充当认证平台⻆⾊),所有的服务都要到这个认证中⼼做认证,只做⼀次登录,就可以在多个授权范围内的服务中⾃由串⾏。

OAuth2的颁发Token授权⽅式

  • 1)授权码(authorization-code)
  • 2)密码式(password)提供⽤户名+密码换取token令牌
  • 3)隐藏式(implicit)
  • 4)客户端凭证(client credentials)

授权码模式使⽤到了回调地址,是最复杂的授权⽅式,微博、微信、QQ等第三⽅登录就是这种模式。

微服务这儿使用接⼝对接中常使⽤的password密码模式(提供⽤户名+密码换取token)。

Spring Cloud OAuth2 + JWT 实现

Spring Cloud OAuth2是什么?

Spring Cloud OAuth2 是 Spring Cloud 体系对OAuth2协议的实现,可以⽤来做多个微服务的统⼀认证(验证身份合法性)授权(验证权限)。通过向OAuth2服务(统⼀认证授权服务)发送某个类型的grant_type进⾏集中认证和授权,从⽽获得access_token(访问令牌),⽽这个token是受其他微服务信任的。

OAuth2解决问题的本质是,引⼊了⼀个认证授权层,认证授权层连接了资源的拥有者,在授权层⾥⾯,资源的拥有者可以给第三⽅应⽤授权去访问我们的某些受保护资源。

Spring Cloud OAuth2构建微服务统⼀认证服务思路
在这里插入图片描述

在我们统⼀认证的场景中,Resource Server其实就是我们的各种受保护的微服务,微服务中的各种API访问接⼝就是资源,发起http请求的浏览器就是Client客户端(对应为第三⽅应⽤)

具体操作需要搭建一个认证服务,主要用来通过用户的一些信息,生成token,颁发token给客户端,
一个资源服务,用来对外提供资源访问,同时像认证服务去请求校验所携带的token令牌。

新建认证服务 cloud-oauth-server-9999

引入依赖

  1. <!--导⼊Eureka Client依赖-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <!--导⼊spring cloud oauth2依赖-->
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-oauth2</artifactId>
  10. <exclusions>
  11. <exclusion>
  12. <groupId>org.springframework.security.oauth.boot</groupId>
  13. <artifactId>spring-security-oauth2-autoconfigure
  14. </artifactId>
  15. </exclusion>
  16. </exclusions>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.springframework.security.oauth.boot</groupId>
  20. <artifactId>spring-security-oauth2-autoconfigure
  21. </artifactId>
  22. <version>2.1.11.RELEASE</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.security.oauth</groupId>
  26. <artifactId>spring-security-oauth2</artifactId>
  27. <version>2.3.4.RELEASE</version>
  28. </dependency>

yml配置 将其注册到注册中心

  1. #eureka server服务端口
  2. server:
  3. port: 9999
  4. spring:
  5. application:
  6. name: cloud-oauth-server-9999 # 应用名称,应用名称会在Eureka中作为服务名称
  7. cloud:
  8. inetutils:
  9. # 指定此客户端的ip
  10. default-ip-address: springcloud
  11. #eureka配置
  12. eureka:
  13. instance:
  14. hostname: springcloud # 当前eureka实例的主机名
  15. ip-address: springcloud
  16. client:
  17. service-url:
  18. prefer-ip-address: true
  19. lease-renewal-interval-in-seconds: 30
  20. # 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发⽣⼼跳,服务注册中心会将服务从列表移除
  21. lease-expiration-duration-in-seconds: 90
  22. # 配置客户端所交互的Eureka Server的地址(Eureka Server集群中每一个Server其实相对于其它Server来说都是Client)
  23. # 集群模式下,defaultZone应该指向其它Eureka Server,如果有更多其它Server实例,逗号拼接即可
  24. defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka # 注册到集群汇总,多个用,拼接
  25. register-with-eureka: true # 集群模式下可以改成true
  26. fetch-registry: true # 集群模式下可以改成true

新建我们的认证服务配置

这儿需要继承父类AuthorizationServerConfigurerAdapter然后重写配置

  1. package com.udeam;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  7. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  8. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  9. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  10. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  11. import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
  12. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  13. import org.springframework.security.oauth2.provider.token.TokenStore;
  14. import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  15. /** * 认证服务配置类 * 当前类为Oauth2 server的配置类(需要继承特定的⽗类 AuthorizationServerConfigurerAdapter) */
  16. @EnableAuthorizationServer //开启认证服务器功能
  17. @Configuration
  18. public class OauthServerConfiger extends AuthorizationServerConfigurerAdapter {
  19. @Autowired
  20. private AuthenticationManager authenticationManager;
  21. /** * 认证服务器最终是以api接⼝的⽅式对外提供服务(校验合法性并⽣成令牌、 * 校验令牌等) * 那么,以api接⼝⽅式对外的话,就涉及到接⼝的访问权限,我们需要在这⾥ * 进⾏必要的配置 * * @param security * @throws Exception */
  22. @Override
  23. public void configure(AuthorizationServerSecurityConfigurer
  24. security) throws Exception {
  25. super.configure(security);
  26. // 相当于打开endpoints 访问接⼝的开关,这样的话后期我们能够访问该接⼝
  27. security
  28. // 允许客户端表单认证
  29. .allowFormAuthenticationForClients()
  30. // 开启端⼝/oauth/token_key的访问权限(允许)
  31. .tokenKeyAccess("permitAll()")
  32. // 开启端⼝/oauth/check_token的访问权限(允许)
  33. .checkTokenAccess("permitAll()");
  34. }
  35. /** * 客户端详情配置, * ⽐如client_id,secret 当前这个服务就如同QQ平台,boss⽹作为客户端需要qq平台进⾏登录授权认证等,提前需要到QQ平台注册,QQ平台会给boss⽹ * 颁发client_id等必要参数,表明客户端是谁 */
  36. @Override
  37. public void configure(ClientDetailsServiceConfigurer
  38. clients) throws Exception {
  39. super.configure(clients);
  40. clients.inMemory()// 客户端信息存储在什么地⽅,可以在内存中,可 以在数据库⾥
  41. .withClient("client_test") // 添加⼀个client配 置,指定其client_id
  42. .secret("abcxyz") // 指定客户端 的密码 / 安全码
  43. .resourceIds("cloud-oauth-server-9998") // 指定客户端 所能访问资源id清单,此处的资源id是需要在具体的资源服务器上也配置⼀样
  44. // 认证类型/令牌颁发模式,可以配置多个在这⾥,但是不⼀定都⽤,具体使⽤哪种⽅式颁发token,需要客户端调⽤的时候传递参数指定
  45. .authorizedGrantTypes("password", "refresh_token")
  46. // 客户端的权限范围,此处配置为all全部即可
  47. .scopes("all");
  48. }
  49. /** * 认证服务器是玩转token的,那么这⾥配置token令牌管理相关(token此时 * 就是⼀个字符串,当下的token需要在服务器端存储, * 那么存储在哪⾥呢?都是在这⾥配置) * * @param endpoints * @throws Exception */
  50. @Override
  51. public void configure(AuthorizationServerEndpointsConfigurer
  52. endpoints) throws Exception {
  53. super.configure(endpoints);
  54. endpoints
  55. .tokenStore(tokenStore()) // 指定token的存储⽅法
  56. .tokenServices(authorizationServerTokenServices()) // token服 务的⼀个描述,可以认为是token⽣成细节的描述,⽐如有效时间多少等
  57. .authenticationManager(authenticationManager) // 指定认证管理器,随后注⼊⼀个到当前类使⽤即可
  58. .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
  59. }
  60. /* 该⽅法⽤于创建tokenStore对象(令牌存储对象)token以什么形式存储 */
  61. public TokenStore tokenStore() {
  62. return new InMemoryTokenStore();
  63. }
  64. /** * 该⽅法⽤户获取⼀个token服务对象(该对象描述了token有效期等信息) */
  65. public AuthorizationServerTokenServices authorizationServerTokenServices() {
  66. // 使⽤默认实现
  67. DefaultTokenServices defaultTokenServices = new
  68. DefaultTokenServices();
  69. defaultTokenServices.setSupportRefreshToken(true); // 是否开启令牌刷新
  70. defaultTokenServices.setTokenStore(tokenStore());
  71. // 设置令牌有效时间(⼀般设置为2个⼩时)
  72. defaultTokenServices.setAccessTokenValiditySeconds(200); //这儿设置200s
  73. // access_token就是我们请求资源需要携带的令牌
  74. // 设置刷新令牌的有效时间
  75. defaultTokenServices.setRefreshTokenValiditySeconds(259200); //3 天
  76. return defaultTokenServices;
  77. }
  78. }
关于三个configure⽅法
  • configure(ClientDetailsServiceConfigurer clients)

⽤来配置客户端详情服务(ClientDetailsService),客户端详情信息在 这⾥进⾏初始化,你能够把客户端详情信息写死在这⾥或者是通过数据库来存储调取详情信息

  • configure(AuthorizationServerEndpointsConfigurer endpoints)

⽤来配置令牌(token)的访问端点和令牌服务(token services)

  • configure(AuthorizationServerSecurityConfigureroauthServer)

⽤来配置令牌端点的安全约束.

关于 TokenStore
  • InMemoryTokenStore

默认采⽤,它可以完美的⼯作在单服务器上(即访问并发量 压⼒不⼤的情况下,并且它在失败的时候不会进⾏备份),⼤多数的项⽬都可以使⽤这个版本的实现来进⾏ 尝试,你可以在开发的时候使⽤它来进⾏管理,因为不会被保存到磁盘中,所以更易于调试。

  • JdbcTokenStore

这是⼀个基于JDBC的实现版本,令牌会被保存进关系型数据库。使⽤这个版本的实现时, 你可以在不同的服务器之间共享令牌信息,使⽤这个版本的时候请注意把”spring-jdbc”这个依赖加⼊到你的 classpath当中。

  • JwtTokenStore

这个版本的全称是 JSON Web Token(JWT),它可以把令牌相关的数据进⾏编码(因此对于后端服务来说,它不需要进⾏存储,这将是⼀个重⼤优势),缺点就是这个令牌占⽤的空间会⽐较⼤,如果你加⼊了⽐较多⽤户凭证信息,JwtTokenStore 不会保存任何数据。

配置生成验证用户名和生成token的配置

  1. package com.udeam;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  7. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  8. import org.springframework.security.core.userdetails.User;
  9. import org.springframework.security.core.userdetails.UserDetails;
  10. import org.springframework.security.crypto.password.NoOpPasswordEncoder;
  11. import org.springframework.security.crypto.password.PasswordEncoder;
  12. import java.util.ArrayList;
  13. /** * 该配置类,主要处理⽤户名和密码的校验等事宜 */
  14. @Configuration
  15. public class SecurityConfiger extends WebSecurityConfigurerAdapter {
  16. /** * 注册⼀个认证管理器对象到容器 */
  17. @Bean
  18. @Override
  19. public AuthenticationManager authenticationManagerBean()
  20. throws Exception {
  21. return super.authenticationManagerBean();
  22. }
  23. /** * 密码编码对象(密码不进⾏加密处理) * * @return */
  24. @Bean
  25. public PasswordEncoder passwordEncoder() {
  26. return NoOpPasswordEncoder.getInstance();
  27. }
  28. @Autowired
  29. private PasswordEncoder passwordEncoder;
  30. /** * 处理⽤户名和密码验证事宜 * 1)客户端传递username和password参数 到认证服务器 * 2)⼀般来说,username和password会存储在数据库中的⽤户表中 * 3)根据⽤户表中数据,验证当前传递过来的⽤户信息的合法性 */
  31. @Override
  32. protected void configure(AuthenticationManagerBuilder auth)
  33. throws Exception {
  34. // 在这个⽅法中就可以去关联数据库了,当前我们先把⽤户信息配置在内存中
  35. // 实例化⼀个⽤户对象(相当于数据表中的⼀条⽤户记录)
  36. UserDetails user = new User("admin", "admin", new
  37. ArrayList<>());
  38. auth.inMemoryAuthentication()
  39. .withUser(user).passwordEncoder(passwordEncoder);
  40. }
  41. }

这儿先写死配置,以及将生成的token存放在内存中。

测试

获取token:

http://localhost:9999/oauth/token?client\_secret=abcxyz&grant\_type=password&username=admin&password=123456&client\_id=client\_test

在这里插入图片描述

参数说明

endpoint:/oauth/token

获取token携带的参数

  • client_id:客户端id
  • client_secret:客户单密码
  • grant_type:指定使⽤哪种颁发类型,password
  • username:⽤户名
  • password:密码

校验token:

http://localhost:9999/oauth/check\_token?token=a9979518-838c-49ff-b14a-ebdb7fde7d08

刷新token:

http://localhost:9999/oauth/token?grant\_type=refresh\_token&client\_id=client\_lagou&client\_secret=abcxyz&refresh\_token=8b640340-30a3-4307-93d4-ed60cc54fbc8

新建资源服务器ResourceTestServer

希望访问被认证的微服务
Resource Server配置

pom和认证的一样

yml基本也一样

这儿我们提供/api/test 以及 /demo/test接口用来测试携带token是否可以访问

在这里插入图片描述
在这里插入图片描述

配置资源服务配置类

配置需要检验的url
配置token认证的服务

  1. package com;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.http.SessionCreationPolicy;
  6. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  7. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  8. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  9. import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
  10. /** * * * 资源服务配置类 */
  11. @Configuration
  12. @EnableResourceServer // 开启资源服务器功能
  13. @EnableWebSecurity // 开启web访问安全
  14. public class ResourceServerConfiger extends ResourceServerConfigurerAdapter {
  15. private String sign_key = "test123"; // jwt签名密钥
  16. /** * 该⽅法⽤于定义资源服务器向远程认证服务器发起请求,进⾏token校验 * 等事宜 * * @param resources * @throws Exception */
  17. @Override
  18. public void configure(ResourceServerSecurityConfigurer
  19. resources) throws Exception {
  20. // 设置当前资源服务的资源id 与认证id保持一致 可以不设置
  21. resources.resourceId("cloud-oauth-server-9998");
  22. // 定义token服务对象(token校验就应该靠token服务对象)
  23. RemoteTokenServices remoteTokenServices = new
  24. RemoteTokenServices();
  25. // 校验端点/接⼝设置
  26. remoteTokenServices.setCheckTokenEndpointUrl("http://localhost:9999/oauth/check_token");
  27. // 携带客户端id和客户端安全码
  28. remoteTokenServices.setClientId("client_test");
  29. remoteTokenServices.setClientSecret("abcxyz");
  30. // 别忘了这⼀步
  31. resources.tokenServices(remoteTokenServices);
  32. }
  33. /** * 场景:⼀个服务中可能有很多资源(API接⼝) * 某⼀些API接⼝,需要先认证,才能访问 * 某⼀些API接⼝,压根就不需要认证,本来就是对外开放的接⼝ * 我们就需要对不同特点的接⼝区分对待(在当前configure⽅法中 * 完成),设置是否需要经过认证 * * @param http * @throws Exception */
  34. @Override
  35. public void configure(HttpSecurity http) throws
  36. Exception {
  37. http // 设置session的创建策略(根据需要创建即可)
  38. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
  39. .and()
  40. .authorizeRequests()
  41. .antMatchers("/api/**").authenticated() // autodeliver为前缀的请求需要认证
  42. .antMatchers("/demo/**").authenticated() // demo为前缀的请求需要认证
  43. .anyRequest().permitAll(); // 其他请求不认证
  44. }
  45. }

测试

当我们不携带token访问的时候 直接会报没有认证权限

在这里插入图片描述

在这里插入图片描述

当我们携带token之后可以看到 正确返回信息

在这里插入图片描述

一个接口请求认证校验流程在认证服务和资源服务之间测试流程结束
因为涉及到微服务,此时我们的微服务只有一个认证个一个资源服务,在分布式架构中,资源服务往往有很多个,认证服务器请求压力剧增,占用很多资源,验证可能导致认证服务出现故障,最终导致我们服务甚至不可用。

如果我们将认证服务器生成的token让客户端存储,每次请求避免与认证服务器交互。

JWT改造统⼀认证授权中⼼的令牌存储机制

JWT令牌介绍

  • 什么是JWT?

JSON Web Token(JWT)是⼀个开放的⾏业标准(RFC 7519),它定义了⼀种简介
的、⾃包含的协议格式,⽤于 在通信双⽅传递json对象,传递的信息经过数字签名
可以被验证和信任。JWT可以使⽤HMAC算法或使⽤RSA的公 钥/私钥对来签名,防
⽌被篡改。

  • JWT令牌结构

JWT令牌由三部分组成,每部分中间使⽤点(.)分隔,⽐如:xxxxx.yyyyy.zzzzz
Header

头部包括令牌的类型(即JWT)及使⽤的哈希算法(如HMAC SHA256或RSA),例如

  1. "alg": "HS256",
  2. "typ": "JWT"
  3. }

将上边的内容使⽤Base64Url编码,得到⼀个字符串就是JWT令牌的第⼀部分。

  • Payload

第⼆部分是负载,内容也是⼀个json对象,它是存放有效信息的地⽅,它可以存
放jwt提供的现成字段,⽐ 如:iss(签发者),exp(过期时间戳), sub(⾯向的
⽤户)等,也可⾃定义字段。 此部分不建议存放敏感信息,因为此部分可以解
码还原原始内容。 最后将第⼆部分负载使⽤Base64Url编码,得到⼀个字符串
就是JWT令牌的第⼆部分。 ⼀个例⼦:

  1. {
  2. "sub": "1234567890",
  3. "name": "John Doe",
  4. "iat": 1516239022
  5. }
  • Signature

第三部分是签名,此部分⽤于防⽌jwt内容被篡改。 这个部分使⽤base64url将
前两部分进⾏编码,编码后使⽤点(.)连接组成字符串,最后使⽤header中声
明 签名算法进⾏签名。

  1. HMACSHA256(
  2. base64UrlEncode(header) + "." +
  3. base64UrlEncode(payload),
  4. secret)
  5. base64UrlEncode(header):jwt令牌的第⼀部分。
  6. base64UrlEncode(payload):jwt令牌的第⼆部分。
  7. secret:签名所使⽤的密钥。
  8. 认证服务器端JWT改造(改造主配置类)
  9. /* 该⽅法⽤于创建tokenStore对象(令牌存储对象) token以什么形式存储 */
  10. public TokenStore tokenStore(){
  11. //return new InMemoryTokenStore();
  12. // 使⽤jwt令牌
  13. return new JwtTokenStore(jwtAccessTokenConverter());
  14. }
  15. /** * 返回jwt令牌转换器(帮助我们⽣成jwt令牌的) * 在这⾥,我们可以把签名密钥传递进去给转换器对象 * @return */
  16. public JwtAccessTokenConverter jwtAccessTokenConverter() {
  17. JwtAccessTokenConverter jwtAccessTokenConverter = new
  18. JwtAccessTokenConverter();
  19. jwtAccessTokenConverter.setSigningKey(sign_key); // 签名密
  20. jwtAccessTokenConverter.setVerifier(new
  21. MacSigner(sign_key)); // 验证时使⽤的密钥,和签名密钥保持⼀致
  22. return jwtAccessTokenConverter;
  23. }

认证服务器端JWT 代码改造

将生成token的代码进行改造,添加jwt

  1. package com.jwt;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.security.authentication.AuthenticationManager;
  6. import org.springframework.security.jwt.crypto.sign.MacSigner;
  7. import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  8. import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  10. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  11. import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
  12. import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
  13. import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
  14. import org.springframework.security.oauth2.provider.token.TokenStore;
  15. import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
  16. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  17. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  18. /** * 认证服务配置类 * 当前类为Oauth2 server的配置类(需要继承特定的⽗类 AuthorizationServerConfigurerAdapter) */
  19. @EnableAuthorizationServer //开启认证服务器功能
  20. @Configuration
  21. public class OauthServerConfiger extends AuthorizationServerConfigurerAdapter {
  22. @Autowired
  23. private AuthenticationManager authenticationManager;
  24. private String sign_key = "test123";
  25. /** * 认证服务器最终是以api接⼝的⽅式对外提供服务(校验合法性并⽣成令牌、 * 校验令牌等) * 那么,以api接⼝⽅式对外的话,就涉及到接⼝的访问权限,我们需要在这⾥ * 进⾏必要的配置 * * @param security * @throws Exception */
  26. @Override
  27. public void configure(AuthorizationServerSecurityConfigurer
  28. security) throws Exception {
  29. super.configure(security);
  30. // 相当于打开endpoints 访问接⼝的开关,这样的话后期我们能够访问该接⼝
  31. security
  32. // 允许客户端表单认证
  33. .allowFormAuthenticationForClients()
  34. // 开启端⼝/oauth/token_key的访问权限(允许)
  35. .tokenKeyAccess("permitAll()")
  36. // 开启端⼝/oauth/check_token的访问权限(允许)
  37. .checkTokenAccess("permitAll()");
  38. }
  39. /** * 客户端详情配置, * ⽐如client_id,secret * @param clients * @throws Exception */
  40. @Override
  41. public void configure(ClientDetailsServiceConfigurer
  42. clients) throws Exception {
  43. super.configure(clients);
  44. clients.inMemory()// 客户端信息存储在什么地⽅,可以在内存中,可 以在数据库⾥
  45. .withClient("client_test") // 添加⼀个client配 置,指定其client_id
  46. .secret("abcxyz") // 指定客户端 的密码 / 安全码
  47. .resourceIds("cloud-oauth-server-9998") // 指定客户端 所能访问资源id清单,此处的资源id是需要在具体的资源服务器上也配置⼀样
  48. // 认证类型/令牌颁发模式,可以配置多个在这⾥,但是不⼀定都⽤,具体使⽤哪种⽅式颁发token,需要客户端调⽤的时候传递参数指定
  49. .authorizedGrantTypes("password", "refresh_token")
  50. // 客户端的权限范围,此处配置为all全部即可
  51. .scopes("all");
  52. }
  53. /** * 认证服务器是玩转token的,那么这⾥配置token令牌管理相关(token此时 * 就是⼀个字符串,当下的token需要在服务器端存储, * 那么存储在哪⾥呢?都是在这⾥配置) * * @param endpoints * @throws Exception */
  54. @Override
  55. public void configure(AuthorizationServerEndpointsConfigurer
  56. endpoints) throws Exception {
  57. super.configure(endpoints);
  58. endpoints
  59. .tokenStore(tokenStore()) // 指定token的存储⽅法
  60. .tokenServices(authorizationServerTokenServices()) // token服 务的⼀个描述,可以认为是token⽣成细节的描述,⽐如有效时间多少等
  61. .authenticationManager(authenticationManager) // 指定认证管理器,随后注⼊⼀个到当前类使⽤即可
  62. .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
  63. }
  64. /** 该⽅法⽤于创建tokenStore对象(令牌存储对象) token以什么形式存储 */
  65. public TokenStore tokenStore(){
  66. //return new InMemoryTokenStore();
  67. // 使⽤jwt令牌
  68. return new JwtTokenStore(jwtAccessTokenConverter());
  69. }
  70. /** * 返回jwt令牌转换器(帮助我们⽣成jwt令牌的) * 在这⾥,我们可以把签名密钥传递进去给转换器对象 * @return */
  71. public JwtAccessTokenConverter jwtAccessTokenConverter() {
  72. JwtAccessTokenConverter jwtAccessTokenConverter = new
  73. JwtAccessTokenConverter();
  74. jwtAccessTokenConverter.setSigningKey(sign_key); // 签名密钥
  75. jwtAccessTokenConverter.setVerifier(new
  76. MacSigner(sign_key)); // 验证时使⽤的密钥,和签名密钥保持⼀致
  77. return jwtAccessTokenConverter;
  78. }
  79. /** * 该⽅法⽤户获取⼀个token服务对象(该对象描述了token有效期等信息) */
  80. public AuthorizationServerTokenServices authorizationServerTokenServices() {
  81. // 使⽤默认实现
  82. DefaultTokenServices defaultTokenServices = new
  83. DefaultTokenServices();
  84. /** * 添加使用jwt令牌 */
  85. defaultTokenServices.setTokenEnhancer(jwtAccessTokenConverter());
  86. defaultTokenServices.setSupportRefreshToken(true); // 是否开启令牌刷新
  87. defaultTokenServices.setTokenStore(tokenStore());
  88. // 设置令牌有效时间(⼀般设置为2个⼩时)
  89. defaultTokenServices.setAccessTokenValiditySeconds(200);
  90. // access_token就是我们请求资源需要携带的令牌
  91. // 设置刷新令牌的有效时间
  92. defaultTokenServices.setRefreshTokenValiditySeconds(259200); //3 天
  93. return defaultTokenServices;
  94. }
  95. }

资源服务

资源服务器校验JWT令牌,不需要和远程认证服务器交互,添加本地tokenStore。

  1. package com;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.http.SessionCreationPolicy;
  6. import org.springframework.security.jwt.crypto.sign.MacSigner;
  7. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  8. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  9. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  10. import org.springframework.security.oauth2.provider.token.TokenStore;
  11. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  12. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  13. /** * * 资源服务配置类 使用jwt 校验 */
  14. @Configuration
  15. @EnableResourceServer // 开启资源服务器功能
  16. @EnableWebSecurity // 开启web访问安全
  17. public class ResourceServerConfigerForJWT extends ResourceServerConfigurerAdapter {
  18. private String sign_key = "test123"; // jwt签名密钥
  19. /** * 该⽅法⽤于定义资源服务器向远程认证服务器发起请求,进⾏token校验 * 等事宜 * * @param resources * @throws Exception */
  20. @Override
  21. public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
  22. // 设置当前资源服务的资源id 与认证id保持一致 可以不设置
  23. resources.resourceId("cloud-oauth-server-9998")
  24. .tokenStore(tokenStore())
  25. .stateless(true);// ⽆状态设置
  26. }
  27. /** * 场景:⼀个服务中可能有很多资源(API接⼝) * 某⼀些API接⼝,需要先认证,才能访问 * 某⼀些API接⼝,压根就不需要认证,本来就是对外开放的接⼝ * 我们就需要对不同特点的接⼝区分对待(在当前configure⽅法中 * 完成),设置是否需要经过认证 * * @param http * @throws Exception */
  28. @Override
  29. public void configure(HttpSecurity http) throws
  30. Exception {
  31. http // 设置session的创建策略(根据需要创建即可)
  32. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
  33. .and()
  34. .authorizeRequests()
  35. .antMatchers("/api/**").authenticated() // autodeliver为前缀的请求需要认证
  36. .antMatchers("/demo/**").authenticated() // demo为前缀的请求需要认证
  37. .anyRequest().permitAll(); // 其他请求不认证
  38. }
  39. /** 该⽅法⽤于创建tokenStore对象(令牌存储对象)token以什么形式存储 */
  40. public TokenStore tokenStore(){
  41. //return new InMemoryTokenStore();
  42. // 使⽤jwt令牌
  43. return new JwtTokenStore(jwtAccessTokenConverter());
  44. }
  45. /** * 返回jwt令牌转换器(帮助我们⽣成jwt令牌的) * 在这⾥,我们可以把签名密钥传递进去给转换器对象 * @return */
  46. public JwtAccessTokenConverter jwtAccessTokenConverter() {
  47. JwtAccessTokenConverter jwtAccessTokenConverter = new
  48. JwtAccessTokenConverter();
  49. jwtAccessTokenConverter.setSigningKey(sign_key); // 签名密钥
  50. jwtAccessTokenConverter.setVerifier(new
  51. MacSigner(sign_key)); // 验证时使⽤的密钥,和签名密钥保持⼀致
  52. return jwtAccessTokenConverter;
  53. }
  54. }

需要注意的是认证和资源端的秘钥需要保持一致

测试

我们再请求认证服务器发现返回的token是 jwt令牌格式

在这里插入图片描述

使用令牌请求接口,请求成功
在这里插入图片描述

一般开发中我们的用户信息,以及客户端信息都是从数据库进行查询的。

客户端详情配置
  1. @Override
  2. public void configure(ClientDetailsServiceConfigurer
  3. clients) throws Exception {
  4. super.configure(clients);
  5. //从数据库中加载客户端详情
  6. clients.withClientDetails(createJdbcClientDetailsService());
  7. }
  8. /** * 从数据库中加载客户端详情 * @return */
  9. @Bean
  10. public JdbcClientDetailsService createJdbcClientDetailsService() {
  11. JdbcClientDetailsService jdbcClientDetailsService = new
  12. JdbcClientDetailsService(dataSource); //需要注入DataSource
  13. return jdbcClientDetailsService;
  14. }

可以看到这儿的客户端配置详情 有两个子类
在这里插入图片描述
clients.withClientDetails(createJdbcClientDetailsService()); 我们使用从数据库中查询,不适用内存的方式。

在子类实现类中可以看到 查询语句

在这里插入图片描述

在这里插入图片描述
this.selectClientDetailsSql 完整的Sql如下

  1. "select client_id, client_secret, resource_ids, scope, authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove from oauth_client_details where client_id = ?";

可以看到是通过client_idoauth_client_details表中查询,字段有以上select后面那些。

我们新建表oauth_client_details

  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for oauth_client_details
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `oauth_client_details`;
  7. CREATE TABLE `oauth_client_details` (
  8. `client_id` varchar(48) NOT NULL,
  9. `resource_ids` varchar(256) DEFAULT NULL,
  10. `client_secret` varchar(256) DEFAULT NULL,
  11. `scope` varchar(256) DEFAULT NULL,
  12. `authorized_grant_types` varchar(256) DEFAULT NULL,
  13. `web_server_redirect_uri` varchar(256) DEFAULT NULL,
  14. `authorities` varchar(256) DEFAULT NULL,
  15. `access_token_validity` int(11) DEFAULT NULL,
  16. `refresh_token_validity` int(11) DEFAULT NULL,
  17. `additional_information` varchar(4096) DEFAULT NULL,
  18. `autoapprove` varchar(256) DEFAULT NULL,
  19. PRIMARY KEY (`client_id`)
  20. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  21. -- ----------------------------
  22. -- Records of oauth_client_details
  23. -- ----------------------------
  24. BEGIN;
  25. INSERT INTO `oauth_client_details` VALUES ('client_test',
  26. 'test,resume', 'abcxyz', 'all', 'password,refresh_token',
  27. NULL, NULL, 7200, 259200, NULL, NULL);
  28. COMMIT;
  29. SET FOREIGN_KEY_CHECKS = 1;

resource_ids 多个服务可以使用逗号进行拼接

然后代码会自动为我们进行查询匹配的。

用户查询配置

用户表

  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. -- ----------------------------
  4. -- Table structure for users
  5. -- ----------------------------
  6. DROP TABLE IF EXISTS `users`;
  7. CREATE TABLE `users` (
  8. `id` int(11) NOT NULL AUTO_INCREMENT,
  9. `username` char(10) DEFAULT NULL,
  10. `password` char(100) DEFAULT NULL,
  11. PRIMARY KEY (`id`)
  12. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
  13. -- ----------------------------
  14. -- Records of users
  15. -- ----------------------------
  16. BEGIN;
  17. INSERT INTO `users` VALUES (4, 'admin', 'iuxyzds');
  18. COMMIT;
  19. SET FOREIGN_KEY_CHECKS = 1;

用户代码查询

在认证配置SecurityConfiger 类中

  1. @Autowired
  2. private JdbcUserDetailsService jdbcUserDetailsService;
  3. /** * 处理⽤户名和密码验证事宜 * 1)客户端传递username和password参数到认证服务器 * 2)⼀般来说,username和password会存储在数据库中的⽤户表中 * 3)根据⽤户表中数据,验证当前传递过来的⽤户信息的合法性 */
  4. @Override
  5. protected void configure(AuthenticationManagerBuilder auth) throws
  6. Exception {
  7. // 在这个⽅法中就可以去关联数据库了,当前我们先把⽤户信息配置在内存中
  8. // 实例化⼀个⽤户对象(相当于数据表中的⼀条⽤户记录)
  9. /*UserDetails user = new User("admin","123456",new ArrayList<> ()); auth.inMemoryAuthentication() .withUser(user).passwordEncoder(passwordEncoder);*/
  10. auth.userDetailsService(jdbcUserDetailsService).passwordEncoder(passwordEncoder);
  11. }

这儿我们使用jpa 根据用户名从db查询 用户详情。

serives

  1. @Service
  2. public class JdbcUserDetailsService implements UserDetailsService {
  3. @Autowired
  4. private UsersRepository usersRepository;
  5. /** * 根据username查询出该用户的所有信息,封装成UserDetails类型的对象返回,至于密码,框架会自动匹配 * @param username * @return * @throws UsernameNotFoundException */
  6. @Override
  7. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  8. Users users = usersRepository.findByUsername(username);
  9. return new User(users.getUsername(),users.getPassword(),new ArrayList<>());
  10. }
  11. }

dao

  1. public interface UsersRepository extends JpaRepository<Users,Long> {
  2. Users findByUsername(String username);
  3. }

依赖

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>com.alibaba</groupId>
  7. <artifactId>druid-spring-boot-starter</artifactId>
  8. <version>1.1.10</version>
  9. </dependency>
  10. <!--操作数据库需要事务控制-->
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-tx</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-jdbc</artifactId>
  18. </dependency>
  19. <!-- jpa-->
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>

yml配置

  1. spring:
  2. application:
  3. name: cloud-oauth-server-9999 # 应用名称,应用名称会在Eureka中作为服务名称
  4. datasource:
  5. driver-class-name: com.mysql.jdbc.Driver
  6. url: jdbc:mysql://localhost:3306/oauth2?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
  7. username: root
  8. password: root
  9. druid:
  10. initialSize: 10
  11. minIdle: 10
  12. maxActive: 30
  13. maxWait: 50000

测试一下

首先是认证服务获取token

在这里插入图片描述
db的信息

在这里插入图片描述
可以看到请求令牌成功

请求服务

在这里插入图片描述

默认情况下jwt令牌存储的客户端信息是特定的,如果我们想额外增加信息呢,就需要扩展jwt信息。

认证服务器⽣成JWT令牌时存⼊扩展信息(⽐如clientIp)

继承DefaultAccessTokenConverter类,重写convertAccessToken⽅法存⼊扩展信息

  1. /** * 扩展jwt令牌 存放客户信息 */
  2. @Component
  3. public class AccessTokenConvertor extends DefaultAccessTokenConverter {
  4. @Override
  5. public Map<String, ?> convertAccessToken(OAuth2AccessToken
  6. token, OAuth2Authentication authentication) {
  7. // 获取到request对象
  8. HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest();
  9. // 获取客户端ip(注意:如果是经过代理之后到达当前服务的话,那么这种⽅式获取的并不是真实的浏览器客户端ip)
  10. String remoteAddr = request.getRemoteAddr();
  11. Map<String, String> stringMap = (Map<String, String>)
  12. super.convertAccessToken(token, authentication);
  13. stringMap.put("clientIp", remoteAddr); //扩展客户端ip 等信息
  14. return stringMap;
  15. }
  16. }

将此组件添加到扩展器中
在这里插入图片描述

同时资源服务器取出 JWT 令牌扩展信息

也需要⾃定义⼀个转换器类,继承DefaultAccessTokenConverter,重写extractAuthentication提取⽅法,把载荷信息设置到认证对象的details属性中

  1. @Component
  2. public class AccessTokenConvertor extends DefaultAccessTokenConverter {
  3. @Override
  4. public OAuth2Authentication extractAuthentication(Map<String, ? > map) {
  5. OAuth2Authentication oAuth2Authentication =
  6. super.extractAuthentication(map);
  7. oAuth2Authentication.setDetails(map); // 将map放⼊认证对象中,认证对象在controller中可以拿到
  8. return oAuth2Authentication;
  9. }
  10. }

同时也需要将此组件添加到扩展器中,客户端需要校验

然后我们可以通过 Object details = SecurityContextHolder.getContext().getAuthentication().getDetails()代码在业务代码中获取到用户信息。

同理我们请求一下拿到令牌,然后验证一下网站https://jwt.io/
可以看到解析之后我们的客户端信息,以及扩展的ip字段。
在这里插入图片描述
在没验证的情况也可以看到客户端的信息,所以不要放敏感信息

然后将其添加到网关层使用

  1. #eureka server服务端口
  2. server:
  3. port: 9000
  4. spring:
  5. application:
  6. name: server-pruduce-9000-getWay # 应用名称,应用名称会在Eureka中作为服务名称
  7. cloud:
  8. inetutils:
  9. # 指定此客户端的ip
  10. default-ip-address: springcloud
  11. #getway配置
  12. ##### 动态路由设置时,uri以 lb: //开头(lb代表从注册中⼼获取服务),后⾯是需要转发到的服务名称
  13. gateway:
  14. routes: # 路由可以有多个
  15. # - id: service-router # 我们⾃定义的路由 ID,保持唯⼀
  16. # #uri: http://127.0.0.1:8082 # ⽬标服务地址 部署多实例) 动态路由:uri配置的应该是⼀个服务名称,⽽不应该是⼀个具体的服务实例的地址
  17. # uri: lb://server-pruduce-8082-feign # ⽬标服务地址 部署多实例) 动态路由:uri配置的应该是⼀个服务名称,⽽不应该是⼀个具体的服务实例的地址
  18. # # gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
  19. # predicates: #断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默 认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
  20. # - Path=/api/**
  21. - id: cloud-oauth-server-9999 # 我们⾃定义的路由 ID,保持唯⼀
  22. #uri: http://127.0.0.1:8082 # ⽬标服务地址 部署多实例) 动态路由:uri配置的应该是⼀个服务名称,⽽不应该是⼀个具体的服务实例的地址
  23. uri: lb://cloud-oauth-server-9999 # ⽬标服务地址 部署多实例) 动态路由:uri配置的应该是⼀个服务名称,⽽不应该是⼀个具体的服务实例的地址
  24. # gateway⽹关从服务注册中⼼获取实例信息然后负载后路由
  25. predicates: #断⾔:路由条件,Predicate 接受⼀个输⼊参数,返回⼀个布尔值结果。该接⼝包含多种默 认⽅法来将 Predicate 组合成其他复杂的逻辑(⽐如:与,或,⾮)。
  26. - Path=/oauth/**
  27. #eureka配置
  28. eureka:
  29. instance:
  30. hostname: springcloud # 当前eureka实例的主机名
  31. ip-address: springcloud
  32. client:
  33. service-url:
  34. prefer-ip-address: true
  35. lease-renewal-interval-in-seconds: 30
  36. # 租约到期,服务时效时间,默认值90秒,服务超过90秒没有发⽣⼼跳,服务注册中心会将服务从列表移除
  37. lease-expiration-duration-in-seconds: 90
  38. # 配置客户端所交互的Eureka Server的地址(Eureka Server集群中每一个Server其实相对于其它Server来说都是Client)
  39. # 集群模式下,defaultZone应该指向其它Eureka Server,如果有更多其它Server实例,逗号拼接即可
  40. defaultZone: http://127.0.0.1:8761/eureka,http://127.0.0.1:8762/eureka # 注册到集群汇总,多个用,拼接
  41. register-with-eureka: true # 集群模式下可以改成true
  42. fetch-registry: true # 集群模式下可以改成true
  43. # 分布式链路追踪
  44. logging:
  45. level:
  46. org.springframework.web.servlet.DispatcherServlet: debug
  47. org.springframework.cloud.sleuth: debug

调用 可以正常返回令牌

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,719人围观)

还没有评论,来说两句吧...

相关阅读