Springcloud Oauth2 HA篇

浅谈微服务安全架构设计 一文中,介绍了基于Springcloud 结合了Oauth2分析了其各种模式下的鉴权认证,今天主要分析如何结合k8s作实现鉴权的高可用。

假设我们的项目中有几个模块:

  • 鉴权中心:Oauth2服务
  • 订单系统:客户端A
  • 用户管理系统:客户端B

在上面的系统中,每个服务之间的耦合性很低,但是又有着很频繁的调用,这就涉及到UI与其之间的频繁流量交互。如何做到其HA,这里引入k8s的Service方法:

Spring Cloud Kubernetes之实战服务注册与发现一文中,就讲解了k8s的Service方式创建服务,然后可以部署多个pod,同时结合 Spring Cloud Kubernetes之实战网关Gateway 来实现LB,类似通过域名来解析其服务,并根据所定义的规则进行LB。同样,本文则是Oauth2的基础上,结合这些来实现微服务的LB。同时此处利用了k8s来作主要处理,如果是其他语言(Python、Go、Rust等)的客户端服务,则自身可以通过逻辑来控制其鉴权以及获取流量的。

注意点:由于各微服务与鉴权中心有交互,故鉴权中心需要提供HA服务,即先在启动类加入@EnableDiscoveryClient ,后续在注入bean时,@LoadBalanced来实现LB鉴权中心。

1
2
3
4
5
6
7
8
9
10
11
12
13
@EnableOAuth2Sso
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.damon"})
@EnableConfigurationProperties(EnvConfig.class)
@EnableDiscoveryClient #为LB多节点鉴权中心准备
public class AdminApp {

public static void main(String[] args) {
SpringApplication.run(AdminApp.class, args);
}

}

在客户端项目模块中,调用鉴权中心时,需要实现LB:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

@Configuration
public class BeansConfig {
@Resource
private Environment env;

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setReadTimeout(env.getProperty("client.http.request.readTimeout", Integer.class, 15000));
requestFactory.setConnectTimeout(env.getProperty("client.http.request.connectTimeout", Integer.class, 3000));
RestTemplate rt = new RestTemplate(requestFactory);
return rt;
}

}

另外本身在配置交互的时候,需要加上域名等形式来实现LB,这里利用了k8s的Service来实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cas-server-url: http://cas-server-service #这里配置成HA地址

security:
oauth2: #与cas-server对应的配置
client:
client-id: admin-web
client-secret: admin-web-123
user-authorization-uri: ${cas-server-url}/oauth/authorize #是授权码认证方式需要的
access-token-uri: ${cas-server-url}/oauth/token #是密码模式需要用到的获取 token 的接口
resource:
loadBalanced: true
id: admin-web
user-info-uri: ${cas-server-url}/api/user #指定user info的URI
prefer-token-info: false

这样,一个客户端关于鉴权的核心就是如此了,同样需要把消费客户端以service形式提供给UI,此时需要借助 Spring Cloud Kubernetes之实战网关Gateway 和nginx代理服务,我们来测试下:curl -X POST -d "username=admin&password=123456&grant_type=password&client_id=admin-web&client_secret=admin-web-123" http://192.168.8.10:5556/cas-server/oauth/token

看到结果:

1
{"access_token":"5a7892b0-7483-4f60-89fd-44255a429ff6","token_type":"bearer","refresh_token":"23f2e8ea-f091-4ab0-822c-f28bebc4ec08","expires_in":3599,"scope":"all"}

通过获取到的access_token来访问对应的客户端:curl -H "Accept: application/json" -H "Authorization:bearer 5a7892b0-7483-4f60-89fd-44255a429ff6" -X GET http://192.168.8.10:5556/admin-web/api/user/getCurrentUser

输出结果:

1
{"authorities":[{"authority":"admin"}],"details":{"remoteAddress":"10.244.0.196","sessionId":null,"tokenValue":"5a7892b0-7483-4f60-89fd-44255a429ff6","tokenType":"bearer","decodedDetails":null},"authenticated":true,"userAuthentication":{"authorities":[{"authority":"admin"}],"details":{"authorities":[{"authority":"admin"}],"details":{"remoteAddress":"10.244.0.201","sessionId":null,"tokenValue":"5a7892b0-7483-4f60-89fd-44255a429ff6","tokenType":"Bearer","decodedDetails":null},"authenticated":true,"userAuthentication":{"authorities":[{"authority":"admin"}],"details":{"client_secret":"admin-web-123","grant_type":"password","client_id":"admin-web","username":"admin"},"authenticated":true,"principal":{"password":null,"username":"admin","authorities":[{"authority":"admin"}],"accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"enabled":true},"credentials":null,"name":"admin"},"oauth2Request":{"clientId":"admin-web","scope":["all"],"requestParameters":{"grant_type":"password","client_id":"admin-web","username":"admin"},"resourceIds":[],"authorities":[],"approved":true,"refresh":false,"redirectUri":null,"responseTypes":[],"extensions":{},"grantType":"password","refreshTokenRequest":null}………

最后,这里鉴权的高可用通过k8s的service,进行默认的轮询方式的访问鉴权中心,鉴权中心如果鉴权时不管使用redis还是jwt,来管理token,都是可以的。



结束福利

开源实战利用 k8s 作微服务的架构设计代码:

https://gitee.com/damon_one/spring-cloud-k8s

欢迎大家 star,多多指教。


关于作者

  笔名:Damon,技术爱好者,长期从事 Java 开发、Spring Cloud 的微服务架构设计,以及结合 Docker、K8s 做微服务容器化,自动化部署等一站式项目部署、落地。目前主要从事基于 K8s 云原生架构研发的工作。Golang 语言开发,长期研究边缘计算框架 KubeEdge、调度框架 Volcano 等。公众号 交个朋友之猿天地 发起人。个人微信 DamonStatham,星球:《交个朋友之猿田地》,个人网站:交个朋友之猿天地 | 微服务 | 容器化 | 自动化,欢迎來撩。


欢迎关注:InfoQ

欢迎关注:腾讯自媒体专栏


欢迎关注

公号:交个朋友之猿天地

公号:damon8

公号:天山六路折梅手


打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2020-2023 交个朋友之猿天地
  • Powered By Hexo | Title - Nothing
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信