Search in sources :

Example 1 with SysUserInfo

use of com.hccake.ballcat.system.model.vo.SysUserInfo in project ballcat by ballcat-projects.

the class CustomTokenEnhancer method enhance.

/**
 * 处理 token 增强
 * @param accessToken token信息
 * @param authentication 鉴权信息
 * @return OAuth2AccessToken 增强后的token
 */
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
    Authentication userAuthentication = authentication.getUserAuthentication();
    if (userAuthentication == null) {
        return accessToken;
    }
    Object principal = userAuthentication.getPrincipal();
    if (principal instanceof User) {
        User user = (User) principal;
        // token 附属信息
        Map<String, Object> additionalInfo = new HashMap<>(8);
        // 用户基本信息
        SysUserInfo sysUserInfo = getSysUserInfo(user);
        additionalInfo.put(TokenAttributeNameConstants.INFO, sysUserInfo);
        // 默认在登陆时只把角色和权限的信息返回
        Map<String, Object> resultAttributes = new HashMap<>(2);
        Map<String, Object> attributes = user.getAttributes();
        resultAttributes.put(UserAttributeNameConstants.ROLE_CODES, attributes.get(UserAttributeNameConstants.ROLE_CODES));
        resultAttributes.put(UserAttributeNameConstants.PERMISSIONS, attributes.get(UserAttributeNameConstants.PERMISSIONS));
        additionalInfo.put(TokenAttributeNameConstants.ATTRIBUTES, resultAttributes);
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
    }
    return accessToken;
}
Also used : User(com.hccake.ballcat.common.security.userdetails.User) HashMap(java.util.HashMap) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) SysUserInfo(com.hccake.ballcat.system.model.vo.SysUserInfo) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 2 with SysUserInfo

use of com.hccake.ballcat.system.model.vo.SysUserInfo in project ballcat by ballcat-projects.

the class SysUserController method getSysUserInfo.

/**
 * 获取指定用户的基本信息
 * @param userId 用户ID
 * @return SysUserInfo
 */
@GetMapping("/{userId}")
@PreAuthorize("@per.hasPermission('system:user:read')")
@Operation(summary = "获取指定用户的基本信息")
public R<SysUserInfo> getSysUserInfo(@PathVariable("userId") Integer userId) {
    SysUser sysUser = sysUserService.getById(userId);
    if (sysUser == null) {
        return R.ok();
    }
    SysUserInfo sysUserInfo = SysUserConverter.INSTANCE.poToInfo(sysUser);
    return R.ok(sysUserInfo);
}
Also used : SysUser(com.hccake.ballcat.system.model.entity.SysUser) SysUserInfo(com.hccake.ballcat.system.model.vo.SysUserInfo) Operation(io.swagger.v3.oas.annotations.Operation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 3 with SysUserInfo

use of com.hccake.ballcat.system.model.vo.SysUserInfo in project ballcat by ballcat-projects.

the class CustomTokenEnhancer method getSysUserInfo.

/**
 * 根据 User 对象获取 SysUserInfo
 * @param user User
 * @return SysUserInfo
 */
public SysUserInfo getSysUserInfo(User user) {
    SysUserInfo sysUserInfo = new SysUserInfo();
    sysUserInfo.setUserId(user.getUserId());
    sysUserInfo.setUsername(user.getUsername());
    sysUserInfo.setNickname(user.getNickname());
    sysUserInfo.setAvatar(user.getAvatar());
    sysUserInfo.setOrganizationId(user.getOrganizationId());
    sysUserInfo.setType(user.getType());
    return sysUserInfo;
}
Also used : SysUserInfo(com.hccake.ballcat.system.model.vo.SysUserInfo)

Aggregations

SysUserInfo (com.hccake.ballcat.system.model.vo.SysUserInfo)3 User (com.hccake.ballcat.common.security.userdetails.User)1 SysUser (com.hccake.ballcat.system.model.entity.SysUser)1 Operation (io.swagger.v3.oas.annotations.Operation)1 HashMap (java.util.HashMap)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 Authentication (org.springframework.security.core.Authentication)1 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)1 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)1