Procházet zdrojové kódy

添加几个实用工具方法

mrbird před 6 roky
rodič
revize
7217b15bef

+ 45 - 0
febs-common/src/main/java/cc/mrbird/febs/common/entity/CurrentUser.java

@@ -0,0 +1,45 @@
+package cc.mrbird.febs.common.entity;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.security.core.GrantedAuthority;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.Set;
+
+/**
+ * @author MrBird
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class CurrentUser implements Serializable {
+
+    private static long serialVersionUID = 761748087824726463L;
+
+    @JsonIgnore
+    private String password;
+    private String username;
+    @JsonIgnore
+    private Set<GrantedAuthority> authorities;
+    private boolean accountNonExpired;
+    private boolean accountNonLocked;
+    private boolean credentialsNonExpired;
+    private boolean enabled;
+    private Long userId;
+    private String avatar;
+    private String email;
+    private String mobile;
+    private String sex;
+    private Long deptId;
+    private String deptName;
+    private String roleId;
+    private String roleName;
+    @JsonIgnore
+    private Date lastLoginTime;
+    private String description;
+    private String status;
+}

+ 73 - 2
febs-common/src/main/java/cc/mrbird/febs/common/utils/FebsUtil.java

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

+ 14 - 5
febs-server/febs-server-test/src/main/java/cc/mrbird/febs/server/test/controller/TestController.java

@@ -1,15 +1,21 @@
 package cc.mrbird.febs.server.test.controller;
 
+import cc.mrbird.febs.common.entity.CurrentUser;
 import cc.mrbird.febs.common.entity.FebsResponse;
 import cc.mrbird.febs.common.entity.QueryRequest;
 import cc.mrbird.febs.common.entity.system.SystemUser;
+import cc.mrbird.febs.common.utils.FebsUtil;
 import cc.mrbird.febs.server.test.service.IUserService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.GrantedAuthority;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 import java.security.Principal;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @author MrBird
@@ -31,12 +37,15 @@ public class TestController {
 
     /**
      * 获取当前用户信息
-     *
-     * @param principal principal
-     * @return Principal
      */
     @GetMapping("user")
-    public Principal currentUser(Principal principal) {
-        return principal;
+    public Map<String, Object> currentUser(Principal principal) {
+        Map<String, Object> map = new HashMap<>(5);
+        map.put("currentUser", FebsUtil.getCurrentUser());
+        map.put("currentUsername", FebsUtil.getCurrentUsername());
+        map.put("currentUserAuthority", FebsUtil.getCurrentUserAuthority());
+        map.put("currentTokenValue", FebsUtil.getCurrentTokenValue());
+        map.put("currentRequestIpAddress", FebsUtil.getCurrentRequestIpAddress());
+        return map;
     }
 }