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;
}
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;
}
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());
}
}
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());
}
}
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;
}
Aggregations