¶Ô±ÈÐÂÎļþ |
| | |
| | | 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; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.LinkedMultiValueMap; |
| | | import org.springframework.util.MultiValueMap; |
| | | import org.springframework.http.HttpMethod; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * å
å¯ç»å½æå¡ |
| | | * @author ludc |
| | | * @date 2023/9/11 15:45 |
| | | */ |
| | | @Service |
| | | public class PasswordFreeLoginServiceImpl implements IPasswordFreeLoginService { |
| | | |
| | | // éè¿æå¡æ³¨åä¸å¿è·åç½å
³ç端å£å· |
| | | @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"; |
| | | } |
| | | |
| | | /** |
| | | * å
å¯ç»å½ |
| | | * @param account è´¦å· |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean passwordFreeLogin(String account) { |
| | | // å
å¯ç»å½æ¥å£å°å |
| | | String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/passwordFreeLogin?username=admin&grant_type=captcha&scope=all&type=account"; |
| | | |
| | | // 设置请æ±å¤´ |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | |
| | | //设置请æ±ä½åæ° |
| | | MultiValueMap<String,String> bodyParams = new LinkedMultiValueMap<String,String>(); |
| | | bodyParams.add("account",account); |
| | | |
| | | // åéPOSTè¯·æ± |
| | | String responseBody = HttpUtils.post(loginUrl, bodyParams); |
| | | System.out.println(responseBody); |
| | | //æ¿å°ååºä½å°tokenåå
¥å°redisä¸ï¼ä»¥accountä½ä¸ºåå¨çkey |
| | | |
| | | // è§£æååºä½è·å令ç |
| | | // è¿éå设ååºä½æ¯JSONæ ¼å¼ï¼å
å«ä¸ä¸ªå为"token"çåæ®µ |
| | | // æ ¹æ®å®é
æ
åµè¿è¡è§£æ |
| | | // JSONObject json = new JSONObject(responseBody); |
| | | // String token = json.getString("token"); |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | } |