Search in sources :

Example 1 with IamAccount

use of com.diboot.iam.entity.IamAccount in project diboot by dibo-software.

the class BaseJwtRealm method doGetAuthenticationInfo.

/**
 * 获取认证信息
 * @param token
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    BaseJwtAuthToken jwtToken = (BaseJwtAuthToken) token;
    String authAccount = (String) jwtToken.getPrincipal();
    if (V.isEmpty(authAccount)) {
        throw new AuthenticationException("无效的用户标识");
    } else {
        // 获取认证方式
        AuthService authService = AuthServiceFactory.getAuthService(jwtToken.getAuthType());
        if (authService == null) {
            jwtToken.clearAuthtoken();
            throw new AuthenticationException("认证类型: " + jwtToken.getAuthType() + " 的AccountAuthService未实现!");
        }
        IamAccount account = authService.getAccount(jwtToken);
        // 登录失败则抛出相关异常
        if (account == null) {
            jwtToken.clearAuthtoken();
            throw new AuthenticationException("用户账号或密码错误!");
        }
        // 获取当前user对象并缓存
        BaseLoginUser loginUser = null;
        BaseService userService = ContextHelper.getBaseServiceByEntity(jwtToken.getUserTypeClass());
        if (userService != null) {
            loginUser = (BaseLoginUser) userService.getEntity(account.getUserId());
        } else {
            throw new AuthenticationException("用户 " + jwtToken.getUserTypeClass().getName() + " 相关的Service未定义!");
        }
        if (loginUser == null) {
            throw new AuthenticationException("用户不存在");
        }
        loginUser.setAuthToken(jwtToken.getAuthtoken());
        IamExtensible iamExtensible = getIamUserRoleService().getIamExtensible();
        if (iamExtensible != null) {
            LabelValue extentionObj = iamExtensible.getUserExtentionObj(jwtToken.getUserTypeClass().getSimpleName(), account.getUserId(), jwtToken.getExtObj());
            if (extentionObj != null) {
                loginUser.setExtentionObj(extentionObj);
            }
        }
        // 清空当前用户缓存
        this.clearCachedAuthorizationInfo(IamSecurityUtils.getSubject().getPrincipals());
        return new SimpleAuthenticationInfo(loginUser, jwtToken.getCredentials(), this.getName());
    }
}
Also used : IamAccount(com.diboot.iam.entity.IamAccount) IamExtensible(com.diboot.iam.auth.IamExtensible) LabelValue(com.diboot.core.vo.LabelValue) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) BaseLoginUser(com.diboot.iam.entity.BaseLoginUser) AuthService(com.diboot.iam.auth.AuthService) BaseService(com.diboot.core.service.BaseService)

Example 2 with IamAccount

use of com.diboot.iam.entity.IamAccount in project diboot by dibo-software.

the class IamAccountServiceImpl method getAuthAccount.

@Override
public String getAuthAccount(String userType, Long userId) {
    LambdaQueryWrapper<IamAccount> queryWrapper = new QueryWrapper<IamAccount>().lambda().select(IamAccount::getAuthAccount).eq(IamAccount::getUserType, userType).eq(IamAccount::getUserId, userId);
    IamAccount account = getSingleEntity(queryWrapper);
    return account != null ? account.getAuthAccount() : null;
}
Also used : IamAccount(com.diboot.iam.entity.IamAccount) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)

Example 3 with IamAccount

use of com.diboot.iam.entity.IamAccount in project diboot by dibo-software.

the class WxMaMemberAuthServiceImpl method getAndSaveWxMember.

@Override
public IamMemberVO getAndSaveWxMember(WxMemberDTO wxInfoDTO) {
    // 校验用户是否存在,如果已经存在,那么直接返回数据
    IamMember iamMember = iamMemberService.getSingleEntity(Wrappers.<IamMember>lambdaQuery().eq(IamMember::getOpenid, wxInfoDTO.getOpenid()).eq(IamMember::getStatus, Cons.DICTCODE_ACCOUNT_STATUS.A.name()));
    if (V.notEmpty(iamMember)) {
        return Binder.convertAndBindRelations(iamMember, IamMemberVO.class);
    }
    // 创建微信用户基本信息
    IamMember wxMember = maInfo2IamMemberEntity(wxInfoDTO).setUserId(0L).setOrgId(0L).setUserType(IamMember.class.getSimpleName());
    boolean success = iamMemberService.createEntity(wxMember);
    if (!success) {
        throw new BusinessException(Status.FAIL_OPERATION, "创建用户信息失败!");
    }
    // 创建当前用户的账户
    IamAccount iamAccount = createIamAccountEntity(wxMember, wxMember.getId(), IamMember.class);
    success = iamAccountService.createEntity(iamAccount);
    if (!success) {
        throw new BusinessException(Status.FAIL_OPERATION, "创建系统账户失败!");
    }
    return Binder.convertAndBindRelations(wxMember, IamMemberVO.class);
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamAccount(com.diboot.iam.entity.IamAccount) IamMember(com.diboot.mobile.entity.IamMember)

Example 4 with IamAccount

use of com.diboot.iam.entity.IamAccount in project diboot by dibo-software.

the class WxMaUserAuthServiceImpl method bindWxMa.

@Override
public IamMember bindWxMa(WxMemberDTO wxInfoDTO) throws Exception {
    IamUser iamUser = IamSecurityUtils.getCurrentUser();
    if (V.isEmpty(iamUser)) {
        throw new BusinessException(Status.FAIL_OPERATION, "请登陆后绑定");
    }
    // 获取用户信息
    IamMember iamMember = iamMemberService.getSingleEntity(Wrappers.<IamMember>lambdaQuery().eq(IamMember::getUserType, IamUser.class.getSimpleName()).eq(IamMember::getUserId, iamUser.getId()));
    if (V.notEmpty(iamMember)) {
        throw new BusinessException(Status.FAIL_OPERATION, "用户已经绑定");
    }
    iamMember = maInfo2IamMemberEntity(wxInfoDTO).setUserId(iamUser.getId()).setOrgId(iamUser.getOrgId()).setUserType(IamUser.class.getSimpleName());
    boolean success = iamMemberService.createEntity(iamMember);
    if (!success) {
        throw new BusinessException(Status.FAIL_OPERATION, "绑定用户信息失败!");
    }
    // 创建当前用户的账户
    IamAccount iamAccount = createIamAccountEntity(iamMember, iamMember.getUserId(), IamUser.class);
    success = iamAccountService.createEntity(iamAccount);
    if (!success) {
        throw new BusinessException(Status.FAIL_OPERATION, "创建系统账户失败!");
    }
    return iamMember;
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamAccount(com.diboot.iam.entity.IamAccount) IamUser(com.diboot.iam.entity.IamUser) IamMember(com.diboot.mobile.entity.IamMember)

Example 5 with IamAccount

use of com.diboot.iam.entity.IamAccount in project diboot by dibo-software.

the class WxMpUserAuthServiceImpl method bindWxMp.

@Override
public IamMember bindWxMp(String code, String state) throws Exception {
    // 校验STATE
    if (V.notEmpty(STATE) && !STATE.equals(state)) {
        throw new BusinessException(Status.FAIL_INVALID_PARAM, "非法来源");
    }
    if (V.isEmpty(code)) {
        log.error("请求参数有误: code = null");
        throw new BusinessException(Status.FAIL_INVALID_PARAM, "请求参数有误: code is null");
    }
    IamUser iamUser = IamSecurityUtils.getCurrentUser();
    if (V.isEmpty(iamUser)) {
        throw new BusinessException(Status.FAIL_OPERATION, "请登陆后绑定");
    }
    WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
    // 获取用户信息
    IamMember iamMember = iamMemberService.getSingleEntity(Wrappers.<IamMember>lambdaQuery().eq(IamMember::getUserType, IamUser.class.getSimpleName()).eq(IamMember::getUserId, iamUser.getId()));
    if (V.notEmpty(iamMember)) {
        throw new BusinessException(Status.FAIL_OPERATION, "请勿重新绑定");
    }
    // 创建绑定
    WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
    iamMember = mpInfo2IamMemberEntity(userInfo).setUserId(iamUser.getId()).setOrgId(iamUser.getOrgId()).setUserType(IamUser.class.getSimpleName());
    iamMemberService.createEntity(iamMember);
    // 基于openId 创建iam_account账号
    IamAccount iamAccount = createIamAccountEntity(iamMember, iamMember.getUserId(), IamUser.class);
    iamAccountService.createEntity(iamAccount);
    return iamMember;
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) IamAccount(com.diboot.iam.entity.IamAccount) IamUser(com.diboot.iam.entity.IamUser) IamMember(com.diboot.mobile.entity.IamMember) WxOAuth2AccessToken(me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken) WxOAuth2UserInfo(me.chanjar.weixin.common.bean.WxOAuth2UserInfo)

Aggregations

IamAccount (com.diboot.iam.entity.IamAccount)12 BusinessException (com.diboot.core.exception.BusinessException)5 IamMember (com.diboot.mobile.entity.IamMember)4 AuthenticationException (org.apache.shiro.authc.AuthenticationException)4 IamUser (com.diboot.iam.entity.IamUser)3 WxOAuth2AccessToken (me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken)3 Transactional (org.springframework.transaction.annotation.Transactional)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)2 MobileCredential (com.diboot.mobile.dto.MobileCredential)2 WxOAuth2UserInfo (me.chanjar.weixin.common.bean.WxOAuth2UserInfo)2 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)1 BaseService (com.diboot.core.service.BaseService)1 LabelValue (com.diboot.core.vo.LabelValue)1 AuthService (com.diboot.iam.auth.AuthService)1 IamExtensible (com.diboot.iam.auth.IamExtensible)1 BaseLoginUser (com.diboot.iam.entity.BaseLoginUser)1 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)1