Search in sources :

Example 1 with AuthRequestWrap

use of com.ruoyi.iot.model.login.AuthRequestWrap in project wumei-smart by kerwincui.

the class SocialLoginServiceImpl method renderAuth.

@Override
public String renderAuth(String source, HttpServletRequest httpServletRequest) {
    AuthRequestWrap authRequestWrap = null;
    try {
        authRequestWrap = iAuthRequestFactory.getAuthRequest(source);
        checkSocialPlatform(authRequestWrap.getSocialPlatform());
        return authRequestWrap.getAuthRequest().authorize(AuthStateUtils.createState());
    } catch (AuthException authException) {
        // 返回错误信息
        log.error("", authException);
        if (authRequestWrap != null) {
            String errorId = genErrorId(authException.getMessage());
            return authRequestWrap.getSocialPlatform().getErrorMsgUri() + errorId;
        } else {
            return httpServletRequest.getProtocol() + httpServletRequest.getServerName() + httpServletRequest.getServerPort();
        }
    } catch (Exception exception) {
        // 这类错误 直接不返回,重定向到主页
        log.error("", exception);
        return HTTPS + httpServletRequest.getServerName();
    }
}
Also used : AuthRequestWrap(com.ruoyi.iot.model.login.AuthRequestWrap) AuthException(me.zhyd.oauth.exception.AuthException) AuthException(me.zhyd.oauth.exception.AuthException)

Example 2 with AuthRequestWrap

use of com.ruoyi.iot.model.login.AuthRequestWrap in project wumei-smart by kerwincui.

the class SocialLoginServiceImpl method callback.

@Override
public String callback(String source, AuthCallback authCallback, HttpServletRequest httpServletRequest) {
    AuthRequestWrap authRequestWrap = null;
    try {
        authRequestWrap = iAuthRequestFactory.getAuthRequest(source);
        checkSocialPlatform(authRequestWrap.getSocialPlatform());
        AuthResponse<AuthUser> authResponse = authRequestWrap.getAuthRequest().login(authCallback);
        String bindId = null;
        String loginId = null;
        if (authResponse.ok()) {
            SocialUser socialUser = findSocialUser(authResponse.getData().getUuid(), authResponse.getData().getSource());
            createOrUpdateSocialUser(socialUser, authResponse.getData());
            if (socialUser == null) {
                // 第一次登录
                bindId = genBindId(authResponse.getData());
            } else if (socialUser.getSysUserId() == null || socialUser.getSysUserId() <= 0) {
                // 初次绑定
                bindId = genBindId(authResponse.getData());
            } else {
                // 查看是否已经绑定
                SysUser sysUser = iSysUserService.selectUserById(socialUser.getSysUserId());
                if (sysUser == null) {
                    bindId = genBindId(authResponse.getData());
                } else {
                    // 直接登录跳转
                    loginId = genLoginId(sysUser);
                }
            }
            if (StringUtils.isNotEmpty(bindId)) {
                return authRequestWrap.getSocialPlatform().getBindUri() + bindId;
            } else {
                return authRequestWrap.getSocialPlatform().getRedirectLoginUri() + loginId;
            }
        } else {
            log.error("登录授权异常,code:{}, msg:{}", authResponse.getCode(), authResponse.getMsg());
            String errorId = genErrorId(authResponse.getMsg());
            return authRequestWrap.getSocialPlatform().getErrorMsgUri() + errorId;
        }
    } catch (AuthException authException) {
        // 返回错误信息
        log.error("", authException);
        if (authRequestWrap != null) {
            String errorId = genErrorId(authException.getMessage());
            return authRequestWrap.getSocialPlatform().getErrorMsgUri() + errorId;
        } else {
            return httpServletRequest.getServerName() + httpServletRequest.getServerPort();
        }
    } catch (Exception exception) {
        log.error("", exception);
        return HTTPS + httpServletRequest.getServerName();
    }
}
Also used : AuthRequestWrap(com.ruoyi.iot.model.login.AuthRequestWrap) SysUser(com.ruoyi.common.core.domain.entity.SysUser) SocialUser(com.ruoyi.iot.domain.SocialUser) AuthException(me.zhyd.oauth.exception.AuthException) AuthUser(me.zhyd.oauth.model.AuthUser) AuthException(me.zhyd.oauth.exception.AuthException)

Example 3 with AuthRequestWrap

use of com.ruoyi.iot.model.login.AuthRequestWrap in project wumei-smart by kerwincui.

the class AuthRequestFactoryImpl method getAuthRequest.

/**
 * 获得对于AUthRequest
 *
 * @param source 登录方式
 * @return 对应AuthRequest
 */
@Override
public AuthRequestWrap getAuthRequest(String source) {
    AuthRequestWrap authRequestWrap = new AuthRequestWrap();
    AuthRequest authRequest;
    try {
        SocialPlatformType socialPlatformType = SocialPlatformType.valueOf(source.toUpperCase(Locale.ROOT));
        SocialPlatform socialPlatform = iSocialPlatformService.selectSocialPlatformByPlatform(source);
        authRequestWrap.setSocialPlatform(socialPlatform);
        AuthConfig authConfig = AuthConfig.builder().clientId(socialPlatform.getClientId()).clientSecret(socialPlatform.getSecretKey()).redirectUri(socialPlatform.getRedirectUri()).build();
        switch(socialPlatformType) {
            case QQ:
                {
                    authRequest = new AuthQqRequest(authConfig, authStateRedisCache);
                    break;
                }
            case Wechat:
                {
                    authRequest = new AuthWeChatMpRequest(authConfig, authStateRedisCache);
                    break;
                }
            default:
                {
                    throw new ServiceException("source: " + source + ",暂不支持");
                }
        }
        authRequestWrap.setAuthRequest(authRequest);
        return authRequestWrap;
    } catch (Exception e) {
        throw new ServiceException(e.getMessage());
    }
}
Also used : AuthRequest(me.zhyd.oauth.request.AuthRequest) SocialPlatform(com.ruoyi.iot.domain.SocialPlatform) AuthRequestWrap(com.ruoyi.iot.model.login.AuthRequestWrap) ServiceException(com.ruoyi.common.exception.ServiceException) SocialPlatformType(com.ruoyi.common.enums.SocialPlatformType) AuthQqRequest(me.zhyd.oauth.request.AuthQqRequest) AuthConfig(me.zhyd.oauth.config.AuthConfig) AuthWeChatMpRequest(me.zhyd.oauth.request.AuthWeChatMpRequest) ServiceException(com.ruoyi.common.exception.ServiceException)

Aggregations

AuthRequestWrap (com.ruoyi.iot.model.login.AuthRequestWrap)3 AuthException (me.zhyd.oauth.exception.AuthException)2 SysUser (com.ruoyi.common.core.domain.entity.SysUser)1 SocialPlatformType (com.ruoyi.common.enums.SocialPlatformType)1 ServiceException (com.ruoyi.common.exception.ServiceException)1 SocialPlatform (com.ruoyi.iot.domain.SocialPlatform)1 SocialUser (com.ruoyi.iot.domain.SocialUser)1 AuthConfig (me.zhyd.oauth.config.AuthConfig)1 AuthUser (me.zhyd.oauth.model.AuthUser)1 AuthQqRequest (me.zhyd.oauth.request.AuthQqRequest)1 AuthRequest (me.zhyd.oauth.request.AuthRequest)1 AuthWeChatMpRequest (me.zhyd.oauth.request.AuthWeChatMpRequest)1