用户与客户
- TOB 用户,指的是通过 PIG 登录后台完成业务能力的用户(比如淘宝的后台管理员等),此部分用户保存在 sys_user 表。
- TOC 客户,指的是面向大众的客户(比如在淘宝购买东西的客户),此部分客户独立存在
快速上手
新增 TOC 客户表
- ① 此表对应的实体都会放在 upms 模块,所以表也要和原有 sys_user 在同一个数据库
CREATE TABLE `toc_custom` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`username` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='客户表';
- ② pig-upms-api 新增 TocCustom 实体
@Data
public class TocCustom implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String nickname;
private String username;
private String password;
}
- ③ pig-upms-biz 新增 TocCustomMapper 查询工具
@Mapper
public interface TocCustomMapper extends BaseMapper<TocCustom> {
}
- ④ pig-upms-biz 新增 custom 查询接口
@Inner
@GetMapping("/custom/{username}")
public R customInfo(@PathVariable String username) {
TocCustom custom = customMapper.selectOne(Wrappers.<TocCustom>lambdaQuery()
.eq(TocCustom::getUsername, username));
return R.ok(custom);
}
- ⑤ feign-client 增加调用 custom 接口
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.UPMS_SERVICE)
public interface RemoteUserService {
@GetMapping("/user/custom/{username}")
R<TocCustom> custom(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM) String from);
}
修改认证中心代码
- ① 增加 custom 客户端
INSERT INTO `sys_oauth_client_details` (`client_id`,`resource_ids`,`client_secret`,`scope`,`authorized_grant_types`,`web_server_redirect_uri`,`authorities`,`access_token_validity`,`refresh_token_validity`,`additional_information`,`autoapprove`) VALUES ('custom',NULL,'custom','server','password,refresh_token',NULL,NULL,10000,11111111,'','true');
- ② 修改整个框架最核心的代码 custom 客户端的专用 UserDetailsService
@Slf4j
@RequiredArgsConstructor
public class PigCustomUserDetailsServiceImpl implements PigUserDetailsService {
private final RemoteUserService remoteUserService;
private final CacheManager cacheManager;
/**
* 用户名密码登录
* @param username 用户名
* @return
*/
@Override
@SneakyThrows
public UserDetails loadUserByUsername(String username) {
R<TocCustom> result = remoteUserService.custom(username, SecurityConstants.FROM_IN);
// 根据 result 构建security 框架需要的 用户对象
TocCustom custom = result.getData();
if (custom == null) {
throw new UsernameNotFoundException("用户不存在");
}
// 构造security用户
return new PigUser(custom.getId(), null, custom.getUsername(), custom.getPassword(), null, true, true,
true, true, AuthorityUtils.NO_AUTHORITIES);
}
/**
* 是否支持此客户端校验
* @param clientId 目标客户端
* @param grantType
* @return true/false
*/
@Override
public boolean support(String clientId, String grantType) {
return "custom".equals(clientId);
}
@Override
public int getOrder() {
return Integer.MIN_VALUE + 1;
}
}
- ③ 配置SPI 加载 CustomUserDetailsService
!> 重点说明以上改动代码出现的重要参数
参数 | 说明 |
---|---|
clientId | 这里根据前端登录请求写的的 clientId 区分是否是 toc 请求还是 tob 请求。这里选择 test 客户端 |
AuthorityUtils.NO_AUTHORITIES | 这里说明 toc 用户没有角色这一说,当然你可以通过后台创建一个角色 “ROLE_CUSTOM” 赋值给它 |
调用测试
curl --location --request POST 'http://pig-gateway:9999/auth/oauth2/token?grant_type=password' \
--header 'Authorization: Basic Y3VzdG9tOmN1c3RvbQ==' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=lengleng' \
--data-urlencode 'password=123456' \
--data-urlencode 'scope=server'