You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.2 KiB
109 lines
3.2 KiB
package com.qs.serve.common.util;
|
|
|
|
import com.qs.serve.common.framework.security.model.LoginUser;
|
|
import com.qs.serve.common.framework.security.model.LoginUserType;
|
|
import com.qs.serve.common.model.consts.GySysConst;
|
|
import com.qs.serve.common.model.enums.HttpCode;
|
|
import lombok.experimental.UtilityClass;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* @author YenHex
|
|
* @since 2022/3/1
|
|
*/
|
|
@UtilityClass
|
|
public class AuthContextUtils {
|
|
|
|
private final static ThreadLocal<String> THREAD_TENANT = new ThreadLocal<>();
|
|
|
|
public static String getAppId(){
|
|
return getAppId(true);
|
|
}
|
|
|
|
public static String getAppId(boolean throwEx){
|
|
String appId = ServletUtils.getHeader(GySysConst.APP_ID_PROP);
|
|
if(throwEx&&StringUtils.isEmpty(appId)){
|
|
Assert.throwEx(HttpCode.LOGIN_ERR_4002);
|
|
}
|
|
return appId;
|
|
}
|
|
|
|
/**
|
|
* 获取用户登录信息
|
|
* @return
|
|
*/
|
|
public static LoginUser getLoginUser(){
|
|
return getLoginUser(true);
|
|
}
|
|
|
|
/**
|
|
* 获取微信用户ID
|
|
* @return
|
|
*/
|
|
public static String getWxUserId(){
|
|
LoginUser loginUser = getLoginUser();
|
|
if(loginUser.getTypeFlag().equals(LoginUserType.APP_USER)){
|
|
return loginUser.getUserId();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 系统用户ID
|
|
* @return
|
|
*/
|
|
public static String getSysUserId(){
|
|
LoginUser loginUser = getLoginUser();
|
|
if(loginUser.getTypeFlag().equals(LoginUserType.SYS_SUP_USER)
|
|
||loginUser.getTypeFlag().equals(LoginUserType.SYS_USER)){
|
|
return loginUser.getUserId();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 判断是否超级管理员
|
|
* @return
|
|
*/
|
|
public static boolean isSupAdmin(){
|
|
return Objects.requireNonNull(getLoginUser(false))
|
|
.getTypeFlag().equals(LoginUserType.SYS_SUP_USER);
|
|
}
|
|
|
|
public static void setTenant(String value){THREAD_TENANT.set(value);}
|
|
|
|
public static String getTenant(){return THREAD_TENANT.get();}
|
|
|
|
public static void removeTenant(){THREAD_TENANT.remove();}
|
|
|
|
public static LoginUser getLoginUser(boolean throwEx){
|
|
UserDetails details = getUserDetail(throwEx);
|
|
if(details instanceof LoginUser){
|
|
return (LoginUser) details;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static UserDetails getUserDetail(boolean throwEx){
|
|
SecurityContext ctx = SecurityContextHolder.getContext();
|
|
Authentication auth = ctx.getAuthentication();
|
|
if (auth != null) {
|
|
Object authPri = auth.getPrincipal();
|
|
if (authPri instanceof UserDetails) {
|
|
UserDetails details = (UserDetails) authPri;
|
|
return details;
|
|
}
|
|
}
|
|
if(throwEx){
|
|
throw new UsernameNotFoundException("未登录或被登出");
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|