Cookie中设置了 HttpOnly,Secure 属性,有效的防止XSS攻击,X-Frame-Options 响应头避免点击劫持

属性介绍:

1) secure属性
当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输(ssl),即

只能在 HTTPS 连接中被浏览器传递到服务器端进行会话验证,

如果是 HTTP 连接则不会传递该信息,所以不会被窃取到Cookie 的具体内容。


2 )HttpOnly属性
如果在Cookie中设置了"HttpOnly"属性,那么通过程序(JS脚本、Applet等)将无法读取到Cookie信息,这样能有效的防止XSS攻击。

3)X-Frame-Options 响应头 

X-Frame-Options HTTP 响应头,表示 是否允许一个页面在 <frame>, </iframe> 或者 <object> 中展现的标记。

通过设置 X-Frame-Options 阻止站点内的页面被其他页面嵌入从而防止点击劫持。X

X-Frame-Options 有三个值:

DENY :该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。 
SAMEORIGIN :该页面可在相同域名页面的 frame 中展示。 
ALLOW-FROM uri该页面可在指定来源的 frame 中展示。 

2.HttpOnly,Secure的设置样例:

1)java代码处理方式:

//设置cookie
response.setHeader("Set-Cookie", "JSESSIONID=" + sessionid + ";Secure;HttpOnly")//设置Secure;HttpOnly
response.setHeader("x-frame-options", "SAMEORIGIN");//设置x-frame-options

2)tomcat处理方式

对 tomcat/conf/context.xml 修改

<Context useHttpOnly="true">

对 tomcat/conf/server.xml 修改

(java代码和配置,二选一即可,目前,我只有java代码方式生效)

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" secure="true"/>

————————————————————————————————————————

附1:

HttpOnly之Apache官网描述
 refer :http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

useHttpOnly 

Should the HttpOnly flag be set on session cookies to prevent client side script from accessing the session ID? Defaults to false.

原文地址:https://www.cnblogs.com/xiaoliu66007/p/10118074.html