Skip to content

Commit

Permalink
fix: language preference is not remembered under non-HTTPS connections (
Browse files Browse the repository at this point in the history
#6891)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
修复非 HTTPS 连接下无法记住用户语言偏好的问题

#### Which issue(s) this PR fixes:
Fixes #6888

#### Does this PR introduce a user-facing change?
```release-note
修复非 HTTPS 连接下无法记住用户语言偏好的问题
```
  • Loading branch information
guqing authored Oct 18, 2024
1 parent 3570353 commit 13644d2
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
.getFirst(LANGUAGE_PARAMETER_NAME);
if (StringUtils.hasText(language)) {
var locale = Locale.forLanguageTag(language);
exchange.getResponse()
.addCookie(ResponseCookie.from(LANGUAGE_COOKIE_NAME, locale.toLanguageTag())
.path("/")
.secure(true)
.build()
);
setLanguageCookie(exchange, locale);
}
})
.then(Mono.defer(() -> chain.filter(exchange)));
}

void setLanguageCookie(ServerWebExchange exchange, Locale locale) {
var cookie = ResponseCookie.from(LANGUAGE_COOKIE_NAME, locale.toLanguageTag())
.path("/")
.httpOnly(true)
.secure("https".equalsIgnoreCase(exchange.getRequest().getURI().getScheme()))
.sameSite("Lax")
.build();
exchange.getResponse().getCookies().set(LANGUAGE_COOKIE_NAME, cookie);
}
}

0 comments on commit 13644d2

Please sign in to comment.