feat: 修改类名称 + 优化获取Context

This commit is contained in:
zengxiaobo 2024-04-28 16:45:47 +08:00
parent f79a63d09c
commit ad39b3458b
6 changed files with 29 additions and 16 deletions

View File

@ -11,9 +11,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class ApiResult<T> {
private static final Integer SUCCESS_HTTP_CODE = 200;
public static final Integer SUCCESS_HTTP_CODE = 200;
private static final String SUCCESS_CODE = "200";
private static final Integer FAILED_HTTP_CODE = 400;
public static final Integer FAILED_HTTP_CODE = 400;
private static final String FAILED_CODE = "400";
protected Integer httpCode;

1
install_all.sh Executable file
View File

@ -0,0 +1 @@
git pull && mvn clean install -am -U -DskipTests

View File

@ -1,6 +1,7 @@
package cn.axzo.foundation.web.support.context;
import cn.axzo.foundation.enums.AppEnvEnum;
import cn.axzo.foundation.result.ResultCode;
import cn.axzo.foundation.web.support.AppRuntime;
import cn.axzo.foundation.web.support.rpc.HttpClient;
import cn.axzo.foundation.web.support.rpc.OkHttpClientImpl;
@ -10,6 +11,7 @@ import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
@ -68,10 +70,20 @@ public class AxContextInterceptor implements HandlerInterceptor {
context = JSONObject.parseObject(StringUtils.removeStart(authorization, "Raw "), AxContext.class);
}
if (authorization.startsWith("Bearer")) {
String result = HTTP_CLIENT.get(ENV_HOSTS.get(appRuntime.getEnv()), RequestParams.FormParams.builder()
.headers(ImmutableMap.of("Authorization", authorization))
.build());
JSONObject userinfo = JSONObject.parseObject(result).getJSONObject("userinfo");
String result;
try {
result = HTTP_CLIENT.get(ENV_HOSTS.get(appRuntime.getEnv()), RequestParams.FormParams.builder()
.headers(ImmutableMap.of("Authorization", authorization))
.build());
} catch (Exception ex) {
throw ResultCode.RUNTIME_EXCEPTION.toException("获取登陆信息错误" + ex.getMessage());
}
//这里是一个非标准返回
JSONObject resultJSON = JSONObject.parseObject(result);
if (BooleanUtils.isNotTrue(resultJSON.getBoolean("approve"))) {
throw ResultCode.RUNTIME_EXCEPTION.toException(resultJSON.getString("msg"));
}
JSONObject userinfo = resultJSON.getJSONObject("userinfo");
context = AxContext.builder()
.ouId(userinfo.getLong("ouId"))
.axUser(userinfo.toJavaObject(AxUser.class))

View File

@ -8,7 +8,7 @@ import java.util.function.Function;
/**
* 代理rpc请求, 并处理授权的token信息.
*/
public interface AuthRequestProxy {
public interface RequestProxy {
/**
* 代理runner发起request请求. 在请求中添加授权的token
@ -20,7 +20,7 @@ public interface AuthRequestProxy {
*/
<T> Optional<T> request(Function<String, T> runner);
AuthRequestProxy SIMPLE_PROXY = new AuthRequestProxy() {
RequestProxy SIMPLE_PROXY = new RequestProxy() {
@Override
public <T> Optional<T> request(Function<String, T> runner) {
return Optional.ofNullable(runner.apply(StringUtils.EMPTY));

View File

@ -28,7 +28,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
@Slf4j
public class AuthRequestProxyImpl implements AuthRequestProxy {
public class RequestProxyImpl implements RequestProxy {
private static final int MAX_RETRY_COUNT = 3;
private RpcClient rpcClient;
@ -38,11 +38,11 @@ public class AuthRequestProxyImpl implements AuthRequestProxy {
private JSONObject globalTokenReqExt;
@Builder
public AuthRequestProxyImpl(RpcClient rpcClient, TokenReq tokenReq, ApplicationContext applicationContext, AppRuntime appRuntime) {
public RequestProxyImpl(RpcClient rpcClient, TokenReq tokenReq, ApplicationContext applicationContext, AppRuntime appRuntime) {
Objects.requireNonNull(tokenReq);
this.rpcClient = Optional.ofNullable(rpcClient)
.orElse(RpcAuthClientImpl.builder().authRequestProxy(SIMPLE_PROXY).build());
.orElse(RpcClientImpl.builder().requestProxy(SIMPLE_PROXY).build());
this.tokenReq = tokenReq;
}

View File

@ -13,15 +13,15 @@ import java.util.function.Function;
import java.util.function.Supplier;
@Slf4j
public class RpcAuthClientImpl implements RpcClient {
public class RpcClientImpl implements RpcClient {
protected Supplier<Map<String, String>> customHeaderSupplier;
@Getter
protected HttpClient httpClient;
protected AuthRequestProxy authRequestProxy;
protected RequestProxy requestProxy;
@Builder
public RpcAuthClientImpl(AuthRequestProxy authRequestProxy, HttpClient.Config config, Supplier<Map<String, String>> requestHeaderSupplier) {
this.authRequestProxy = Optional.ofNullable(authRequestProxy).orElse(AuthRequestProxy.SIMPLE_PROXY);
public RpcClientImpl(RequestProxy requestProxy, HttpClient.Config config, Supplier<Map<String, String>> requestHeaderSupplier) {
this.requestProxy = Optional.ofNullable(requestProxy).orElse(RequestProxy.SIMPLE_PROXY);
this.httpClient = OkHttpClientImpl.builder()
.config(config)
.build();
@ -65,7 +65,7 @@ public class RpcAuthClientImpl implements RpcClient {
});
}
return authRequestProxy.request(token -> {
return requestProxy.request(token -> {
///XXX 注意此处有一个副作用
requestParams.addAuthorization(token);
return supplier.get();