use of me.zhyd.oauth.request.AuthRequest in project jeecg-boot by jeecgboot.
the class ThirdLoginController method render.
@RequestMapping("/render/{source}")
public void render(@PathVariable("source") String source, HttpServletResponse response) throws IOException {
log.info("第三方登录进入render:" + source);
AuthRequest authRequest = factory.get(source);
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
log.info("第三方登录认证地址:" + authorizeUrl);
response.sendRedirect(authorizeUrl);
}
use of me.zhyd.oauth.request.AuthRequest in project matecloud by matevip.
the class SocialTokenGranter method getOAuth2Authentication.
@Override
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters());
String code = parameters.get("code");
String state = parameters.get("state");
String codeFromRedis = redisService.get(PREFIX + state).toString();
if (StrUtil.isBlank(code)) {
throw new UserDeniedAuthorizationException("未传入请求参数");
}
if (codeFromRedis == null) {
throw new UserDeniedAuthorizationException("openId已过期,请重新发起授权请求");
}
String oauthType = code.split("-")[0];
code = code.split("-")[1];
AuthRequest authRequest = factory.get(oauthType);
AuthCallback authCallback = AuthCallback.builder().code(code).state(state).build();
AuthResponse response = authRequest.login(authCallback);
log.info("【response】= {}", JSON.toJSON(response));
AuthUser authUser = null;
// 第三方登录成功
if (response.getCode() == AuthResponseStatus.SUCCESS.getCode()) {
authUser = (AuthUser) response.getData();
}
log.error("authUser:{}", JSON.toJSON(authUser));
Authentication userAuth = new SocialAuthenticationToken(authUser);
((AbstractAuthenticationToken) userAuth).setDetails(parameters);
try {
userAuth = authenticationManager.authenticate(userAuth);
} catch (AccountStatusException | BadCredentialsException ase) {
// covers expired, locked, disabled cases (mentioned in section 5.2, draft 31)
throw new InvalidGrantException(ase.getMessage());
}
if (userAuth == null || !userAuth.isAuthenticated()) {
throw new InvalidGrantException("Could not authenticate user: " + authUser);
}
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth);
}
use of me.zhyd.oauth.request.AuthRequest in project kms by mahonelau.
the class ThirdLoginController method render.
@RequestMapping("/render/{source}")
public void render(@PathVariable("source") String source, HttpServletResponse response) throws IOException {
log.info("第三方登录进入render:" + source);
AuthRequest authRequest = factory.get(source);
String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
log.info("第三方登录认证地址:" + authorizeUrl);
response.sendRedirect(authorizeUrl);
}
use of me.zhyd.oauth.request.AuthRequest in project kms by mahonelau.
the class ThirdLoginController method loginThird.
@RequestMapping("/{source}/callback")
public String loginThird(@PathVariable("source") String source, AuthCallback callback, ModelMap modelMap) {
log.info("第三方登录进入callback:" + source + " params:" + JSONObject.toJSONString(callback));
AuthRequest authRequest = factory.get(source);
AuthResponse response = authRequest.login(callback);
log.info(JSONObject.toJSONString(response));
Result<JSONObject> result = new Result<JSONObject>();
if (response.getCode() == 2000) {
JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(response.getData()));
String username = data.getString("username");
String avatar = data.getString("avatar");
String uuid = data.getString("uuid");
// 构造第三方登录信息存储对象
ThirdLoginModel tlm = new ThirdLoginModel(source, uuid, username, avatar);
// 判断有没有这个人
// update-begin-author:wangshuai date:20201118 for:修改成查询第三方账户表
LambdaQueryWrapper<SysThirdAccount> query = new LambdaQueryWrapper<SysThirdAccount>();
query.eq(SysThirdAccount::getThirdUserUuid, uuid);
query.eq(SysThirdAccount::getThirdType, source);
List<SysThirdAccount> thridList = sysThirdAccountService.list(query);
SysThirdAccount user = null;
if (thridList == null || thridList.size() == 0) {
// 否则直接创建新账号
user = saveThirdUser(tlm);
} else {
// 已存在 只设置用户名 不设置头像
user = thridList.get(0);
}
// update-begin-author:wangshuai date:20201118 for:从第三方登录查询是否存在用户id,不存在绑定手机号
if (oConvertUtils.isNotEmpty(user.getSysUserId())) {
String sysUserId = user.getSysUserId();
SysUser sysUser = sysUserService.getById(sysUserId);
String token = saveToken(sysUser);
modelMap.addAttribute("token", token);
} else {
modelMap.addAttribute("token", "绑定手机号," + "" + uuid);
}
// update-end-author:wangshuai date:20201118 for:从第三方登录查询是否存在用户id,不存在绑定手机号
// update-begin--Author:wangshuai Date:20200729 for:接口在签名校验失败时返回失败的标识码 issues#1441--------------------
} else {
modelMap.addAttribute("token", "登录失败");
}
// update-end--Author:wangshuai Date:20200729 for:接口在签名校验失败时返回失败的标识码 issues#1441--------------------
result.setSuccess(false);
result.setMessage("第三方登录异常,请联系管理员");
return "thirdLogin";
}
use of me.zhyd.oauth.request.AuthRequest in project ruoyi-vue-pro by YunaiV.
the class SocialUserServiceImpl method getAuthUser0.
/**
* 请求社交平台,获得授权的用户
*
* @param type 社交平台的类型
* @param authCallback 授权回调
* @return 授权的用户
*/
private AuthUser getAuthUser0(Integer type, AuthCallback authCallback) {
AuthRequest authRequest = authRequestFactory.get(SocialTypeEnum.valueOfType(type).getSource());
AuthResponse<?> authResponse = authRequest.login(authCallback);
log.info("[getAuthUser0][请求社交平台 type({}) request({}) response({})]", type, toJsonString(authCallback), toJsonString(authResponse));
if (!authResponse.ok()) {
throw exception(SOCIAL_USER_AUTH_FAILURE, authResponse.getMsg());
}
return (AuthUser) authResponse.getData();
}
Aggregations