Ver código fonte

跳转登陆修改:通过eitoken获取用户名

sl 1 mês atrás
pai
commit
14257c27f5

+ 60 - 8
jeecg-module-interlock/src/main/java/org/jeecg/modules/ssoClient/controller/SSOLoginLogoutController.java

@@ -371,16 +371,17 @@ public class SSOLoginLogoutController {
             }else {
 //                response.setHeader("Set-Cookie", String.format("EIToken=%s; Max-Age=3600; Path=/", eiToken));
 //                response.setHeader("Set-Cookie", String.format("WISEUser=%s; Max-Age=3600; Path=/", usernameSSO));
-                if(usernameSSO==null || "".equals(usernameSSO)){
-                    loginResult.setResult(result.error("当前没有获取到用户名,请重新登录"));
-                    loginResult.setUpdate(false);
-                    return loginResult;
+                if(usernameSSO==null || "".equals(usernameSSO) || isHasSSORole.equals("0")){
+                    log.info("没有获取到用户名,需要获取用户名");
+                    JSONObject ssoUserInfo = getSSOUserInfo(eiToken);
+                    ssoRole = (String) ssoUserInfo.get("ssoRole");
+                    usernameSSO = (String) ssoUserInfo.get("username");
                 }
 
-                if(isHasSSORole.equals("0")){//如果没有SSORole字段,查询一下sso角色
-                    //获取用户角色 是否是管理员
-                    ssoRole = getSSORole(eiToken);
-                }
+//                if(isHasSSORole.equals("0")){//如果没有SSORole字段,查询一下sso角色
+//                    //获取用户角色 是否是管理员
+//                    ssoRole = getSSORole(eiToken);
+//                }
                 Cookie cookie = new Cookie("EIToken", eiToken);
                 Cookie cookie1 = new Cookie("WISEUser", usernameSSO);
                 Cookie cookie2 = new Cookie("SSORole", ssoRole);
@@ -733,6 +734,57 @@ public class SSOLoginLogoutController {
     }
 
     /**
+     *  sso获取当前登陆的sso用户的信息
+     */
+    public JSONObject getSSOUserInfo(String eiToken){
+        //String url = "http://192.168.2.248:8188/v4.0/users/me";
+        JSONObject ssoUserInfo = new JSONObject();
+        ssoUserInfo.put("ssoRole", "");
+        ssoUserInfo.put("username", "");
+        String url = configService.getConfigValue(ConfigInfo.SSO_GETSSORLOE);
+        // 设置请求头部
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_JSON);
+        headers.add("Authorization", "Bearer " + eiToken);
+
+        // 创建RestTemplate实例
+        RestTemplate restTemplate = new RestTemplate();
+
+        // 创建HttpEntity封装请求体和头部信息
+        HttpEntity<String> entity = new HttpEntity<>(headers);
+
+        try {
+            // 发送Get请求并获取响应
+//            ResponseEntity<JSONObject> response = restTemplate.getForEntity(url, JSONObject.class);
+            ResponseEntity<JSONObject> response = restTemplate.exchange(
+                    url,
+                    HttpMethod.GET,
+                    entity,
+                    JSONObject.class);
+
+            // 处理响应
+            if (response.getStatusCode() == HttpStatus.OK) {
+                JSONObject responseBody = response.getBody();
+//                System.out.println(responseBody);
+                if (responseBody != null && !responseBody.equals("")) {
+                    ssoUserInfo.put("ssoRole", responseBody.get("ssoRole"));
+                    ssoUserInfo.put("username", responseBody.get("username"));
+                    return responseBody;
+//                    return (String) responseBody.get("ssoRole");
+                } else {
+                    System.out.println("未找到body信息");
+                }
+            } else {
+                System.out.println("登录失败,HTTP状态码:" + response.getStatusCode());
+            }
+        } catch (Exception e) {
+            System.err.println("登录请求失败:" + e.getMessage());
+            e.printStackTrace();
+        }
+        return ssoUserInfo;
+    }
+
+    /**
      *  sso登录,获取当前sso用户的角色,是否是系统管理员
      */
     public  String getSSORole(String eiToken){