From 7f9d6840cc01c25ac9816df60dbac3d040594fa3 Mon Sep 17 00:00:00 2001 From: ludc Date: 星期二, 12 九月 2023 19:12:55 +0800 Subject: [PATCH] 代码提交 --- Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java | 33 +++++++++++++++++++++++++-------- 1 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java index 692c097..5134140 100644 --- a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java +++ b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/PasswordFreeLoginServiceImpl.java @@ -1,8 +1,12 @@ package com.vci.ubcs.code.service.impl; import com.vci.ubcs.code.service.IPasswordFreeLoginService; +import com.vci.ubcs.code.util.HttpUtils; import org.apache.commons.collections4.MultiValuedMap; import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -13,6 +17,9 @@ import org.springframework.http.HttpMethod; import org.springframework.web.client.RestTemplate; +import javax.annotation.Resource; +import java.util.List; + /** * 鍏嶅瘑鐧诲綍鏈嶅姟 * @author ludc @@ -21,8 +28,22 @@ @Service public class PasswordFreeLoginServiceImpl implements IPasswordFreeLoginService { - private RestTemplate restTemplate; + // 閫氳繃鏈嶅姟娉ㄥ唽涓績鑾峰彇缃戝叧鐨勭鍙e彿 + @Autowired + private DiscoveryClient discoveryClient; + /** + * 鑾峰彇缃戝叧绔彛 + * @return + */ + public String getGatewayPort() { + List<ServiceInstance> instances = discoveryClient.getInstances("ubcs-gateway"); + if (!instances.isEmpty()) { + ServiceInstance gatewayInstance = instances.get(0); + return String.valueOf(gatewayInstance.getPort()); + } + return "80"; + } /** * 鍏嶅瘑鐧诲綍 @@ -32,7 +53,7 @@ @Override public boolean passwordFreeLogin(String account) { // 鍏嶅瘑鐧诲綍鎺ュ彛鍦板潃 - String loginUrl = "http://ubcs-auth/auth/login"; + String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/passwordFreeLogin?username=admin&grant_type=captcha&scope=all&type=account"; // 璁剧疆璇锋眰澶� HttpHeaders headers = new HttpHeaders(); @@ -42,13 +63,9 @@ MultiValueMap<String,String> bodyParams = new LinkedMultiValueMap<String,String>(); bodyParams.add("account",account); - // 鍒涘缓璇锋眰瀹炰綋 - HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(bodyParams, headers); - // 鍙戦�丳OST璇锋眰 - ResponseEntity<String> responseEntity = restTemplate.exchange(loginUrl, HttpMethod.POST, requestEntity, String.class); - String responseBody = responseEntity.getBody(); - + String responseBody = HttpUtils.post(loginUrl, bodyParams); + System.out.println(responseBody); //鎷垮埌鍝嶅簲浣撳皢token瀛樺叆鍒皉edis涓紝浠ccount浣滀负瀛樺偍鐨刱ey // 瑙f瀽鍝嶅簲浣撹幏鍙栦护鐗� -- Gitblit v1.9.3