我们在我们的 micronaut 应用程序中启用了 https。
控制器文件如下
@Controller("/api/v1/graphs/")
class MarketDataController(private val testUseCase: TestUseCase) {
private val log: Logger = LoggerFactory.getLogger(this::class.java)
@Get(value = "{resource}")
fun index(
resource: Resource,
request: HttpRequest<*>
): HttpResponse<MarketSnapshot> {
val url = request.uri
log.info("Request url is $url")
val eTag = url.hashCode().toString()
val requestETag = request.headers.get(HttpHeaders.IF_NONE_MATCH)
if (requestETag != null && requestETag == eTag) {
log.info("Request url is $url")
log.info("ETag matches. Returning 304 - Not modified")
return Publishers.just(HttpResponse.notModified().header(HttpHeaders.ETAG, eTag).header(
HttpHeaders.CACHE_CONTROL, "max-age=60"))
}
val logStartTime = System.currentTimeMillis()
val data = testUseCase.getData(resource)
val eTag = url.hashCode().toString()
return HttpResponse.ok(data).header(ETAG, eTag).header(CACHE_CONTROL, "max-age=60")
}
application.yml文件如下
micronaut:
server:
cors:
enabled: true
ssl:
enabled: true
buildSelfSigned: true
netty:
default:
allocator:
max-order: 3
第一次访问 API 端点时,根据控制器逻辑,API 响应将包含 Etag 和缓存控制:max-age:60 标头
预期: 第二次访问 API 端点时,API 请求应包含浏览器添加的“If-None-Match”标头,根据控制器逻辑,响应代码应为 304。
实际: 请求不包含“If-Non-Match”标头。
注意:如果我们从 application.yml 中删除 ssl 配置并使用 http 运行应用程序,标头将按预期工作。
我们需要为“HTTPS”添加任何明确的标头配置吗?