
# devspore-auth自定义token解析认证
#### **模块依赖设计图**
图1devspore-auth插件依赖   
![](https://support.huaweicloud.com/devg-astropro/zh-cn_image_0000002173203957.png "点击放大")
图2模块依赖图   
![](https://support.huaweicloud.com/devg-astropro/zh-cn_image_0000002137766300.png)
#### 开发环境准备
需提前安装的工具，包括JDK、Maven、Eclipse和IDEA，配置对应的环境变量，确保本地开发环境可用。
#### 工作原理
- 引入devspore-auth-starter依赖后，会在SDK内自动注册认证Filter：AuthAuthenticationFilter。
- 调用业务接口时会自动执行已注册的认证Filter，AuthAuthenticationFilter.doFilterInternal中将通过SPI方式先调用TokenParser的实现类（DefaultTokenParser，用户可自定义）完成token解析。
- AuthAuthenticationFilter.doFilterInternal在token解析成功后将解析出的用户信息通过SPI方式调用UserStore的实现类（DefaultUserStore，用户可自定义）存储用户信息供后续业务代码使用。
- TokenParser接口介绍详见：[TokenParser接口介绍](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section541736324)
- UserInfo用户信息类介绍详见：[UserInfo用户信息类](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section0518162214341)
- UserStore接口介绍详见：[UserStore接口介绍](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section1183410133357)
 
#### 认证配置
认证配置详见：[认证配置](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section51248462614)
#### 配置说明
配置说明详见：[配置说明](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section142537619294)
#### **使用devspore-auth通用认证能力**
1. 添加依赖。 
   代码中添加devspore-auth-starter依赖。
   ```
   <dependency>
       <groupId>com.huaweicloud.devspore</groupId>
       <artifactId>devspore-auth-starter</artifactId>
       <version>${devspore-auth-starter.version}</version>
   </dependency>
   ```
   ![](https://support.huaweicloud.com/devg-astropro/public_sys-resources/notice_3.0-zh-cn.png)
   TokenParser实现类与UserStore实现类，均需注册成bean，并完成bean扫描，确保Spring容器中可获取到对应bean。
   
   
2. 实现TokenParser接口：详见[实现TokenParser接口示例](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section175664178232)
3. 实现UserStore接口：详见[实现UserStore接口](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section4707125418236)
4. 添加认证配置：在配置文件中加入devspore相关配置，详见[认证配置](https://support.huaweicloud.com/devg-astropro/astropro_06_0022.html#astropro_06_0022__section51248462614)
 
#### 时序图
使用devspore完成认证鉴权模块调用时序图，如[图3]所示。
图3模块调用时序图   
![](https://support.huaweicloud.com/devg-astropro/zh-cn_image_0000002173207573.png "点击放大")
