客户端证书
按URL配置测试中使用的证书颁发机构(CA)和客户端证书。
info
文档范围
本文档仅提供如何为指定测试URL配置证书文件路径的指导。关于这些文件内容和用途的详细信息不属于Cypress文档范围。
语法
clientCertificates (Object[])
定义证书的对象数组。每个对象必须包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
url | String | 要匹配请求的URL。支持遵循minimatch规则的通配符。 |
ca | Array | (可选) 用于验证证书的一个或多个CA文件路径,相对于项目根目录。 |
certs | Object[] | PEM格式的证书/私钥对或PFX证书容器 |
certs
数组中的每个对象可以定义PEM格式证书/私钥对或PFX证书容器。
PEM格式证书/私钥对可包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
cert | String | 证书文件路径,相对于项目根目录。 |
key | String | 私钥文件路径,相对于项目根目录。 |
passphrase | String | (可选) 包含密码短语的文本文件路径,相对于项目根目录。 |
PFX证书容器可包含以下属性:
属性 | 类型 | 描述 |
---|---|---|
pfx | String | 证书容器路径,相对于项目根目录。 |
passphrase | String | (可选) 包含密码短语的文本文件路径,相对于项目根目录。 |
使用方式
要在Cypress配置中配置CA/客户端证书,可以添加clientCertificates
键来定义客户端证书数组,如下所示:
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
clientCertificates: [
{
url: 'https://a.host.com',
ca: ['certs/ca.pem'],
certs: [
{
cert: 'certs/cert.pem',
key: 'certs/private.key',
passphrase: 'certs/pem-passphrase.txt',
},
],
},
{
url: 'https://b.host.com/a_base_route/**',
ca: [],
certs: [
{
pfx: '/home/tester/certs/cert.pfx',
passphrase: '/home/tester/certs/pfx-passphrase.txt',
},
],
},
{
url: 'https://a.host.*.com/',
ca: [],
certs: [
{
pfx: 'certs/cert.pfx',
passphrase: 'certs/pfx-passphrase.txt',
},
],
},
],
})
import { defineConfig } from 'cypress'
export default defineConfig({
clientCertificates: [
{
url: 'https://a.host.com',
ca: ['certs/ca.pem'],
certs: [
{
cert: 'certs/cert.pem',
key: 'certs/private.key',
passphrase: 'certs/pem-passphrase.txt',
},
],
},
{
url: 'https://b.host.com/a_base_route/**',
ca: [],
certs: [
{
pfx: '/home/tester/certs/cert.pfx',
passphrase: '/home/tester/certs/pfx-passphrase.txt',
},
],
},
{
url: 'https://a.host.*.com/',
ca: [],
certs: [
{
pfx: 'certs/cert.pfx',
passphrase: 'certs/pfx-passphrase.txt',
},
],
},
],
})
历史版本
版本 | 变更内容 |
---|---|
8.0.0 | 新增客户端证书配置选项 |