使用kmse检查服务的权限
通过一个简单的示例说明开发人员如何在kmse中进行服务器之间的权限检查。
一.准备客户端和服务端两个demo
本节介绍了如何快速实践服务认证功能。 如果当前有两个微服务器auth-client和auth-server,并且希望实现auth-client调用auth-server,则auth-server将对请求进行身份验证。 查看服务开发文档,下载auth-server和auth-client这两个demo。
看一下依赖关系,实现服务认证只需要依赖以下maven组件,调用方和被调用方都需要如下依赖。
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-openfeign
com.ksyun.kmse
spring-cloud-kmse-starter-authentic ation
${version}
由于auth-server是调用方,因此将验证设置写入auth-server的bootstrap.yaml文件。 设置包括版本属性(VERSION,subset )和服务名称属性(spring.application.nammon )。设置意味着创建名为auth-rule-1的认证规则。 此规则意味着禁止应用名称前缀为“auth-client”的请求来访问auth-server APP应用程序。
版本: v1
自动策略:
http:
- match:
- APP name :
最终用户:
prefix : '自动客户端'
name :自动运行- 1
路:
-目标:
主机:自动服务器
subset: v1
type :黑名单
spring:
APP :
name :自动服务器
服务器:
端口: 8080
将验证所需的参数写入自动客户端的yaml中。 这些参数将自动注入系统,目前正在手动填写。 APP应用程序名称为auth-client,版本为v1。
版本: v1
服务器:
端口: 8081
spring:
APP :
name :自动客户端
要测试的java代码,自动服务器端提供服务的控制器:
package com.ksy un.kmse.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
importorg.spring framework.web.bind.annotation.path variable;
importorg.spring framework.web.bind.annotation.request mapping;
importorg.spring framework.web.bind.annotation.rest controller;
@requestmapping((/server ) )
@RestController
公共类帐户控制器{
privatestaticfinalloggerlog=logger factory.getlogger (account controller.class;
公共帐户控制器
}
@requestmapping({'/{id}} ) )
公共字符串帐户(@ path variable ) (id ) ) Integer id ) {
log.info ('调用服务器' id );
返回id ' ';
}
}
自动客户端提供的远程呼叫客户端:
package com.ksyun.kmse.client;
import org.springfr
amework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(name = "auth-server", url = "http://127.0.0.1:8080")//如果使用注册中心可以不使用显式的url配置
public interface OrderClient {
@GetMapping("/server/{id}")
String getById(@PathVariable Integer id);
}
auth-client端提供的测试访问入口controller:
package com.ksyun.kmse.controller;
import com.ksyun.kmse.client.OrderClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping({"/client"})
@RestController
public class AccountController {
private static final Logger log = LoggerFactory.getLogger(AccountController.class);
public AccountController() {
}
@Autowired
private OrderClient server;
@GetMapping({"/{id}"})
public String account(@PathVariable("id") Integer id) {
log.info("调用参数 " + id);
String result = server.getById(id);
log.info("远程调用结果 " + result);
return result;
}
}
至此两个测试应用准备完毕。
二、对服务鉴权进行测试
步骤一中的鉴权配置含义是"不允许applicationname前缀等于’auth-client’的请求访问"。 调用auth-client的测试接口 http://127.0.0.1:8081/client/1。
发现auth-server返回http验证码为403。
将auth-server的配置改为如下:
VERSION: v1
auth-policy:
http:
- match:
- applicationName:
endUser:
prefix: "Aclient"
name: auth-rule-1
route:
- destination:
host: auth-server
subset: v1
type: black-list
spring:
application:
name: auth-server
这个配置的含义是"不允许applicationname前缀等于Aclient的请求访问"。 重启auth-server后,再次调用测试接口,返回http状态码为200。
将auth-server的配置改为如下,测试后缀拦截:
VERSION: v1
auth-policy:
http:
- match:
- applicationName:
endUser:
suffix: "ent"
name: auth-rule-1
route:
- destination:
host: auth-server
subset: v1
type: black-list
spring:
application:
name: auth-server
这个配置的含义是"不允许applicationname后缀等于ent的请求访问"。 重启auth-server后,再次调用测试接口,返回http状态码为403,请求被拦截。
依次类推还有如下的请求场景:
#匹配请求来源url
auth-policy:
http:
- match:
- APIPath: "/auth-server/1"
#匹配请求来源ip
auth-policy:
http:
- match:
- IP: "127.0.0.1"
#匹配请求http方法
auth-policy:
http:
- match:
- Method: "GET"
#匹配应用版本
auth-policy:
http:
- match:
- applicationVersion: "v1"
#前缀匹配
auth-policy:
http:
- match:
- applicationName:
endUser:
prefix: "a"
#后缀匹配
auth-policy:
http:
- match:
- applicationName:
endUser:
suffix: "b"
#精准匹配
auth-policy:
http:
- match:
- applicationName:
endUser:
exact: "c"
#正则匹配,例如正整数
auth-policy:
http:
- match:
- applicationName:
endUser:
regular: "[1-9]d*"