Search in sources :

Example 1 with AuthRequest

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);
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest)

Example 2 with AuthRequest

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);
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest) AuthCallback(me.zhyd.oauth.model.AuthCallback) AuthUser(me.zhyd.oauth.model.AuthUser) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) InvalidGrantException(org.springframework.security.oauth2.common.exceptions.InvalidGrantException) LinkedHashMap(java.util.LinkedHashMap) AuthResponse(me.zhyd.oauth.model.AuthResponse) SocialAuthenticationToken(vip.mate.uaa.social.SocialAuthenticationToken) AccountStatusException(org.springframework.security.authentication.AccountStatusException) UserDeniedAuthorizationException(org.springframework.security.oauth2.common.exceptions.UserDeniedAuthorizationException) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) Authentication(org.springframework.security.core.Authentication)

Example 3 with AuthRequest

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);
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest)

Example 4 with AuthRequest

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";
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest) SysThirdAccount(org.jeecg.modules.system.entity.SysThirdAccount) JSONObject(com.alibaba.fastjson.JSONObject) SysUser(org.jeecg.modules.system.entity.SysUser) ThirdLoginModel(org.jeecg.modules.system.model.ThirdLoginModel) AuthResponse(me.zhyd.oauth.model.AuthResponse) Result(org.jeecg.common.api.vo.Result) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)

Example 5 with AuthRequest

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();
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest) AuthUser(me.zhyd.oauth.model.AuthUser)

Aggregations

AuthRequest (me.zhyd.oauth.request.AuthRequest)16 AuthResponse (me.zhyd.oauth.model.AuthResponse)6 JSONObject (com.alibaba.fastjson.JSONObject)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 AuthUser (me.zhyd.oauth.model.AuthUser)3 Result (org.jeecg.common.api.vo.Result)3 SysThirdAccount (org.jeecg.modules.system.entity.SysThirdAccount)3 SysUser (org.jeecg.modules.system.entity.SysUser)3 ThirdLoginModel (org.jeecg.modules.system.model.ThirdLoginModel)3 ApiOperation (io.swagger.annotations.ApiOperation)2 AuthCallback (me.zhyd.oauth.model.AuthCallback)2 JsonUtils.toJsonString (cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString)1 JapException (com.fujieid.jap.core.exception.JapException)1 JapUserException (com.fujieid.jap.core.exception.JapUserException)1 SocialConfig (com.fujieid.jap.social.SocialConfig)1 User (com.moxi.mogublog.commons.entity.User)1 SocialPlatformType (com.ruoyi.common.enums.SocialPlatformType)1 ServiceException (com.ruoyi.common.exception.ServiceException)1 SocialPlatform (com.ruoyi.iot.domain.SocialPlatform)1 AuthRequestWrap (com.ruoyi.iot.model.login.AuthRequestWrap)1