| | |
| | | package com.vci.ubcs.code.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.vci.ubcs.code.config.HeaderMapRequestWrapper; |
| | | import com.vci.ubcs.code.entity.TokenUserObject; |
| | | 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.springblade.core.cache.utils.CacheUtil; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.redis.cache.BladeRedis; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | 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 javax.servlet.ServletRequest; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | // 通过服务注册中心获取网关的端口号 |
| | | @Autowired |
| | | private DiscoveryClient discoveryClient; |
| | | @Value("${user-info.pwd-free-tenant-id}") |
| | | private String pwdFreeTenantId; |
| | | @Autowired |
| | | private BladeRedis bladeRedis; |
| | | // 缓存名 |
| | | public static final String PWD_FREE_LOGIN_TOKEN = "pwdFreeLogin:Token:"; |
| | | |
| | | /** |
| | | * 获取网关端口 |
| | | * @return |
| | | */ |
| | | public String getGatewayPort() { |
| | | private String getGatewayPort() { |
| | | List<ServiceInstance> instances = discoveryClient.getInstances("ubcs-gateway"); |
| | | if (!instances.isEmpty()) { |
| | | ServiceInstance gatewayInstance = instances.get(0); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 免密登录 |
| | | * 免密登录并存入缓存,配合过滤器写法 |
| | | * @param username 账号 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean passwordFreeLogin(String username) { |
| | | public String passwordFreeLogin(String username, ServletRequest servletRequest) { |
| | | // 免密登录接口地址 |
| | | String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/password-free-login"; |
| | | |
| | | String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/token"; |
| | | // 请求来自己哪个ip地址 |
| | | HttpServletRequest request = (HeaderMapRequestWrapper) servletRequest; |
| | | String ipAddr = request.getRemoteAddr(); |
| | | // 先尝试从缓存当中取,如果不存在就登录 |
| | | String redisToken = (String)bladeRedis.get(PWD_FREE_LOGIN_TOKEN+ipAddr); |
| | | if(Func.isNotBlank(redisToken)){ |
| | | // 缓存中已经存在就直接删除该缓存,主要为了避免统一ip下存在多个token的情况 |
| | | bladeRedis.del(PWD_FREE_LOGIN_TOKEN+ipAddr); |
| | | } |
| | | // 不存在就重新获取token |
| | | // 设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | headers.set("Authorization", "Basic c3dvcmQ6c3dvcmRfc2VjcmV0"); |
| | | headers.set("Tenant-Id", "000000"); |
| | | headers.set("Tenant-Id", pwdFreeTenantId); |
| | | |
| | | //设置请求体参数 |
| | | MultiValueMap<String,String> parameters = new LinkedMultiValueMap<String,String>(); |
| | | parameters.add("username",username); |
| | | parameters.add("grant_type", "captcha"); |
| | | parameters.add("grant_type", "passwordfree"); |
| | | parameters.add("scope", "all"); |
| | | parameters.add("type", "account"); |
| | | |
| | | // 发送POST请求 |
| | | String responseBody = HttpUtils.post(loginUrl, parameters,headers); |
| | | System.out.println(responseBody); |
| | | //拿到响应体将token存入到redis中,以account作为存储的key |
| | | |
| | | // 解析响应体获取令牌 |
| | | // 这里假设响应体是JSON格式,包含一个名为"token"的字段 |
| | | // 根据实际情况进行解析 |
| | | // JSONObject json = new JSONObject(responseBody); |
| | | // String token = json.getString("token"); |
| | | |
| | | return false; |
| | | //拿到响应体其中包含token,用request中的ip地址作为键值,将token存入缓存 |
| | | TokenUserObject tokenUserObject = null; |
| | | try { |
| | | tokenUserObject = JSON.parseObject(responseBody, TokenUserObject.class); |
| | | }catch (Exception e){ |
| | | throw new ServiceException("responseBody转换TokenUserObject失败:"+e.getMessage()); |
| | | } |
| | | // 将token存入缓存当中,过期时间为24小时 |
| | | bladeRedis.setEx(PWD_FREE_LOGIN_TOKEN+ipAddr,"bearer "+tokenUserObject.getAccess_token(),60*60*60*24L); |
| | | return responseBody; |
| | | } |
| | | |
| | | /** |
| | | * 免密登录,改变当前webservice请求的header |
| | | * @param username 账号 |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean passwordFreeLogin2(String username, HttpServletRequest servletRequest) { |
| | | // 免密登录接口地址 |
| | | String loginUrl = "http://localhost:"+this.getGatewayPort()+"/ubcs-auth/oauth/token"; |
| | | |
| | | // 获取token,先设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | headers.set("Authorization", "Basic c3dvcmQ6c3dvcmRfc2VjcmV0"); |
| | | headers.set("Tenant-Id", pwdFreeTenantId); |
| | | //设置请求体参数 |
| | | MultiValueMap<String,String> parameters = new LinkedMultiValueMap<String,String>(); |
| | | parameters.add("username",username); |
| | | parameters.add("grant_type", "passwordfree"); |
| | | parameters.add("scope", "all"); |
| | | parameters.add("type", "account"); |
| | | // 发送POST请求 |
| | | String responseBody = HttpUtils.post(loginUrl, parameters,headers); |
| | | //拿到响应体其中包含token,用request中的ip地址作为键值,将token存入缓存 |
| | | TokenUserObject tokenUserObject = null; |
| | | try { |
| | | tokenUserObject = JSON.parseObject(responseBody, TokenUserObject.class); |
| | | }catch (Exception e){ |
| | | throw new ServiceException("responseBody转换TokenUserObject失败:"+e.getMessage()); |
| | | } |
| | | HttpServletRequest request = (HeaderMapRequestWrapper) servletRequest; |
| | | HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(request); |
| | | |
| | | //设置当前web接口的请求头 |
| | | String token = "bearer " + tokenUserObject.getAccess_token(); |
| | | // 将token设置到header中 |
| | | requestWrapper.setHeader("Blade-Auth", token); |
| | | requestWrapper.setHeader("Authorization","Basic c3dvcmQ6c3dvcmRfc2VjcmV0"); |
| | | |
| | | // 将token存入缓存当中,过期时间为24小时 |
| | | return true; |
| | | } |
| | | |
| | | } |