cookie工具类

package cn.ffcs.cookie.common;
 
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
 
 
import com.ffcs.auth.platform.spring.util.AESEncoder;
import com.ffcs.auth.platform.spring.util.Base64;
import com.ffcs.auth.platform.spring.util.RSAEncoder;
 
 
/**
 * Cookie 辅助类
 * 
 */
public class CookieUtils {
 
 
/**
* 获得cookie
* 
* @param request
*            HttpServletRequest
* @param name
*            cookie name
* @return if exist return cookie, else return null.
*/
public static Cookie getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if(cookies == null)return null;
for (Cookie c : cookies) {
if (c.getName().equalsIgnoreCase(name)) {
return c;
}
}
return null;
}
 
 
/**
* 获取COOKIE
* 
* @param name
*/
public static String getCookieValue(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if(cookies == null)return null;
for (Cookie ck : cookies) {
if (StringUtils.equalsIgnoreCase(name,ck.getName())) 
return ck.getValue();
}
return null;
}
 
/**
* 设置COOKIE
* 
* @param name
* @param value
* @param maxAge
*/
public static void addCookie(HttpServletRequest request, HttpServletResponse response, String name,String value, int maxAge) {
addCookie(request,response,name,value,maxAge,true);
}
 
/**
* 根据部署路径,将cookie保存在根目录。
* 
* @param request
* @param response
* @param name
* @param value
* @param all_sub_domain
* @return
*/
public static Cookie addCookie(HttpServletRequest request,HttpServletResponse response, String name, String value,int maxAge, boolean all_sub_domain) {
Cookie cookie = new Cookie(name, value);
cookie.setMaxAge(maxAge);
if(all_sub_domain){
String serverName = request.getServerName();
if(isIPAddr(serverName)){
cookie.setDomain(serverName);
}else{
String domain = getDomainOfServerName(serverName);
if(domain!=null){
if(domain.indexOf('.')!=-1){
cookie.setDomain('.' + domain);
}
}
}
}
cookie.setPath("/");
response.addCookie(cookie);
return cookie;
 
 
}
 
 
/**
* 取消cookie
* 
* @param response
* @param name
* @param domain
*/
public static void cancleCookie(HttpServletResponse response, String name,String domain) {
Cookie cookie = new Cookie(name, null);
cookie.setMaxAge(0);
cookie.setPath("/");
if (!StringUtils.isBlank(domain)) {
cookie.setDomain(domain);
}
response.addCookie(cookie);
}
 
/**
* 判断字符串是否是一个IP地址
* @param addr
* @return
*/
public static boolean isIPAddr(String addr){
if(StringUtils.isEmpty(addr))
return false;
String[] ips = StringUtils.split(addr, '.');
if(ips.length != 4)
return false;
try{
int ipa = Integer.parseInt(ips[0]);
int ipb = Integer.parseInt(ips[1]);
int ipc = Integer.parseInt(ips[2]);
int ipd = Integer.parseInt(ips[3]);
return ipa >= 0 && ipa <= 255 && ipb >= 0 && ipb <= 255 && ipc >= 0
&& ipc <= 255 && ipd >= 0 && ipd <= 255;
}catch(Exception e){}
return false;
}
 
/**
* 获取用户访问URL中的根域名,用于cookie;
* 默认情况下,IE和Chrome内核的浏览器会认为http://localhost为无效的域名,所以不会保存它的cookie,使用127.0.0.1访问就可以解决。
* 例如: china.herostart.com -> herostart.com
* @param req
* @return
*/
public static String getDomainOfServerName(String serverName){
if(isIPAddr(serverName)) return null;
String[] names = StringUtils.split(serverName, '.');
int len = names.length;
if(len==1) return null;
if(len==3){
return makeup(names[len-2],names[len-1]);
}
if(len>3){
String dp = names[len-2];
if(dp.equalsIgnoreCase("com")||dp.equalsIgnoreCase("gov")||dp.equalsIgnoreCase("net")||dp.equalsIgnoreCase("edu")||dp.equalsIgnoreCase("org"))
return makeup(names[len-3],names[len-2],names[len-1]);
else
return makeup(names[len-2],names[len-1]);
}
return serverName;
}
 
/**
* 使用点号拼接字符
* @param ps
* @return
*/
private static String makeup(String...ps){
StringBuilder s = new StringBuilder();
for(int idx = 0; idx < ps.length; idx++){
if(idx > 0)
s.append('.');
s.append(ps[idx]);
}
return s.toString();
}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值