From 3296d920915860e934e5620488e80e536e168a40 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Fri, 12 Jun 2020 17:12:04 +0800
Subject: [PATCH] Merge branch 'master' of https://gitee.com/chonggaoxiao/new_excoin

---
 src/main/java/com/xcong/excoin/common/LoginUserUtils.java |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/common/LoginUserUtils.java b/src/main/java/com/xcong/excoin/common/LoginUserUtils.java
index dbdd9d2..8f2400a 100644
--- a/src/main/java/com/xcong/excoin/common/LoginUserUtils.java
+++ b/src/main/java/com/xcong/excoin/common/LoginUserUtils.java
@@ -1,8 +1,15 @@
 package com.xcong.excoin.common;
 
+import com.xcong.excoin.common.exception.GlobalException;
 import com.xcong.excoin.modules.member.entity.MemberEntity;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.http.HttpRequest;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.ArrayList;
 
 /**
  * 登陆用户工具类
@@ -17,9 +24,43 @@
 
     public static MemberEntity getAppLoginUser() {
         if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(ANON)) {
+            throw new GlobalException("无法获取登陆信息");
+        } else {
+            return (MemberEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
+        }
+    }
+
+    public static String getAppLoginUserToken() {
+        return (String) SecurityContextHolder.getContext().getAuthentication().getCredentials();
+    }
+
+    public static void resetAppLoginUser(MemberEntity memberEntity) {
+        Authentication authentication = new UsernamePasswordAuthenticationToken(memberEntity, getAppLoginUserToken(), new ArrayList<>());
+        SecurityContextHolder.getContext().setAuthentication(authentication);
+    }
+
+    /**
+     * mybatis 拦截器专用
+     *
+     * @return MemberEntity
+     */
+    public static MemberEntity getUser() {
+        if (SecurityContextHolder.getContext().getAuthentication() == null) {
+            return null;
+        }
+
+        if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals(ANON)) {
             return null;
         } else {
             return (MemberEntity) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
         }
     }
+
+    public static boolean isBrowser(HttpServletRequest request) {
+        String userAgent = request.getHeader("user-agent");
+        if (userAgent.toLowerCase().contains("mobile") || userAgent.toLowerCase().contains("okhttp")) {
+            return false;
+        }
+        return true;
+    }
 }

--
Gitblit v1.9.1