java - Spring RestTemplate 身份验证的性能问题
问题描述
我正在使用 Spring RestTemplate 从一个 restful 服务器中检索对象以及一个用于基本身份验证的拦截器。但是,我在本地主机和开发环境之间遇到了巨大的性能差异。
在本地运行客户端和 Restful 服务器时,RestTemplate getForObject 需要 3 秒。在 DEV 中本地和 restful 服务器上运行客户端时,需要 59 秒。我在拦截器中添加了一些日志记录——在 DEV 中,安静的服务器响应非常快(在 1 秒内)。导致延迟的原因是 getForObject 所以我猜在 DEV RestTemplate 中需要花费大量时间来将响应主体转换为对象。
我在 localhost 和 DEV 中进行了相同的调用,因此我希望响应主体的大小相同。有人知道如何提高 DEV 的性能吗?非常感谢!
拦截器:
public class BasicAuthInterceptor implements ClientHttpRequestInterceptor {
public static final Log log = LogFactory.getLog(BasicAuthInterceptor.class);
private String username;
private String password;
public BasicAuthInterceptor( String username, String password ) {
this.username = username;
this.password = password;
}
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
//Build the auth-header
final String auth = username + ":" + password;
final byte[] encodedAuth = Base64.encodeBase64( auth.getBytes( Charset.forName( "US-ASCII" ) ) );
final String authHeader = "Basic " + new String( encodedAuth );
//Add the auth-header
request.getHeaders().add( "Authorization", authHeader );
log.info("Sending request to the data server");
ClientHttpResponse response = execution.execute(request, body);
log.info("Got response from the data server. Status: " + response.getStatusCode() + " " + response.getStatusText());
log.info("Response header: " + response.getHeaders());
return response;
} }
RestTemplate 调用:
RestTemplate restTemplate = new RestTemplate();
final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add( new BasicAuthInterceptor( "user", "xxx" ) );
restTemplate.setInterceptors( interceptors );
AggregatedCptyExposure[] expoIq = restTemplate.getForObject(fullUrl, AggregatedCptyExposure[].class);
从日志中,我注意到 DEV 和本地的响应标头不同。
当地的:
INFO 09/12 13:41:00.160 {} [pool-6-thread-8] BasicAuthInterceptor - Response header: {Content-Type=[application/json;charset=UTF-8], Transfer-Encoding=[chunked], Date=[Wed, 12 Sep 2018 17:41:00 GMT]}
开发:
INFO 09/12 13:44:05.800 {} [pool-6-thread-13] BasicAuthInterceptor - Response header: {Date=[Wed, 12 Sep 2018 17:44:05 GMT], Set-Cookie=[X-Session-Token=mode&BASICAUTH&user&name&token&AAEAAAFlzuIHW2wljAAXQ5zrkYZFE75H7p9MfbH6REVWICAgICAgIAEnuj5LmEhn8vdL8atZ9j6DGGmXSimKUUSEc%2BpPv6uJ2A%3D%3D; path=/; HttpOnly, ROUTEID=.2; path=/, ROUTEID=.2; path=/, EncryptCookie=!MUIcv63qIjUiv0DaswGZ/7EuVxdhGlpB0wRK4L7mIcd8CeqPrqCaQXDpIomEbw5h3/vRHDvPYFxjQQ==; expires=Wed, 12-Sep-2018 21:44:05 GMT; path=/; Httponly; Secure], Strict-Transport-Security=[max-age=31536000], Content-Type=[application/json;charset=UTF-8], Vary=[Accept-Encoding], X-Frame-Options=[SAMEORIGIN], Content-Security-Policy-Report-Only=[default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' 'unsafe-eval'; report-uri ....], X-Content-Type-Options=[nosniff], Access-Control-Allow-Origin=[.....], Access-Control-Allow-Credentials=[true], X-XSS-Protection=[1; mode=block], X-Route-Ref=[Route:2;dcts01aa/1=dcts03;2=dcts01; P59357], Cache-Control=[no-cache, no-store, must-revalidate], Pragma=[no-cache], Expires=[0], Keep-Alive=[timeout=5, max=100], Connection=[Keep-Alive], Transfer-Encoding=[chunked]}
解决方案
推荐阅读
- powershell - 如何在没有安全提示的情况下从 url 列表中查询特定标头
- javascript - SQL 查询 Nodejs Express 中的 SQL 查询
- powershell - 使用 OAuth 1.0 生成签名时出现问题
- python - 将Detectron2实例分割转换为opencv Mat数组
- ios - SwiftUI:为什么 ScenePhase 在 .onChange() 中的行为不同?
- java - 如何使用 th:switch 和 th:each 处理 thymeleaf 中的两个列表
- javascript - 尝试使用spring boot将对象存储在服务器上的数组中
- openlayers - OpenLayers:图层和特征的混合排序
- json - 收到 Firebase 云消息传递实例后如何重建 ListViewBuilder?
- plugins - 1.16 以 player 或 @a 作为参数的 Bukkit 插件命令