|
|
@ -43,6 +43,14 @@ public class CustomTokenServices extends DefaultTokenServices { |
|
|
|
|
|
|
|
private AuthenticationManager authenticationManager; |
|
|
|
|
|
|
|
// 新增TransportClient依赖 |
|
|
|
private TransportClient transportClient; |
|
|
|
|
|
|
|
// 新增TransportClient的setter方法 |
|
|
|
public void setTransportClient(TransportClient transportClient) { |
|
|
|
this.transportClient = transportClient; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public OAuth2AccessToken createAccessToken(OAuth2Authentication authentication) throws AuthenticationException { |
|
|
|
OAuth2AccessToken existingAccessToken = tokenStore.getAccessToken(authentication); |
|
|
@ -97,7 +105,22 @@ public class CustomTokenServices extends DefaultTokenServices { |
|
|
|
return null; |
|
|
|
} |
|
|
|
int validitySeconds = getRefreshTokenValiditySeconds(authentication.getOAuth2Request()); |
|
|
|
String tokenValue = new String(Base64.encodeBase64URLSafe(DEFAULT_TOKEN_GENERATOR.generateKey()), US_ASCII); |
|
|
|
// 修改点1:根据认证模式区分来源 |
|
|
|
String grantType = authentication.getOAuth2Request().getGrantType(); |
|
|
|
boolean isClientCredential = "client_credentials".equals(grantType); |
|
|
|
|
|
|
|
String tokenValue = ""; |
|
|
|
if(isClientCredential) { |
|
|
|
try { |
|
|
|
tokenValue = transportClient.getToken(); // 假设TransportClient有getToken()方法 |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
throw new AuthenticationException("Failed to generate token via TransportClient", e) {}; |
|
|
|
} |
|
|
|
} else { |
|
|
|
tokenValue = new String(Base64.encodeBase64URLSafe(DEFAULT_TOKEN_GENERATOR.generateKey()), US_ASCII); |
|
|
|
} |
|
|
|
|
|
|
|
if (validitySeconds > 0) { |
|
|
|
return new DefaultExpiringOAuth2RefreshToken(tokenValue, new Date(System.currentTimeMillis() |
|
|
|
+ (validitySeconds * 1000L))); |
|
|
@ -106,11 +129,19 @@ public class CustomTokenServices extends DefaultTokenServices { |
|
|
|
} |
|
|
|
|
|
|
|
private OAuth2AccessToken createAccessToken(OAuth2Authentication authentication, OAuth2RefreshToken refreshToken) { |
|
|
|
String sourceType = ServletUtils.getRequest().getHeader("SourceType"); |
|
|
|
// 第三方客户端调用 |
|
|
|
if(StringUtils.isEmpty(sourceType)) { |
|
|
|
sourceType = "data_exchange"; |
|
|
|
} |
|
|
|
|
|
|
|
// 修改点1:根据认证模式区分来源 |
|
|
|
String grantType = authentication.getOAuth2Request().getGrantType(); |
|
|
|
boolean isClientCredential = "client_credentials".equals(grantType); |
|
|
|
|
|
|
|
// 修改点2:优先通过认证模式判断 |
|
|
|
String sourceType = isClientCredential ? "data_exchange_feign" : "data_exchange_login"; |
|
|
|
|
|
|
|
// String sourceType = ServletUtils.getRequest().getHeader("SourceType"); |
|
|
|
// // 第三方客户端调用 |
|
|
|
// if(StringUtils.isEmpty(sourceType)) { |
|
|
|
// sourceType = "data_exchange"; |
|
|
|
// } |
|
|
|
//密码模式 |
|
|
|
// if(authentication.getUserAuthentication() != null && authentication.getUserAuthentication() instanceof UsernamePasswordAuthenticationToken) { |
|
|
|
// UsernamePasswordAuthenticationToken userAuthentication = (UsernamePasswordAuthenticationToken) authentication.getUserAuthentication(); |
|
|
@ -118,8 +149,19 @@ public class CustomTokenServices extends DefaultTokenServices { |
|
|
|
// LinkedHashMap details = (LinkedHashMap) userAuthentication.getDetails(); |
|
|
|
// sourceType = details.get("sourceType") + "_"; |
|
|
|
// } |
|
|
|
String tokenValue = ""; |
|
|
|
if(isClientCredential) { |
|
|
|
try { |
|
|
|
tokenValue = transportClient.getToken(); // 假设TransportClient有getToken()方法 |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
throw new AuthenticationException("Failed to generate token via TransportClient", e) {}; |
|
|
|
} |
|
|
|
} else { |
|
|
|
tokenValue = new String(Base64.encodeBase64URLSafe(DEFAULT_TOKEN_GENERATOR.generateKey()), US_ASCII); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String tokenValue = new String(Base64.encodeBase64URLSafe(DEFAULT_TOKEN_GENERATOR.generateKey()), US_ASCII); |
|
|
|
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(sourceType + "_" + tokenValue); |
|
|
|
int validitySeconds = getAccessTokenValiditySeconds(authentication.getOAuth2Request()); |
|
|
|
if (validitySeconds > 0) { |
|
|
|