Merge branch 'master' of 837152891/pigx into dev
This commit is contained in:
commit
cf4cc58f5e
6 files changed
+164
-2
No files matched your search
@@ -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<PigxUser> userInfo(@RequestParam("access_token") String accessToken){
|
||||
return dealService.getUserInfo(accessToken);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<PigxUser> 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<PigxUser>(userInfo, HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -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";
|
||||
|
||||
/**
|
||||
* 码云获取用户信息
|
||||
*/
|
||||
|
||||
+1
-1
@@ -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;
|
||||
}
|
||||
|
||||
+121
@@ -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
|
||||
* <p>
|
||||
* 通过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.<SysUser>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<SysUser> userList = sysUserService
|
||||
.list(Wrappers.<SysUser>query().lambda().eq(SysUser::getQqOpenid, identify));
|
||||
|
||||
// 先把原有绑定关系去除,设置绑定为NULL
|
||||
if (CollUtil.isNotEmpty(userList)) {
|
||||
SysUser condition = new SysUser();
|
||||
condition.setQqOpenid(identify);
|
||||
sysUserService.update(condition, Wrappers.<SysUser>lambdaUpdate().set(SysUser::getQqOpenid, null));
|
||||
log.info("QQ账号 {} 更换账号绑定", identify);
|
||||
}
|
||||
|
||||
user.setQqOpenid(identify);
|
||||
sysUserService.updateById(user);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -79,7 +79,7 @@ public class SysSocialDetailsServiceImpl extends ServiceImpl<SysSocialDetailsMap
|
||||
public UserInfo getUserInfo(String inStr) {
|
||||
String[] inStrs = inStr.split(StringPool.AT);
|
||||
String type = inStrs[0];
|
||||
String loginStr = inStrs[1];
|
||||
String loginStr = inStr.substring(type.length() + 1);
|
||||
return loginHandlerMap.get(type).handle(loginStr);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user