Search in sources :

Example 1 with User

use of com.hccake.ballcat.common.security.userdetails.User in project ballcat by ballcat-projects.

the class CustomAccessTokenConverter method convertAccessToken.

@Override
@SuppressWarnings("unchecked")
public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    Map<String, Object> response = (Map<String, Object>) super.convertAccessToken(token, authentication);
    // 根据 OAuth2 自省端点协议,scope 应返回字符串,用空格间隔
    // https://datatracker.ietf.org/doc/html/rfc7662
    Object scopeValue = response.get("scope");
    if (scopeValue instanceof Collection) {
        Collection<String> scopes = (Collection<String>) scopeValue;
        response.put("scope", CollectionUtil.join(scopes, " "));
    }
    // 是否是客户端
    boolean isClient = authentication.getPrincipal().getClass().isAssignableFrom(ClientPrincipal.class);
    response.put("is_client", isClient);
    if (isClient) {
        return response;
    }
    // TODO 使用 Scope 进行校验
    // 默认的 CustomTokenEnhancer 在登录获取 token 时只在 attribute 中存放了 ROLE 和 PERMISSION
    // 如果是自己系统内部认可的远程 资源服务器,在拥有权限的情况下,把所有的属性都返回回去
    // 因为实际业务中,可能会在 attributes 中存放一些敏感信息,比如数据权限相关属性
    Collection<? extends GrantedAuthority> requestClientAuthorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
    if (CollectionUtil.isEmpty(requestClientAuthorities)) {
        return response;
    }
    for (GrantedAuthority authority : requestClientAuthorities) {
        if ("all_attribute".equals(authority.getAuthority())) {
            User principal = (User) authentication.getPrincipal();
            Map<String, Object> attributes = principal.getAttributes();
            response.put("attributes", attributes);
            break;
        }
    }
    return response;
}
Also used : User(com.hccake.ballcat.common.security.userdetails.User) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Collection(java.util.Collection) Map(java.util.Map)

Example 2 with User

use of com.hccake.ballcat.common.security.userdetails.User in project ballcat by ballcat-projects.

the class CustomOperationLogHandler method buildLog.

@Override
public OperationLog buildLog(OperationLogging operationLogging, ProceedingJoinPoint joinPoint) {
    // 获取 Request
    HttpServletRequest request = LogUtils.getHttpServletRequest();
    // @formatter:off
    OperationLog operationLog = new OperationLog().setCreateTime(LocalDateTime.now()).setIp(IpUtils.getIpAddr(request)).setMethod(request.getMethod()).setUserAgent(request.getHeader("user-agent")).setUri(URLUtil.getPath(request.getRequestURI())).setType(operationLogging.type().getValue()).setMsg(operationLogging.msg()).setParams(getParams(joinPoint)).setTraceId(MDC.get(LogConstant.TRACE_ID));
    // @formatter:on
    // 操作用户
    User user = SecurityUtils.getUser();
    if (user != null) {
        operationLog.setOperator(user.getUsername());
    }
    return operationLog;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.hccake.ballcat.common.security.userdetails.User) OperationLog(com.hccake.ballcat.log.model.entity.OperationLog)

Example 3 with User

use of com.hccake.ballcat.common.security.userdetails.User in project ballcat by ballcat-projects.

the class FillMetaObjectHandle method updateFill.

@Override
public void updateFill(MetaObject metaObject) {
    // 修改时间
    this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    // 修改人
    User user = SecurityUtils.getUser();
    if (user != null) {
        this.strictUpdateFill(metaObject, "updateBy", Integer.class, user.getUserId());
    }
}
Also used : User(com.hccake.ballcat.common.security.userdetails.User)

Example 4 with User

use of com.hccake.ballcat.common.security.userdetails.User in project ballcat by ballcat-projects.

the class FillMetaObjectHandle method insertFill.

@Override
public void insertFill(MetaObject metaObject) {
    // 逻辑删除标识
    this.strictInsertFill(metaObject, "deleted", Long.class, GlobalConstants.NOT_DELETED_FLAG);
    // 创建时间
    this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    // 创建人
    User user = SecurityUtils.getUser();
    if (user != null) {
        this.strictInsertFill(metaObject, "createBy", Integer.class, user.getUserId());
    }
}
Also used : User(com.hccake.ballcat.common.security.userdetails.User)

Example 5 with User

use of com.hccake.ballcat.common.security.userdetails.User 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)

Aggregations

User (com.hccake.ballcat.common.security.userdetails.User)10 HashMap (java.util.HashMap)3 Map (java.util.Map)3 SysUser (com.hccake.ballcat.system.model.entity.SysUser)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 RequiredArgsConstructor (lombok.RequiredArgsConstructor)2 CollectionUtil (cn.hutool.core.collection.CollectionUtil)1 CreateOperationLogging (com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging)1 DeleteOperationLogging (com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging)1 UpdateOperationLogging (com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging)1 BaseResultCode (com.hccake.ballcat.common.model.result.BaseResultCode)1 R (com.hccake.ballcat.common.model.result.R)1 UserAttributeNameConstants (com.hccake.ballcat.common.security.constant.UserAttributeNameConstants)1 SecurityUtils (com.hccake.ballcat.common.security.util.SecurityUtils)1 OperationLog (com.hccake.ballcat.log.model.entity.OperationLog)1 NotifyChannelEnum (com.hccake.ballcat.notify.enums.NotifyChannelEnum)1 Announcement (com.hccake.ballcat.notify.model.entity.Announcement)1