Search in sources :

Example 1 with ConnectAuthUser

use of cn.lili.modules.connect.entity.dto.ConnectAuthUser in project lilishop by lilishop.

the class MemberServiceImpl method loginBindUser.

/**
 * 成功登录,则检测cookie中的信息,进行会员绑定
 *
 * @param member 会员
 */
private void loginBindUser(Member member) {
    // 获取cookie存储的信息
    String uuid = CookieUtil.getCookie(ConnectService.CONNECT_COOKIE, ThreadContextHolder.getHttpRequest());
    String connectType = CookieUtil.getCookie(ConnectService.CONNECT_TYPE, ThreadContextHolder.getHttpRequest());
    // 如果联合登陆存储了信息
    if (CharSequenceUtil.isNotEmpty(uuid) && CharSequenceUtil.isNotEmpty(connectType)) {
        try {
            // 获取信息
            ConnectAuthUser connectAuthUser = getConnectAuthUser(uuid, connectType);
            if (connectAuthUser == null) {
                return;
            }
            Connect connect = connectService.queryConnect(ConnectQueryDTO.builder().unionId(connectAuthUser.getUuid()).unionType(connectType).build());
            if (connect == null) {
                connect = new Connect(member.getId(), connectAuthUser.getUuid(), connectType);
                connectService.save(connect);
            }
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("绑定第三方联合登陆失败:", e);
        } finally {
            // 联合登陆成功与否,都清除掉cookie中的信息
            CookieUtil.delCookie(ConnectService.CONNECT_COOKIE, ThreadContextHolder.getHttpResponse());
            CookieUtil.delCookie(ConnectService.CONNECT_TYPE, ThreadContextHolder.getHttpResponse());
        }
    }
}
Also used : ServiceException(cn.lili.common.exception.ServiceException) Connect(cn.lili.modules.connect.entity.Connect) ConnectAuthUser(cn.lili.modules.connect.entity.dto.ConnectAuthUser) ServiceException(cn.lili.common.exception.ServiceException)

Example 2 with ConnectAuthUser

use of cn.lili.modules.connect.entity.dto.ConnectAuthUser in project lilishop by lilishop.

the class BaseAuthRequest method login.

/**
 * 统一的登录入口。当通过{@link AuthRequest#login(AuthCallback)} (String)}授权成功后,会跳转到调用方的相关回调方法中
 * 方法的入参可以使用{@code AuthCallback},{@code AuthCallback}类中封装好了OAuth2授权回调所需要的参数
 *
 * @param authCallback 用于接收回调参数的实体
 * @return AuthResponse
 */
@Override
public AuthResponse login(AuthCallback authCallback) {
    try {
        AuthChecker.checkCode(source, authCallback);
        AuthToken authToken = this.getAccessToken(authCallback);
        ConnectAuthUser user = this.getUserInfo(authToken);
        return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(user).build();
    } catch (Exception e) {
        log.error("Failed to login with oauth authorization.", e);
        return this.responseError(e);
    }
}
Also used : AuthToken(cn.lili.modules.connect.entity.dto.AuthToken) ConnectAuthUser(cn.lili.modules.connect.entity.dto.ConnectAuthUser) AuthException(cn.lili.modules.connect.exception.AuthException)

Example 3 with ConnectAuthUser

use of cn.lili.modules.connect.entity.dto.ConnectAuthUser in project lilishop by lilishop.

the class ConnectUtil method callback.

/**
 * 登录回调
 *
 * @param type
 * @param callback
 * @param httpServletResponse
 * @param httpServletRequest
 * @throws IOException
 */
public void callback(String type, AuthCallback callback, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
    AuthRequest authRequest = this.getAuthRequest(type);
    AuthResponse<ConnectAuthUser> response = authRequest.login(callback);
    ResultMessage<Object> resultMessage;
    // 联合登陆处理,如果响应正常,则录入响应结果到redis
    if (response.ok()) {
        ConnectAuthUser authUser = response.getData();
        Token token;
        try {
            token = connectService.unionLoginCallback(type, authUser, callback.getState());
            resultMessage = ResultUtil.data(token);
        } catch (ServiceException e) {
            throw new ServiceException(ResultCode.ERROR, e.getMessage());
        }
    } else // 否则录入响应结果,等待前端获取信息
    {
        throw new ServiceException(ResultCode.ERROR, response.getMsg());
    }
    // 缓存写入登录结果,300秒有效
    cache.put(CachePrefix.CONNECT_RESULT.getPrefix() + callback.getCode(), resultMessage, 300L);
    // 跳转地址
    String url = this.check(httpServletRequest.getHeader("user-agent")) ? domainProperties.getWap() + "/pages/passport/login?state=" + callback.getCode() : domainProperties.getPc() + "/login?state=" + callback.getCode();
    try {
        httpServletResponse.sendRedirect(url);
    } catch (Exception e) {
        log.error("登录回调错误", e);
    }
}
Also used : AuthRequest(cn.lili.modules.connect.request.AuthRequest) ServiceException(cn.lili.common.exception.ServiceException) Token(cn.lili.common.security.token.Token) ConnectAuthUser(cn.lili.modules.connect.entity.dto.ConnectAuthUser) ServiceException(cn.lili.common.exception.ServiceException) IOException(java.io.IOException) AuthException(cn.lili.modules.connect.exception.AuthException)

Example 4 with ConnectAuthUser

use of cn.lili.modules.connect.entity.dto.ConnectAuthUser in project lilishop by lilishop.

the class MemberServiceImpl method checkConnectUser.

/**
 * 检测是否可以绑定第三方联合登陆
 * 返回null原因
 * 包含原因1:redis中已经没有联合登陆信息  2:已绑定其他账号
 *
 * @return 返回对象则代表可以进行绑定第三方会员,返回null则表示联合登陆无法继续
 */
private ConnectAuthUser checkConnectUser() {
    // 获取cookie存储的信息
    String uuid = CookieUtil.getCookie(ConnectService.CONNECT_COOKIE, ThreadContextHolder.getHttpRequest());
    String connectType = CookieUtil.getCookie(ConnectService.CONNECT_TYPE, ThreadContextHolder.getHttpRequest());
    // 如果联合登陆存储了信息
    if (CharSequenceUtil.isNotEmpty(uuid) && CharSequenceUtil.isNotEmpty(connectType)) {
        // 枚举 联合登陆类型获取
        ConnectAuthEnum authInterface = ConnectAuthEnum.valueOf(connectType);
        ConnectAuthUser connectAuthUser = getConnectAuthUser(uuid, connectType);
        if (connectAuthUser == null) {
            throw new ServiceException(ResultCode.USER_OVERDUE_CONNECT_ERROR);
        }
        // 检测是否已经绑定过用户
        Connect connect = connectService.queryConnect(ConnectQueryDTO.builder().unionType(connectType).unionId(connectAuthUser.getUuid()).build());
        // 没有关联则返回true,表示可以继续绑定
        if (connect == null) {
            connectAuthUser.setConnectEnum(authInterface);
            return connectAuthUser;
        } else {
            throw new ServiceException(ResultCode.USER_CONNECT_BANDING_ERROR);
        }
    } else {
        throw new ServiceException(ResultCode.USER_CONNECT_NOT_EXIST_ERROR);
    }
}
Also used : ServiceException(cn.lili.common.exception.ServiceException) Connect(cn.lili.modules.connect.entity.Connect) ConnectAuthUser(cn.lili.modules.connect.entity.dto.ConnectAuthUser) ConnectAuthEnum(cn.lili.modules.connect.config.ConnectAuthEnum)

Aggregations

ConnectAuthUser (cn.lili.modules.connect.entity.dto.ConnectAuthUser)4 ServiceException (cn.lili.common.exception.ServiceException)3 Connect (cn.lili.modules.connect.entity.Connect)2 AuthException (cn.lili.modules.connect.exception.AuthException)2 Token (cn.lili.common.security.token.Token)1 ConnectAuthEnum (cn.lili.modules.connect.config.ConnectAuthEnum)1 AuthToken (cn.lili.modules.connect.entity.dto.AuthToken)1 AuthRequest (cn.lili.modules.connect.request.AuthRequest)1 IOException (java.io.IOException)1