java+设置全局响应头,在Java Servlet应用程序中为HTTP 201响应设置Location标头的正确方法是什么...

决定采用Julian Reschke的建议并违反规范!至少我添加了以下评论:

/* Note: strictly speaking (per section 14.30 of RFC 2616), the Location header

* requires an *absolute URI*. However, in practice, many web

* applications send an *absolute path* instead. This is interoperable,

* that is, works in popular web browsers (according to

* http://en.wikipedia.org/wiki/HTTP_location).

*

* As the information required to set the Location header to an absolute URI

* is not generally available to a Servlet, we go with the flow and send

* an absolute path instead (in violation of RFC 2616).

*

* There is an issue filed with Hypertext Transfer Protocol Bis (httpbis)

* working group to resolve this problem here:

* http://trac.tools.ietf.org/wg/httpbis/trac/ticket/185

*/

response.setHeader("Location", url);

我不想自己发送绝对URI的原因是因为我在负载均衡器和其他 生产环境 基础设施背后遇到了问题 . 虽然在开发模式下“http:// localhost:8080 / foo”趋于正常工作:))

现在接受朱利安的答案......

在Apache Shiro中,您可以使用`org.apache.shiro.web.servlet.SimpleCookie`类来创建和设置Cookie对象,并将SameSite属性设置为STRICT。以下是一个示例代码: ```java import org.apache.shiro.web.servlet.SimpleCookie; // 创建一个SimpleCookie对象 SimpleCookie cookie1 = new SimpleCookie("cookie1"); cookie1.setValue("value1"); cookie1.setSameSite("Strict"); // 设置SameSite属性为STRICT SimpleCookie cookie2 = new SimpleCookie("cookie2"); cookie2.setValue("value2"); cookie2.setSameSite("Strict"); // 设置SameSite属性为STRICT // 将Cookie转换为字符串,添加到响应中 String cookie1String = cookie1.toServletString(); String cookie2String = cookie2.toServletString(); // 将Cookie添加到响应中 response.addHeader("Set-Cookie", cookie1String); response.addHeader("Set-Cookie", cookie2String); ``` 在这个示例中,我们使用`SimpleCookie`类创建了两个Cookie对象,并将其SameSite属性设置为STRICT。然后,使用`toServletString()`方法将每个Cookie对象转换为字符串表示形式,并将其添加到响应中的Set-Cookie标头中。 请注意,上述示例中的`response`对象是一个`javax.servlet.http.HttpServletResponse`类型的对象,您需要根据您的实际情况进行调整。同样,您需要根据您的代码逻辑和框架来获取`response`对象。 这种方法适用于使用Apache Shiro框架的Java应用程序,以设置响应标头中的Set-Cookie值,并将SameSite属性设置为STRICT。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值