gpt4 book ai didi

Spring CookieLocaleResolver : set cookiePath

转载 作者:行者123 更新时间:2023-12-01 23:34:41 24 4
gpt4 key购买 nike

是否可以使用应用程序名称的值(自动)设置cookiePath?例如,我有一个 test.war,因此它将在 bla.com/test/上提供,所以我希望我的 cookie 路径为 /test/ 而不是默认的 /值(value)。谢谢

最佳答案

当您创建CookieLocaleResolver时,您可以设置路径,但它将被硬编码。

例如

<bean id="localeResolver" class="CookieLocaleResolver">
<property name="cookiePath" value="test" />
</bean>

另一种可能的解决方案是覆盖 LocaleResolver

public class MyCookieLocaleResolver extends CookieLocaleResolver {

@Override
public void setLocale(HttpServletRequest request,
HttpServletResponse response, Locale locale) {
if (locale != null) {
// Set request attribute and add cookie.
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME, locale);
addCookie(response, locale.toString());
} else {
// Set request attribute to fallback locale and remove cookie.
request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
determineDefaultLocale(request));
removeCookie(response);
}
}

public void addCookie(HttpServletRequest request,
HttpServletResponse response, String cookieValue) {
Cookie cookie = createCookie(request, cookieValue);
Integer maxAge = getCookieMaxAge();
if (maxAge != null) {
cookie.setMaxAge(maxAge);
}
if (isCookieSecure()) {
cookie.setSecure(true);
}
response.addCookie(cookie);
if (logger.isDebugEnabled()) {
logger.debug("Added cookie with name [" + getCookieName()
+ "] and value [" + cookieValue + "]");
}
}

protected Cookie createCookie(HttpServletRequest request, String cookieValue) {
Cookie cookie = new Cookie(getCookieName(), cookieValue);
if (getCookieDomain() != null) {
cookie.setDomain(getCookieDomain());
}
cookie.setPath(request.getContextPath());
return cookie;
}
}

关于Spring CookieLocaleResolver : set cookiePath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17165298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com