diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java index 8422580d8..4e5a1b131 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/endpoint/PigxTokenEndpoint.java @@ -27,10 +27,12 @@ import com.pig4cloud.pigx.auth.service.PigxTokenDealServiceImpl; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.core.util.R; import com.pig4cloud.pigx.common.security.annotation.Inner; +import com.pig4cloud.pigx.common.security.service.PigxUser; import com.pig4cloud.pigx.common.security.util.SecurityUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpHeaders; +import org.springframework.http.ResponseEntity; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.provider.AuthorizationRequest; import org.springframework.security.oauth2.provider.ClientDetails; @@ -140,4 +142,14 @@ public class PigxTokenEndpoint { return dealService.queryToken(page); } + /** + * 根据访问令牌获取用户信息 + * @param accessToken 访问令牌 + * @return 登录用户信息 + */ + @GetMapping("/user") + public ResponseEntity userInfo(@RequestParam("access_token") String accessToken){ + return dealService.getUserInfo(accessToken); + } + } diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxTokenDealServiceImpl.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxTokenDealServiceImpl.java index 37d26e785..0ed0e6364 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxTokenDealServiceImpl.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/service/PigxTokenDealServiceImpl.java @@ -9,6 +9,7 @@ import com.pig4cloud.pigx.common.core.constant.SecurityConstants; import com.pig4cloud.pigx.common.core.util.KeyStrResolver; import com.pig4cloud.pigx.common.core.util.R; import com.pig4cloud.pigx.common.core.util.SpringContextHolder; +import com.pig4cloud.pigx.common.security.service.PigxUser; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.cache.CacheManager; @@ -18,9 +19,12 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.event.LogoutSuccessEvent; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.OAuth2RefreshToken; +import org.springframework.security.oauth2.common.exceptions.InvalidRequestException; import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.token.TokenStore; import org.springframework.stereotype.Service; @@ -150,4 +154,19 @@ public class PigxTokenDealServiceImpl { return result; } + /** + * 根据访问令牌获取用户信息 + * @param accessToken 访问令牌 + * @return 登录用户信息 + */ + public ResponseEntity getUserInfo(String accessToken) { + OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(accessToken); + if (oAuth2AccessToken == null || StrUtil.isBlank(oAuth2AccessToken.getValue()) || oAuth2AccessToken.isExpired()) { + throw new InvalidRequestException("The access token is invalid or expired"); + } + OAuth2Authentication auth2Authentication = tokenStore.readAuthentication(oAuth2AccessToken); + PigxUser userInfo = (PigxUser) auth2Authentication.getUserAuthentication().getPrincipal(); + return new ResponseEntity(userInfo, HttpStatus.OK); + } + } diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java index fe01e2683..26850f0e7 100644 --- a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/constant/SecurityConstants.java @@ -105,6 +105,11 @@ public interface SecurityConstants { */ String MOBILE_TOKEN_URL = "/mobile/token/*"; + /** + * QQ获取token + */ + String QQ_AUTHORIZATION_CODE_URL = "https://graph.qq.com/oauth2.0/token?grant_type=" + + "authorization_code&code=%S&client_id=%s&redirect_uri=" + "%s&client_secret=%s"; /** * 微信获取OPENID */ @@ -128,6 +133,11 @@ public interface SecurityConstants { */ String OSC_AUTHORIZATION_CODE_URL = "https://www.oschina.net/action/openapi/token"; + /** + * QQ获取用户信息 + */ + String QQ_USER_INFO_URL = "https://graph.qq.com/oauth2.0/me?access_token=%s"; + /** * 码云获取用户信息 */ diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/SmsLoginHandler.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/SmsLoginHandler.java index 1e9fb5127..acb147f7b 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/SmsLoginHandler.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/SmsLoginHandler.java @@ -70,7 +70,7 @@ public class SmsLoginHandler extends AbstractLoginHandler { */ @Override public Boolean bind(SysUser user, String identify) { - user.setGiteeLogin(identify); + user.setPhone(identify); sysUserService.updateById(user); return null; } diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/TencentLoginHandler.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/TencentLoginHandler.java new file mode 100644 index 000000000..ef227e30d --- /dev/null +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/handler/TencentLoginHandler.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.admin.handler; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.pig4cloud.pigx.admin.api.dto.UserInfo; +import com.pig4cloud.pigx.admin.api.entity.SysSocialDetails; +import com.pig4cloud.pigx.admin.api.entity.SysUser; +import com.pig4cloud.pigx.admin.mapper.SysSocialDetailsMapper; +import com.pig4cloud.pigx.admin.service.SysUserService; +import com.pig4cloud.pigx.common.core.constant.SecurityConstants; +import com.pig4cloud.pigx.common.core.constant.enums.LoginTypeEnum; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author lengleng + * @date 2018/11/18 + */ +@Slf4j +@Component("QQ") +@AllArgsConstructor +public class TencentLoginHandler extends AbstractLoginHandler { + + private final SysUserService sysUserService; + + private final SysSocialDetailsMapper sysSocialDetailsMapper; + + /** + * QQ登录传入code + *

+ * 通过code 调用qq 获取唯一标识 + * @param code + * @return + */ + @Override + public String identify(String code) { + SysSocialDetails condition = new SysSocialDetails(); + condition.setType(LoginTypeEnum.QQ.getType()); + SysSocialDetails socialDetails = sysSocialDetailsMapper.selectOne(new QueryWrapper<>(condition)); + + String url = String.format(SecurityConstants.QQ_AUTHORIZATION_CODE_URL, socialDetails.getAppId(), + socialDetails.getAppSecret(), code); + String result = HttpUtil.get(url); + log.debug("QQ响应报文:{}", result); + + String accessToken = JSONUtil.parseObj(result).getStr("access_token"); + String userUrl = String.format(SecurityConstants.QQ_USER_INFO_URL, accessToken); + String resp = HttpUtil.get(userUrl); + log.debug("QQ获取个人信息返回报文{}", resp); + + JSONObject userInfo = JSONUtil.parseObj(resp); + // QQ唯一标识 + String openid = userInfo.getStr("openid"); + return openid; + } + + /** + * openId 获取用户信息 + * @param openId + * @return + */ + @Override + public UserInfo info(String openId) { + SysUser user = sysUserService.getOne(Wrappers.query().lambda().eq(SysUser::getQqOpenid, openId)); + + if (user == null) { + log.info("QQ未绑定:{}", openId); + return null; + } + return sysUserService.findUserInfo(user); + } + + /** + * 绑定逻辑 + * @param user 用户实体 + * @param identify 渠道返回唯一标识 + * @return + */ + @Override + public Boolean bind(SysUser user, String identify) { + List userList = sysUserService + .list(Wrappers.query().lambda().eq(SysUser::getQqOpenid, identify)); + + // 先把原有绑定关系去除,设置绑定为NULL + if (CollUtil.isNotEmpty(userList)) { + SysUser condition = new SysUser(); + condition.setQqOpenid(identify); + sysUserService.update(condition, Wrappers.lambdaUpdate().set(SysUser::getQqOpenid, null)); + log.info("QQ账号 {} 更换账号绑定", identify); + } + + user.setQqOpenid(identify); + sysUserService.updateById(user); + return null; + } + +} diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysSocialDetailsServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysSocialDetailsServiceImpl.java index 825fc65d2..5b6dccd76 100755 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysSocialDetailsServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysSocialDetailsServiceImpl.java @@ -79,7 +79,7 @@ public class SysSocialDetailsServiceImpl extends ServiceImpl