public interface TenantContextHolder
tenantIdHandle(HttpServletRequest, String)从注册用户入口或登录用户入口提取 tenantId 及进行必要的逻辑处理(如: tenantId 存入 ThreadLocal, 或存入 session, 或存入 redis 缓存等).getTenantId(),方便后续用户注册、登录、授权的数据处理(如:sql 添加 tenantId 的条件,注册用户添加 TENANT_tenantId 权限,根据 tenantId 获取角色的权限数据).getTenantId(Authentication) 默认实现方法, 用户已登录的情况下, 获取租户 ID, 直接从 authority 中解析获取.UserCache.getUserFromCache(String)/UmsUserDetailsService 等接口,
可通过 getTenantId() 来获取 tenantId. 登录用户可以通过 Authentication 来获取 tenantId.tenantIdHandle(HttpServletRequest, String) 逻辑,
用户在实现 UserCache/UmsUserDetailsService 等接口中需要 tenantId 时, 调用 getTenantId() 方法即可.tenantIdHandle(HttpServletRequest, String) 逻辑, 再在
实现 UserCache/UmsUserDetailsService 等接口中需要 tenantId 时, 调用 getTenantId() 方法即可.| 限定符和类型 | 方法和说明 |
|---|---|
String |
getTenantId()
获取租户 ID:
1.
|
default String |
getTenantId(org.springframework.security.core.Authentication authentication)
用户已登录的情况下, 获取租户 ID, 直接从
authority 中解析获取. |
default String |
getTenantId(Collection<? extends org.springframework.security.core.GrantedAuthority> authorities)
通过 authorities 获取租户 ID, 直接从
authority 中解析获取. |
default String |
getTenantId(org.springframework.security.core.userdetails.UserDetails userDetails)
通过
UserDetails 获取租户 ID, 直接从 authority 中解析获取. |
String |
tenantIdHandle(javax.servlet.http.HttpServletRequest request,
String tenantId)
提取 tenantId 及进行必要的逻辑处理(如: tenantId 存入 ThreadLocal, 或存入 session, 或存入 redis 缓存等), 方便后续调用.
|
@NonNull String tenantIdHandle(@NonNull javax.servlet.http.HttpServletRequest request, @Nullable String tenantId) throws TenantIdNotFoundException
request - HttpServletRequesttenantId - 租户 id, 有时候用户自定义登录/注册等接口时, 可能 tenantId 通过 uri 模板变量传递, 此时通过此参数传递,
省的对 HttpServletRequest 进行解析, 如果需要对 HttpServletRequest 进行解析时, 此参数为 null.TenantIdNotFoundException - 获取不到租户 ID 异常@NonNull String getTenantId() throws TenantIdNotFoundException
getTenantId(Authentication)
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken) && authentication.isAuthenticated()) {
// 已登录用户获取租户 id
return getTenantId(authentication);
}
TenantIdNotFoundException - 获取不到租户 ID 异常@NonNull default String getTenantId(@NonNull org.springframework.security.core.Authentication authentication) throws TenantIdNotFoundException
authority 中解析获取.authentication - Authentication 必须是登录成功的用户, 未登录用户直接抛 TenantIdNotFoundException 异常.TenantIdNotFoundException - 获取不到租户 ID 异常@NonNull default String getTenantId(@NonNull org.springframework.security.core.userdetails.UserDetails userDetails) throws TenantIdNotFoundException
UserDetails 获取租户 ID, 直接从 authority 中解析获取.userDetails - UserDetailsTenantIdNotFoundException - 获取不到租户 ID 异常@Nullable default String getTenantId(@NonNull Collection<? extends org.springframework.security.core.GrantedAuthority> authorities)
authority 中解析获取.authorities - 用户权限列表Copyright © 2021. All rights reserved.