# diboot-IAM: 身份认证组件

# 1、扩展其他登录方式

默认的登录方式为:用户名密码,如需扩展其他登录方式:

  • 创建你的认证凭证对象,继承自AuthCredential
  • 实现AuthService接口,定义认证方式及接口实现
  • 申请token替换为你的认证方式:
MyAuthCredential credential = new MyAuthCredential();
String authtoken = AuthServiceFactory.getAuthService("WX_CP").applyToken(credential);
1
2

# 2、替换或扩展用户类型

默认的用户实体为IamUser,获取当前登录用户对象:

IamUser currentUser = IamSecurityUtils.getCurrentUser();
1

如果预置属性如果不能满足业务场景需要,可替换用户为你的实体:

  • 首先在登录的方法中,生成token前传入当前用户类型: (如果有多种用户类型共用登录接口,可以从前端登录页传入"用户类型值")
MyAuthCredential credential = new MyAuthCredential();
credential.setUserTypeClass(Employee.class); // 用户类型为自定义
String authtoken = AuthServiceFactory.getAuthService("WX_CP").applyToken(credential);
1
2
3

获取当前登录用户对象相关代码改为:

Employee currentUser = IamSecurityUtils.getCurrentUser();
1

EmployeeService 参考 com.diboot.iam.service.IamUserService ,实现用户账号与角色绑定的创建更新删除 - 多用户类型登录可定义多个登录接口用于不同类型用户登录,或根据不同参数设置的用户类型

# 3、替换缓存为Redis

在未引入redis的场景下,IAM组件默认缓存类为shiro的内存缓存实现:MemoryConstrainedCacheManager以及diboot的内存缓存类。
如果需要替换为redis缓存,参照如下步骤配置后重启即可生效(IAM组件识别到redis后将自动切换为redis缓存):

# 1). 依赖spring-boot-starter-data-redis,并配置参数

<!-- 引入redis依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
1
2
3
4
5

# 2). 配置redis server连接参数

# redis config
spring.redis.database=0
spring.redis.port=6379
spring.redis.host=localhost
spring.redis.password=
1
2
3
4
5

# 4. 自定义数据权限

查看 diboot-core 数据权限控制

# 5. 改为无状态token

  • v2.6.0 及之后版本 IAM已默认为无状态token(无需任何配置),剥离了session依赖,以简化集群环境部署及适配移动端。

  • v2.5.0 及之前版本 当我们开发移动端(如微信小程序)等接口时,需要为移动前端提供无状态的token管理。
    配置方式:

#是否开启无状态 Jwt 身份验证过滤器
diboot.iam.enable-stateless-session=true
1
2