1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| public class AiopsUserAuthenticationConverter implements UserAuthenticationConverter {
private static final String N_A = "N/A";
@Override public Map<String, ?> convertUserAuthentication(Authentication authentication) { Map<String, Object> response = new LinkedHashMap<>(); response.put(USERNAME, authentication.getName()); if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) { response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities())); } return response; }
@Override public Authentication extractAuthentication(Map<String, ?> responseMap) { if (responseMap.containsKey(USERNAME)) { Collection<? extends GrantedAuthority> authorities = getAuthorities(responseMap); Map<String, ?> map = MapUtil.get(responseMap, SecurityConstants.DETAILS_USER, Map.class); validateTenantId(map); String username = MapUtil.getStr(map, SecurityConstants.DETAILS_USERNAME); Integer id = MapUtil.getInt(map, SecurityConstants.DETAILS_USER_ID); Integer deptId = MapUtil.getInt(map, SecurityConstants.DETAILS_DEPT_ID); Integer tenantId = MapUtil.getInt(map, SecurityConstants.DETAILS_TENANT_ID); String phone = MapUtil.getStr(map, SecurityConstants.DETAILS_PHONE); String avatar = MapUtil.getStr(map, SecurityConstants.DETAILS_AVATAR); AiopsUser user = new AiopsUser(id, deptId, phone, avatar, tenantId, username, N_A, true, true, true, true, authorities); return new UsernamePasswordAuthenticationToken(user, N_A, authorities); } return null; }
private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) { Object authorities = map.get(AUTHORITIES); if (authorities instanceof String) { return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities); } if (authorities instanceof Collection) { return AuthorityUtils.commaSeparatedStringToAuthorityList( StringUtils.collectionToCommaDelimitedString((Collection<?>) authorities)); } return AuthorityUtils.NO_AUTHORITIES; }
private void validateTenantId(Map<String, ?> map) { String headerValue = getCurrentTenantId(); Integer userValue = MapUtil.getInt(map, SecurityConstants.DETAILS_TENANT_ID); if (StrUtil.isNotBlank(headerValue) && !userValue.toString().equals(headerValue)) { log.warn("请求头中的租户ID({})和用户的租户ID({})不一致", headerValue, userValue); throw new AiopsAuth2Exception(AiopsSecurityMessageSourceUtil.getAccessor().getMessage( "AbstractUserDetailsAuthenticationProvider.badTenantId", new Object[] { headerValue }, "Bad tenant ID")); } }
private Optional<HttpServletRequest> getCurrentHttpRequest() { return Optional.ofNullable(RequestContextHolder.getRequestAttributes()).filter( requestAttributes -> ServletRequestAttributes.class.isAssignableFrom(requestAttributes.getClass())) .map(requestAttributes -> ((ServletRequestAttributes) requestAttributes)) .map(ServletRequestAttributes::getRequest); }
private String getCurrentTenantId() { return getCurrentHttpRequest() .map(httpServletRequest -> httpServletRequest.getHeader(CommonConstants.TENANT_ID)).orElse(null); }
}
|