parent
b5ba8ee0a9
commit
060b77b1df
11 changed files with 167 additions and 259 deletions
@ -0,0 +1,126 @@ |
||||
package org.springblade.auth.granter; |
||||
|
||||
import com.alibaba.fastjson.JSON; |
||||
import com.yawei.pso.SSOResponse; |
||||
import com.yawei.pso.TicketManager; |
||||
import lombok.RequiredArgsConstructor; |
||||
import org.springblade.auth.constant.AuthConstant; |
||||
import org.springblade.auth.service.BladeUserDetails; |
||||
import org.springblade.auth.utils.TokenUtil; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.utils.*; |
||||
import org.springblade.system.feign.IDictBizClient; |
||||
import org.springblade.system.user.entity.User; |
||||
import org.springblade.system.user.entity.UserInfo; |
||||
import org.springblade.system.user.feign.IUserClient; |
||||
import org.springframework.security.authentication.AbstractAuthenticationToken; |
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
||||
import org.springframework.security.core.Authentication; |
||||
import org.springframework.security.core.authority.AuthorityUtils; |
||||
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException; |
||||
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException; |
||||
import org.springframework.security.oauth2.common.exceptions.UnauthorizedUserException; |
||||
import org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException; |
||||
import org.springframework.security.oauth2.provider.*; |
||||
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter; |
||||
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.util.HashMap; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* @author ytl |
||||
* @since 2023-05-19 13:38 |
||||
*/ |
||||
public class YaweiTokenGranter extends AbstractTokenGranter { |
||||
|
||||
private static final String GRANT_TYPE = "yawei"; |
||||
private static final Integer AUTH_SUCCESS_CODE = 2000; |
||||
|
||||
private final IUserClient userClient; |
||||
private final IDictBizClient dictBizClient; |
||||
|
||||
protected YaweiTokenGranter(AuthorizationServerTokenServices tokenServices, |
||||
ClientDetailsService clientDetailsService, |
||||
OAuth2RequestFactory requestFactory, |
||||
IUserClient userClient, IDictBizClient dictBizClient) { |
||||
super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE); |
||||
this.userClient = userClient; |
||||
this.dictBizClient = dictBizClient; |
||||
} |
||||
|
||||
@Override |
||||
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) { |
||||
// 请求头租户信息
|
||||
HttpServletRequest request = WebUtil.getRequest(); |
||||
String tenantId = Func.toStr(request.getHeader(TokenUtil.TENANT_HEADER_KEY), TokenUtil.DEFAULT_TENANT_ID); |
||||
|
||||
Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters()); |
||||
// 金宏来源
|
||||
String ssoToken = parameters.get("SSOToken"); |
||||
R<UserInfo> result; |
||||
BladeUserDetails bladeUserDetails; |
||||
|
||||
// 1. 未传ssotoken
|
||||
if (Func.isBlank(ssoToken)) { |
||||
// 调用feign接口获取重定向地址
|
||||
String redirectAddr = "http://jhoa.qd.gov.cn/Keeper.aspx"; |
||||
R<String> bizResult = dictBizClient.getValue("redirectAddr", "redirectAddrKey"); |
||||
// 若从字段配置中获取到重定向地址, 返回该地址, 否者返回默认地址
|
||||
if (bizResult.isSuccess() && Func.isNotBlank(bizResult.getData())) { |
||||
redirectAddr = bizResult.getData(); |
||||
} |
||||
|
||||
throw new InvalidRequestException(redirectAddr); |
||||
} |
||||
|
||||
// 如果服务器端通过认证后,会返回后执行改操作,然后写入cookie
|
||||
SSOResponse ssoResp = new SSOResponse(ssoToken); |
||||
|
||||
// 2. 传了ssotoken, 解析失败
|
||||
TicketManager tm; |
||||
try { |
||||
tm = ssoResp.CreatePSOTicket(); |
||||
result = userClient.userInfo(StringPool.EMPTY, tm.getUserName()); |
||||
} catch (Exception e) { |
||||
// result = userClient.userInfo(StringPool.EMPTY, "jiangzx");
|
||||
throw new InvalidGrantException("SSOToken解析失败!"); |
||||
} |
||||
|
||||
// 调用用户模块获取用户信息失败
|
||||
if (!result.isSuccess()) { |
||||
throw new InvalidGrantException("调用用户模块获取用户信息失败!"); |
||||
} |
||||
|
||||
// 3. 解析成功, 未找到用户
|
||||
UserInfo userInfo = result.getData(); |
||||
User user = userInfo.getUser(); |
||||
|
||||
// 用户不存在,但提示用户名与密码错误并锁定账号
|
||||
if (user == null || user.getId() == null) { |
||||
throw new InvalidGrantException(TokenUtil.USER_NOT_FOUND); |
||||
} |
||||
|
||||
// 用户角色不存在
|
||||
// if (Func.isEmpty(userInfo.getRoles())) {
|
||||
// throw new InvalidGrantException(TokenUtil.USER_HAS_NO_ROLE);
|
||||
// }
|
||||
|
||||
bladeUserDetails = new BladeUserDetails(user.getId(), |
||||
user.getTenantId(), StringPool.EMPTY, user.getName(), user.getRealName(), user.getDeptId(), user.getPostId(), user.getRoleId(), Func.join(userInfo.getRoles()), Func.toStr(user.getAvatar(), TokenUtil.DEFAULT_AVATAR), |
||||
user.getName(), AuthConstant.ENCRYPT + user.getPassword(), userInfo.getDetail(), true, true, true, true, |
||||
AuthorityUtils.commaSeparatedStringToAuthorityList(Func.join(result.getData().getRoles()))); |
||||
|
||||
// 组装认证数据,关闭密码校验
|
||||
Authentication userAuth = new UsernamePasswordAuthenticationToken(bladeUserDetails, null, bladeUserDetails.getAuthorities()); |
||||
((AbstractAuthenticationToken) userAuth).setDetails(parameters); |
||||
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest); |
||||
|
||||
// 返回 OAuth2Authentication
|
||||
OAuth2Authentication authentication = new OAuth2Authentication(storedOAuth2Request, userAuth); |
||||
return authentication; |
||||
} |
||||
} |
||||
@ -1,3 +1,6 @@ |
||||
#服务器端口 |
||||
server: |
||||
port: 8002 |
||||
social: |
||||
domain: http://127.0.0.1:1888 |
||||
enabled: true |
||||
@ -1,254 +0,0 @@ |
||||
|
||||
package org.springblade.common.constant; |
||||
|
||||
import org.springblade.core.launch.constant.AppConstant; |
||||
|
||||
import static org.springblade.core.launch.constant.AppConstant.APPLICATION_NAME_PREFIX; |
||||
|
||||
/** |
||||
* 启动常量 |
||||
* |
||||
* @author Chill |
||||
*/ |
||||
public interface LauncherConstant { |
||||
|
||||
/** |
||||
* 郵件服務 |
||||
*/ |
||||
String APPLICATION_EMAIL_NAME = APPLICATION_NAME_PREFIX + "email"; |
||||
|
||||
|
||||
String APPLICATION_FLOW_NAME = APPLICATION_NAME_PREFIX + "workflow"; |
||||
|
||||
/** |
||||
* 资产 |
||||
*/ |
||||
String APPLICATION_CAPITAL_NAME = APPLICATION_NAME_PREFIX + "capital"; |
||||
|
||||
/** |
||||
* lims |
||||
*/ |
||||
String APPLICATION_LIMS_NAME = APPLICATION_NAME_PREFIX + "lims"; |
||||
|
||||
/** |
||||
* office |
||||
*/ |
||||
String APPLICATION_OFFICE_NAME = APPLICATION_NAME_PREFIX + "office"; |
||||
/** |
||||
* repair |
||||
*/ |
||||
String APPLICATION_REPAIR_NAME = APPLICATION_NAME_PREFIX + "repair"; |
||||
|
||||
/** |
||||
* monitor |
||||
*/ |
||||
String APPLICATION_MONITOR_NAME = APPLICATION_NAME_PREFIX + "monitor"; |
||||
|
||||
/** |
||||
* iot |
||||
*/ |
||||
String APPLICATION_IOT_NAME = APPLICATION_NAME_PREFIX + "iot"; |
||||
/** |
||||
* xxljob |
||||
*/ |
||||
String APPLICATION_XXLJOB_NAME = APPLICATION_NAME_PREFIX + "xxljob"; |
||||
|
||||
/** |
||||
* xxljob |
||||
*/ |
||||
String APPLICATION_XXLJOB_ADMIN_NAME = APPLICATION_NAME_PREFIX + "xxljob-admin"; |
||||
|
||||
/** |
||||
* nacos dev 地址 |
||||
*/ |
||||
// String NACOS_DEV_ADDR = "172.29.14.103:8848"; |
||||
String NACOS_DEV_ADDR = "127.0.0.1:8848"; |
||||
|
||||
//String NACOS_DEV_ADDR = "172.29.14.103:8848"; |
||||
|
||||
/** |
||||
* nacos prod 地址 |
||||
*/ |
||||
String NACOS_PROD_ADDR = "172.30.0.48:8848"; |
||||
|
||||
/** |
||||
* nacos test 地址 |
||||
*/ |
||||
String NACOS_TEST_ADDR = "192.168.1.88:8848"; |
||||
|
||||
/** |
||||
* sentinel dev 地址 |
||||
*/ |
||||
String SENTINEL_DEV_ADDR = "127.0.0.1:8848"; |
||||
|
||||
/** |
||||
* sentinel prod 地址 |
||||
*/ |
||||
String SENTINEL_PROD_ADDR = "172.30.0.58:8858"; |
||||
|
||||
/** |
||||
* sentinel test 地址 |
||||
*/ |
||||
String SENTINEL_TEST_ADDR = "172.30.0.58:8858"; |
||||
|
||||
/** |
||||
* seata dev 地址 |
||||
*/ |
||||
String SEATA_DEV_ADDR = "127.0.0.1:8091"; |
||||
|
||||
/** |
||||
* seata prod 地址 |
||||
*/ |
||||
String SEATA_PROD_ADDR = "172.30.0.68:8091"; |
||||
|
||||
/** |
||||
* seata test 地址 |
||||
*/ |
||||
String SEATA_TEST_ADDR = "172.30.0.68:8091"; |
||||
|
||||
/** |
||||
* zipkin dev 地址 |
||||
*/ |
||||
String ZIPKIN_DEV_ADDR = "http://127.0.0.1:9411"; |
||||
|
||||
/** |
||||
* zipkin prod 地址 |
||||
*/ |
||||
String ZIPKIN_PROD_ADDR = "http://172.30.0.71:9411"; |
||||
|
||||
/** |
||||
* zipkin test 地址 |
||||
*/ |
||||
String ZIPKIN_TEST_ADDR = "http://172.30.0.71:9411"; |
||||
|
||||
/** |
||||
* elk dev 地址 |
||||
*/ |
||||
String ELK_DEV_ADDR = "127.0.0.1:9000"; |
||||
|
||||
/** |
||||
* elk prod 地址 |
||||
*/ |
||||
String ELK_PROD_ADDR = "172.30.0.72:9000"; |
||||
|
||||
/** |
||||
* elk test 地址 |
||||
*/ |
||||
String ELK_TEST_ADDR = "172.30.0.72:9000"; |
||||
|
||||
/** |
||||
* seata file模式 |
||||
*/ |
||||
String FILE_MODE = "file"; |
||||
|
||||
/** |
||||
* seata nacos模式 |
||||
*/ |
||||
String NACOS_MODE = "nacos"; |
||||
|
||||
/** |
||||
* seata default模式 |
||||
*/ |
||||
String DEFAULT_MODE = "default"; |
||||
|
||||
/** |
||||
* seata group后缀 |
||||
*/ |
||||
String GROUP_NAME = "-group"; |
||||
|
||||
/** |
||||
* seata 服务组格式 |
||||
* |
||||
* @param appName 服务名 |
||||
* @return group |
||||
*/ |
||||
static String seataServiceGroup(String appName) { |
||||
return appName.concat(GROUP_NAME); |
||||
} |
||||
|
||||
/** |
||||
* 动态获取nacos地址 |
||||
* |
||||
* @param profile 环境变量 |
||||
* @return addr |
||||
*/ |
||||
static String nacosAddr(String profile) { |
||||
switch (profile) { |
||||
case (AppConstant.PROD_CODE): |
||||
return NACOS_PROD_ADDR; |
||||
case (AppConstant.TEST_CODE): |
||||
return NACOS_TEST_ADDR; |
||||
default: |
||||
return NACOS_DEV_ADDR; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 动态获取sentinel地址 |
||||
* |
||||
* @param profile 环境变量 |
||||
* @return addr |
||||
*/ |
||||
static String sentinelAddr(String profile) { |
||||
switch (profile) { |
||||
case (AppConstant.PROD_CODE): |
||||
return SENTINEL_PROD_ADDR; |
||||
case (AppConstant.TEST_CODE): |
||||
return SENTINEL_TEST_ADDR; |
||||
default: |
||||
return SENTINEL_DEV_ADDR; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 动态获取seata地址 |
||||
* |
||||
* @param profile 环境变量 |
||||
* @return addr |
||||
*/ |
||||
static String seataAddr(String profile) { |
||||
switch (profile) { |
||||
case (AppConstant.PROD_CODE): |
||||
return SEATA_PROD_ADDR; |
||||
case (AppConstant.TEST_CODE): |
||||
return SEATA_TEST_ADDR; |
||||
default: |
||||
return SEATA_DEV_ADDR; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 动态获取zipkin地址 |
||||
* |
||||
* @param profile 环境变量 |
||||
* @return addr |
||||
*/ |
||||
static String zipkinAddr(String profile) { |
||||
switch (profile) { |
||||
case (AppConstant.PROD_CODE): |
||||
return ZIPKIN_PROD_ADDR; |
||||
case (AppConstant.TEST_CODE): |
||||
return ZIPKIN_TEST_ADDR; |
||||
default: |
||||
return ZIPKIN_DEV_ADDR; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 动态获取elk地址 |
||||
* |
||||
* @param profile 环境变量 |
||||
* @return addr |
||||
*/ |
||||
static String elkAddr(String profile) { |
||||
switch (profile) { |
||||
case (AppConstant.PROD_CODE): |
||||
return ELK_PROD_ADDR; |
||||
case (AppConstant.TEST_CODE): |
||||
return ELK_TEST_ADDR; |
||||
default: |
||||
return ELK_DEV_ADDR; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue