转载链接:转载
自己写一下避免找不到
一、maven
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.4.49.ALL</version>
</dependency>
二、工具
@Slf4j
@Component
public class AliUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 获取支付宝参数
*
* @param request
* @return
*/
public Map<String, String> getAliPayParam(HttpServletRequest request) {
Map<String, String> map = new HashMap();
Map<String, String[]> requestParams = request.getParameterMap();
for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
// 乱码解决,这段代码在出现乱码时使用
// valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
map.put(name, valueStr);
log.info("接受支付宝回调参数:{}", map);
}
return map;
}
public AlipaySystemOauthTokenResponse getAliUserToken(String code, AlipayClient alipayClient) throws AlipayApiException {
return getAliUserToken(code, alipayClient, 0);
}
/**
* 获取token
*
* @param code auth_code
* @param alipayClient 支付宝构建的客户端
* @param number
* @return
* @throws AlipayApiException
*/
public AlipaySystemOauthTokenResponse getAliUserToken(String code, AlipayClient alipayClient, int number) throws AlipayApiException {
AlipaySystemOauthTokenRequest alipaySystemOauthTokenRequest = new AlipaySystemOauthTokenRequest();
alipaySystemOauthTokenRequest.setGrantType("authorization_code");
alipaySystemOauthTokenRequest.setCode(code);
AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(alipaySystemOauthTokenRequest);
log.info("获得用户+++++++++++++++token:{}+++++++++++++++", oauthTokenResponse.getAccessToken());
log.info("获得用户+++++++++++++++uuid:{}+++++++++++++++", oauthTokenResponse.getUserId());
return oauthTokenResponse;
}
public AlipayUserInfoShareResponse getUserInfo(AlipayClient alipayClient, AlipaySystemOauthTokenResponse aliUserToken) throws AlipayApiException {
return getUserInfo(alipayClient, aliUserToken, 0);
}
//获取支付宝信息
public AlipayUserInfoShareResponse getUserInfo(AlipayClient alipayClient, AlipaySystemOauthTokenResponse aliUserToken, int number) throws AlipayApiException {
AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse infoShareResponse = alipayClient.execute(alipayUserInfoShareRequest, aliUserToken.getAccessToken());
log.info("----------------获得支付宝用户详情:{}", infoShareResponse.getBody());
return infoShareResponse;
}
/**
* 支付宝获取token
*
* @param code
* @param alipayClient
* @return
*/
public AlipaySystemOauthTokenResponse getToken(String code, AlipayClient alipayClient) throws AlipayApiException {
if (redisTemplate.hasKey(CacheKeyEnum.ALI_TOKEN_SN.key())) {
Object aliTokenObj = redisTemplate.opsForValue().get(CacheKeyEnum.ALI_TOKEN_SN.key());
return JSONObject.parseObject((String) aliTokenObj, AlipaySystemOauthTokenResponse.class);
}
AlipaySystemOauthTokenResponse aliUserToken = this.getAliUserToken(code, alipayClient);
log.info("微信token返回:{}", aliUserToken.toString());
if (aliUserToken.isSuccess()) {
redisTemplate.opsForValue().set(CacheKeyEnum.ALI_TOKEN_SN.key(), JSON.toJSONString(aliUserToken), CacheKeyEnum.ALI_TOKEN_SN.timeOut(), TimeUnit.SECONDS);
return aliUserToken;
} else {
throw new BusinessException("微信Token获取失败:" + aliUserToken.getMsg());
}
}
}
三、调用
//构建阿里客户端
AlipayClient alipayClient = aliPayProperty.getByFlag(flag);
//获取阿里用户token 这里面只包含用户ID
AlipaySystemOauthTokenResponse aliUserToken = aliUtils.getToken(code, alipayClient);
//获取用户信息
AlipayUserInfoShareResponse infoShareResponse = aliUtils.getUserInfo(alipayClient, aliUserToken);
if (null == aliUserToken) {
throw new BusinessException("用户信息获取失败");
} else if (!aliUserToken.isSuccess()) {
throw new BusinessException(aliUserToken.getSubMsg());
}
log.info("token信息:{}", JSONUtil.toJsonStr(aliUserToken));