|
|
@@ -1,16 +1,22 @@
|
|
|
package cc.mrbird.febs.common.utils;
|
|
|
|
|
|
+import cc.mrbird.febs.common.entity.CurrentUser;
|
|
|
import cc.mrbird.febs.common.entity.constant.PageConstant;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
|
|
+import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.IntStream;
|
|
|
@@ -124,4 +130,69 @@ public class FebsUtil {
|
|
|
Matcher m = p.matcher(value);
|
|
|
return m.find();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取在线用户信息
|
|
|
+ *
|
|
|
+ * @return CurrentUser 当前用户信息
|
|
|
+ */
|
|
|
+ public static CurrentUser getCurrentUser() {
|
|
|
+ try {
|
|
|
+ LinkedHashMap<String, Object> authenticationDetails = getAuthenticationDetails();
|
|
|
+ Object principal = authenticationDetails.get("principal");
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ return mapper.readValue(mapper.writeValueAsString(principal), CurrentUser.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("获取当前用户信息失败", e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前用户名称
|
|
|
+ *
|
|
|
+ * @return String 用户名
|
|
|
+ */
|
|
|
+ public static String getCurrentUsername() {
|
|
|
+ return (String) getOAuth2Authentication().getPrincipal();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前用户权限集
|
|
|
+ *
|
|
|
+ * @return Collection<GrantedAuthority>权限集
|
|
|
+ */
|
|
|
+ public static Collection<GrantedAuthority> getCurrentUserAuthority() {
|
|
|
+ return getOAuth2Authentication().getAuthorities();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前令牌内容
|
|
|
+ *
|
|
|
+ * @return String 令牌内容
|
|
|
+ */
|
|
|
+ public static String getCurrentTokenValue() {
|
|
|
+ OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) getOAuth2Authentication().getDetails();
|
|
|
+ return details.getTokenValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前请求IP地址
|
|
|
+ *
|
|
|
+ * @return String IP地址
|
|
|
+ */
|
|
|
+ public static String getCurrentRequestIpAddress() {
|
|
|
+ OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) getOAuth2Authentication().getDetails();
|
|
|
+ return details.getRemoteAddress();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static OAuth2Authentication getOAuth2Authentication() {
|
|
|
+ Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
+ return (OAuth2Authentication) authentication;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private static LinkedHashMap<String, Object> getAuthenticationDetails() {
|
|
|
+ return (LinkedHashMap<String, Object>) getOAuth2Authentication().getUserAuthentication().getDetails();
|
|
|
+ }
|
|
|
}
|