| | |
| | | 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; |
| | | |
| | | /** |
| | | * 免密登录服务 |
| | |
| | | @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; |
| | | } |