如何在httpwebresponse中获取httponly cookie?习惯上我使用CookieContainer来获取httpwebresponse中的cookie,但它不适用于httponly cookie.
有没有其他方式来抓住他们?
我在 Sof链接上找到了这个解决方案, 但我不得不从两个原因改变它:
我的代码是这样的:
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.addHeader("Set-Cookie", "; HTTPOnly");
Run Code Online (Sandbox Code Playgroud)
原始代码
FacesContext facesContext = FacesContext.getCurrentInstance().getFacesContext();
HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
response.addHeader("Set-Cookie", "yourcookiename=yourcookievalue; HTTPOnly");
Run Code Online (Sandbox Code Playgroud)
如果我错了,请纠正我。先感谢您
我找到了这个链接
但在最下方则说此信息可能不再是最新的。 所以我的问题是,将使用AJAX发送http_only cookie吗?可以通过AJAX响应设置http_only cookie吗?
编辑1:假设有一个用户登录到系统。他的会话开始,并且设置了http_only cookie。他尝试通过AJAX获取朋友列表并发送JSON。发出AJAX请求时,我是否需要特别说明要发送或不发送Cookie?默认情况下,每个请求都会将cookie发送到服务器吗?响应会发回Cookie吗(假设我正在更新用户的上次活动时间)?
我在通过弹出脚本中的 chrome 扩展程序处理 cookie 时遇到了一些麻烦。
popup.js 内容:
document.addEventListener('DOMContentLoaded', () => {
function cookieinfo() {
chrome.cookies.getAll({url: 'http://localhost:8080'}, function(cookie) {
console.log('Found cookie: ', cookie)
if (cookie == null)
return;
fetch('http://localhost:8080', {credentials: 'include'}).then((response) => {
// do some stuff
return response;
});
});
}
window.onload=cookieinfo;
}, false);
Run Code Online (Sandbox Code Playgroud)
我执行的步骤:
也许有人知道我做错了什么?
编辑:
看来原因是我的cookie有参数HttpOnly=true
和SameSite=Lax
(相关链接)。我可以在服务器日志中看到另一个 cookie。但是由于这个线程,如果credentials
参数设置为include
,则所有 cookie 都将被发送,即使是 httpOnly cookie。由于这个答案,我也尝试将它发送到 …
javascript cookies httponly google-chrome-extension samesite
设置http-only我在web.xml中使用了它
<session-config>
<cookie-config>
<http-only>true</http-only>
</cookie-config>
</session-config>
Run Code Online (Sandbox Code Playgroud)
但它没有设置http-only.可以任何人建议,可能是什么问题.以及如何设置它.
谢谢.
嗨我到目前为止尝试使用httponly cookie这是我编写的代码
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
Cookie cookie = new Cookie("mycookie", "hi");
resp.addCookie(cookie);
cookie.setHttpOnly(true);
boolean bol = cookie.isHttpOnly();
out.println("<br>Cookie is Marked as HttpOnly = " + bol);
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
out.println("<br/> From Cookies Array Name is: "
+ cookies[i].getName());
out.println("<br/> From Cookies Array Value is: "
+ cookies[i].getValue());
out.println("<br/> From Cookies Array isHttpOnly: …
Run Code Online (Sandbox Code Playgroud) 我有一个Angular Js应用程序,使用https协议与服务器通信.我使用cookie来存储登录用户的名字和jwt令牌.下面是我用来设置cookie的代码.
$cookies.putObject('token', token, {
expires: exp,
secure: true
});
Run Code Online (Sandbox Code Playgroud)
为了从安全团队获得我的应用程序许可,我需要为cookie设置HttpOnly标志.
当我打开我的网站的cookie时,我看到两个项目
我搜索并做了必要的更改,在这些cookie上设置HttpOnly标志.它工作,我可以看到两个cookie都有HttpOnly标志检查.但在此更改后,我的身份验证停止工作(即我无法访问cookie中的jwt集).换句话说,我与后端的沟通被打破了.我使用下面的语句来读取cookie.
$cookies.getObject('token')
Run Code Online (Sandbox Code Playgroud)
我不得不恢复一些改变以恢复到以前的状态.现在,connect.sid cookie设置为HttpOnly标志而令牌cookie不设置.我想知道这里一定是什么问题.我是Angular Js的新手,之前从未参与过Cookie管理.任何帮助将不胜感激.
我想通过JWT身份验证来保护SPA专用路由。为了使所有内容尽可能地安全,我想使用httpOnly
cookie将我的access_token
内容存储在客户端。
使用httpOnly
cookie可以保护我免受XSS攻击,但是不幸的是,这种方法不允许我检查cookie是否确实存在于浏览器中。
在这种情况下,如何实现一些逻辑以防止未登录的用户访问SPA的私有安全路由?
我是否被迫使用非httpOnly
Cookie localStorage
?