wujimin commented on a change in pull request #119: 轻松微服务系列:边缘服务支持统一认证
URL: 
https://github.com/apache/incubator-servicecomb-website/pull/119#discussion_r202228031
 
 

 ##########
 File path: _posts/cn/2018-07-10-easy-build-microservice-system-part-IV.md
 ##########
 @@ -0,0 +1,273 @@
+---
+title: "轻松微服务系列:边缘服务支持统一认证"
+lang: cn
+ref: easy-build-microservice-system-part-IV
+permalink: /cn/docs/easy-build-microservice-system-part-IV/
+excerpt: "轻松微服务系列:边缘服务支持统一认证"
+last_modified_at: 2018-07-10T19:00:00+08:00
+author: Yangyong Zheng
+tags: [Edge Service,API Gateway,Authentication]
+redirect_from:
+  - /theme-setup/
+---
+
+## 轻松微服务系列:边缘服务支持统一认证
+在前一篇博文[《轻松微服务系列:开发高性能边缘服务》](http://servicecomb.incubator.apache.org/cn/docs/easy-build-microservice-system-part-III/),我们开发了具备基本路由能力的高性能边缘服务。这篇博文我们将在Edge服务上实施如何扩展支持统一认证。
+
+### 设计思路
+正如前面的博文提到过,统一认证的目的是在Edge入口处进行访问认证,避免需要在所有的微服务中都承载重复的认证机制,因此:
+1. 
我们先要将认证功能作为一个独立的Procuder发布出来,使Edge服务能够随时认证Token,我们将其命名为`AuthenticationService`,放在用户服务中;
+2. 将无需认证的访问请求识别出来,包括:
+
+| 功能      | 描述                     |
+| :------- | :--------------------- |
+| login    | 登录验证,通过后为用户生成Token |
+| logon    | 新用户注册                  |
+
+除此之外其他业务请求都需要做Token认证;
+
+3. 
Edge服务转发访问请求之前,对需要认证的请求先做统一认证,认证通过之后才转发,为了能够未来更好的扩展这种“转发前处理”的能力,我们设计一个处理链机制`FilterChain`:
+
+![FilterChain](/assets/images/scaffold/FilterChain.png)
+
+>提示:另外一种方案就是扩展Handler,如果检查失败则使Handler链调用直接返回;但是由于认证过程同样是一个Consumer调用,也会触发Handler
 处理,这会使Handler的逻辑和配置复杂化,因此此场景下不推荐。
+
+完整统一认证时序图为:
+
+![EdgeAuth](/assets/images/scaffold/EdgeAuth.png)
+
+### 实现统一认证
+#### 第一步:发布认证服务
+##### 定义AuthenticationService
+```java
+public interface AuthenticationService {
+  String validate(String token);
+}
+```
+##### 实现并发布AuthenticationService
+```java
+@RestSchema(schemaId = "authentication")
+@RequestMapping(path = "/")
+public class AuthenticationServiceImpl implements AuthenticationService {
+
+  private final TokenStore tokenStore;
+
+  @Autowired
+  public AuthenticationServiceImpl(TokenStore tokenStore) {
+    this.tokenStore = tokenStore;
+  }
+
+  @Override
+  @GetMapping(path = "validate")
+  public String validate(String token) {
+    String userName = tokenStore.validate(token);
+    if (userName == null) {
+      throw new InvocationException(BAD_REQUEST, "incorrect token");
+    }
+    return userName;
+  }
+}
+```
+
+#### 第二步:实现转发前处理链FilterChain
+##### 定义处理链接口EdgeFilter
+```java
+public interface EdgeFilter {
 
 Review comment:
   httpServerFilter has problems?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to