Cookie常用属性 Session使用介绍 URL组成结构 HTTP地址介绍 获取本机地址
- 2016-11-06 19:55:00
- admin
- 原创 2926
一、Cookie常用属性
domain,所属域名;
path,所属路径;
expires,过时时间,GMT时间;
max-age,过期时间,建议使用;
Secure,使用安全的方式向服务器传输Cookie,只能在https连接中被浏览器传递到服务器进行会话;
HttpOnly,如果设置了HttpOnly属性,JS脚本将无法读取到Cookie信息,这样能有效得防止XSS攻击;
JSESSIONID的HttpOnly设置,默认开启:
1、useHttpOnly="true" on Context element in a web application;
2、useHttpOnly="true" in the global CATALINA_BASE/conf/context.xml;
二、Session使用介绍
1、如果会话不存在,request.getSession会创建会话,cookie有效期默认等于会话时间;
2、HttpSession使用JSESSIONID进行查找,实现类org.apache.catalina.session.StandardSession;
3、HttpSession使用ConcurrentHashMap存储数据,所以线程安全;
4、会话过期时间设置,使用会话一定要设置,最近访问时间加上过期时间后会话过期;
<!-- session时长以分钟为单位,cookie时长以秒为单位,只对自动生成的cookie有效 -->
<session-config>
<session-timeout>120</session-timeout>
<cookie-config>
<max-age>7200</max-age>
</cookie-config>
</session-config>
三、URL组成结构
URL组成结构:
URL url = new URL(URLDecoder.decode(urlstr, "UTF-8"));
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getUserInfo());
System.out.println(url.getQuery());
WEB框架涉及接口:
1、URL、URI与用户输入保持一致,除了客户端有处理井号,所以井号一般不会被传到后台;
2、两种Path与用户输入不完全一致,权限判断使用ServletPath,只有他经过完全标准化处理;
URL=http://host:port/myapp/business/queryInfo.do?name=feinen
httpRequest.getMethod(),GET
httpRequest.getRequestURL(),http://host:port/myapp/business/queryInfo.do
httpRequest.getRequestURI(),/myapp/business/queryInfo.do
httpRequest.getContextPath(),/myapp
httpRequest.getServletPath(),/business/queryInfo.do
httpRequest.getQueryString(),name=feinen
四、HTTP地址介绍
request.getRemoteHost(),客户端地址;
request.getRemoteAddr(),客户端地址;
request.getRemotePort(),客户端端口;
request.getLocalAddr(),本机服务地址;
request.getLocalPort(),本机服务端口;
request.getServerName(),http报文中获取的服务器地址;
request.getServerPort(),http报文中获取的服务器端口;
request.getScheme(),http或https;
使用nginx代理时,需要设置以下请求头,然后应用从请求头获取客户端地址:
1、proxy_set_header X-Real-IP $remote_addr; 设置客户端地址到请求头;
2、proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 增加客户端地址到请求头,最近添加的在最后面;
五、获取本机地址
代码下载:GetLocalIP.java