# 使用JWT身份验证

### 使用流程

1. 通过工具或业务系统生成Jwt Token
2. 在mqtt客户端登陆时在password参数填写jwt token。注意：需要让password参数生效必须同时填写user参数，可随意填写，只是不允许空值。相关协议规定请查询mqtt协议说明。
3. http或coap的publish接口均支持使用Jwt token功能。请参阅本手册相关章节。

### 演练

#### 1.通过jwt.io网站生成jwt token

![](/files/-MOEzsSoSo5e4mCIcVFp)

复制左边的Encoded内容为实际jwt token

#### 2.配置Coolpy7内核启动相关参数

```
//-as=1即启动jwt验证功能
//-jsk=coolpy7即设置jwt解密密钥为coolpy7，此密钥必须与你生成jwt token的密钥一致
./go_build_Coolpy7_go_linux -as=1 -jsk=coolpy7
```

#### 3.通过客户端连接测试

以mqttx客户端工具为例

![](/files/-MOF0rqz4iGGjQotSdML)

点击连接按扭即可连接到Coolpy7并完成身份认证。

{% hint style="success" %}
一般业务场境使用jwt功能是由以下方法构建业务系统

1.通过业务系统生成与业务系统一致的jwt token，通过jwt token自动的超时机制对用户身份核实，并提供临时授权的功能。

2.用户通过已在业务系统中使用的jwt token同时用于连接到coolpy7的相关功能进行身份验证。

3.即可达到同一密钥，又能限制非法用户连接到cp7，还可使用到jwt的口令超时验证功能。
{% endhint %}

相关测试用例代码请参阅

http用例：<https://github.com/Coolpy7/coolpy7_benchmark/blob/master/test_coap_publish/cp7_coap_pub_jwt.go>

coap用例：<https://github.com/Coolpy7/coolpy7_benchmark/blob/master/test_coap_publish/cp7_coap_pub_jwt.go>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coolpy7.gitbook.io/coolpy7book/kai-shi-shi-yong/shi-yong-jwt-shen-fen-yan-zheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
