mirror of
https://gitee.com/dromara/sa-token.git
synced 2024-11-29 18:37:49 +08:00
chore: oauth2-server demo 数据加载方式改为 SysClientMockDao 查询方式
This commit is contained in:
parent
442cf9db41
commit
2ce1328cdc
@ -0,0 +1,84 @@
|
||||
package com.pj.mock;
|
||||
|
||||
import cn.dev33.satoken.oauth2.consts.GrantType;
|
||||
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SaClientModel 模拟查询操作
|
||||
*
|
||||
* @author click33
|
||||
* @since 2024/11/15
|
||||
*/
|
||||
@Component
|
||||
public class SaClientMockDao {
|
||||
|
||||
public List<SaClientModel> list = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 构造方法,添加三个模拟应用
|
||||
*/
|
||||
public SaClientMockDao(){
|
||||
// 模拟应用1
|
||||
SaClientModel client1 = new SaClientModel()
|
||||
.setClientId("1001") // client id
|
||||
.setClientSecret("aaaa-bbbb-cccc-dddd-eeee") // client 秘钥
|
||||
.addAllowRedirectUris("*") // 所有允许授权的 url
|
||||
.addContractScopes("openid", "userid", "userinfo", "oidc") // 所有签约的权限
|
||||
.addAllowGrantTypes( // 所有允许的授权模式
|
||||
GrantType.authorization_code, // 授权码式
|
||||
GrantType.implicit, // 隐式式
|
||||
GrantType.refresh_token, // 刷新令牌
|
||||
GrantType.password, // 密码式
|
||||
GrantType.client_credentials, // 客户端模式
|
||||
"phone_code" // 自定义授权模式 手机号验证码登录
|
||||
);
|
||||
list.add(client1);
|
||||
|
||||
// 模拟应用2
|
||||
SaClientModel client2 = new SaClientModel()
|
||||
.setClientId("1002")
|
||||
.setClientSecret("aaaa-bbbb-cccc-dddd-eeee")
|
||||
.addAllowRedirectUris("*")
|
||||
.addContractScopes("openid", "userid", "userinfo", "oidc")
|
||||
.addAllowGrantTypes(
|
||||
GrantType.authorization_code,
|
||||
GrantType.implicit,
|
||||
GrantType.refresh_token,
|
||||
GrantType.password,
|
||||
GrantType.client_credentials
|
||||
);
|
||||
list.add(client2);
|
||||
|
||||
// 模拟应用3
|
||||
SaClientModel client3 = new SaClientModel()
|
||||
.setClientId("1003")
|
||||
.setClientSecret("aaaa-bbbb-cccc-dddd-eeee")
|
||||
.addAllowRedirectUris("*")
|
||||
.addContractScopes("openid", "userid", "userinfo", "oidc")
|
||||
.addAllowGrantTypes(
|
||||
GrantType.authorization_code,
|
||||
GrantType.implicit,
|
||||
GrantType.refresh_token,
|
||||
GrantType.password,
|
||||
GrantType.client_credentials
|
||||
);
|
||||
list.add(client3);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据应用 id 查找对应的应用,找不到则返回 null
|
||||
* @param clientId 应用 id
|
||||
* @return 应用对象
|
||||
*/
|
||||
public SaClientModel getClientModel(String clientId) {
|
||||
return list.stream()
|
||||
.filter(e -> e.getClientId().equals(clientId))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package com.pj.oauth2;
|
||||
|
||||
import cn.dev33.satoken.oauth2.consts.GrantType;
|
||||
import cn.dev33.satoken.oauth2.data.loader.SaOAuth2DataLoader;
|
||||
import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel;
|
||||
import com.pj.mock.SaClientMockDao;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@ -12,34 +13,21 @@ import org.springframework.stereotype.Component;
|
||||
*/
|
||||
@Component
|
||||
public class SaOAuth2DataLoaderImpl implements SaOAuth2DataLoader {
|
||||
|
||||
|
||||
@Autowired
|
||||
SaClientMockDao saClientMockDao;
|
||||
|
||||
// 根据 clientId 获取 Client 信息
|
||||
@Override
|
||||
public SaClientModel getClientModel(String clientId) {
|
||||
// 此为模拟数据,真实环境需要从数据库查询
|
||||
if("1001".equals(clientId)) {
|
||||
return new SaClientModel()
|
||||
.setClientId("1001") // client id
|
||||
.setClientSecret("aaaa-bbbb-cccc-dddd-eeee") // client 秘钥
|
||||
.addAllowRedirectUris("*") // 所有允许授权的 url
|
||||
.addContractScopes("openid", "userid", "userinfo", "oidc") // 所有签约的权限
|
||||
.addAllowGrantTypes( // 所有允许的授权模式
|
||||
GrantType.authorization_code, // 授权码式
|
||||
GrantType.implicit, // 隐式式
|
||||
GrantType.refresh_token, // 刷新令牌
|
||||
GrantType.password, // 密码式
|
||||
GrantType.client_credentials, // 客户端模式
|
||||
"phone_code" // 自定义授权模式 手机号验证码登录
|
||||
)
|
||||
;
|
||||
}
|
||||
return null;
|
||||
// 此为模拟数据,真实环境需要从数据库查询
|
||||
return saClientMockDao.getClientModel(clientId);
|
||||
}
|
||||
|
||||
// 根据 clientId 和 loginId 获取 openid
|
||||
@Override
|
||||
public String getOpenid(String clientId, Object loginId) {
|
||||
// 此处使用框架默认算法生成 openid,真实环境建议改为从数据库查询
|
||||
// 此处使用框架默认算法生成 openid,真实项目建议改为从数据库查询
|
||||
return SaOAuth2DataLoader.super.getOpenid(clientId, loginId);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user