| | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.vci.ubcs.code.entity.TokenUserObject; |
| | | import com.vci.ubcs.code.service.IPasswordFreeLoginService; |
| | | import com.vci.ubcs.code.util.HttpUtils; |
| | | import com.vci.ubcs.starter.util.HttpUtils; |
| | | import io.jsonwebtoken.Claims; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.http.auth.AuthenticationException; |
| | | import org.springblade.core.jwt.JwtUtil; |
| | | import org.springblade.core.jwt.props.JwtProperties; |
| | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.logging.Logger; |
| | | |
| | | import static org.springblade.core.secure.utils.AuthUtil.parseJWT; |
| | | |
| | |
| | | * @date 2023/9/11 15:45 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class PasswordFreeLoginServiceImpl implements IPasswordFreeLoginService { |
| | | |
| | | // 通过服务注册中心获取网关的端口号 |
| | |
| | | private DiscoveryClient discoveryClient; |
| | | // 配置的免密登录的账号所属的租户id |
| | | @Value("${password-free.pwd-free-tenant-id}") |
| | | // 配置的token在redis中的生存时间 |
| | | private String pwdFreeTenantId; |
| | | // 配置的token在redis中的生存时间 |
| | | @Value("${password-free.token-redis-expire}") |
| | | private Long tokenRedisExpire; |
| | | @Value("${password-free.pwd-free-addr}") |
| | | private String pwdFreeAddr; |
| | | @Autowired |
| | | private BladeRedis bladeRedis; |
| | | |
| | |
| | | String ipAddr = request.getRemoteAddr(); |
| | | // 先尝试从缓存当中取,存在就直接从缓存中获取 |
| | | String authToken = bladeRedis.get(PWD_FREE_LOGIN_TOKEN+ipAddr+":"+userName); |
| | | BladeUser user = null; |
| | | // 解析token存放进attr中 |
| | | String token2 = JwtUtil.getToken(authToken); |
| | | BladeUser user = this.getUser(token2); |
| | | //不存在就请求 |
| | | if(Func.isEmpty(authToken)){ |
| | | if(Func.isEmpty(authToken) || Func.isEmpty(user)){ |
| | | // 免密登录接口地址 |
| | | String loginUrl = "http://localhost:"+this.getGatewayPort("ubcs-gateway")+"/ubcs-auth/oauth/token"; |
| | | String loginUrl = "http://"+pwdFreeAddr+":"+this.getGatewayPort("ubcs-gateway")+"/ubcs-auth/oauth/token"; |
| | | log.debug("当前免密登录调用地址:"+loginUrl); |
| | | // 请求ubcs-auth服务获取token,先设置请求头 |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | |
| | | authToken = "bearer " + tokenUserObject.getAccess_token(); |
| | | // 将token存入缓存当中,过期时间为24小时 |
| | | bladeRedis.setEx(PWD_FREE_LOGIN_TOKEN+ipAddr+":"+userName,"bearer "+tokenUserObject.getAccess_token(),tokenRedisExpire); |
| | | token2 = JwtUtil.getToken(authToken); |
| | | user = this.getUser(token2); |
| | | } |
| | | // 解析token存放进attr中 |
| | | String token2 = JwtUtil.getToken(authToken); |
| | | user = this.getUser(token2); |
| | | //request.setAttribute("Blade-Auth",token); |
| | | request.setAttribute("_BLADE_USER_REQUEST_ATTR_",user); |
| | | return true; |
| | |
| | | if (StringUtil.isNotBlank(authToken)) { |
| | | token = JwtUtil.getToken(authToken); |
| | | } |
| | | |
| | | if (StringUtil.isNotBlank(token)) { |
| | | claims = parseJWT(token); |
| | | } |
| | | |
| | | if (ObjectUtil.isNotEmpty(claims) && getJwtProperties().getState()) { |
| | | tenantId = Func.toStr(claims.get("tenant_id")); |
| | | String userId = Func.toStr(claims.get("user_id")); |