J2EE的web项目里经常用到的类(2)


  1. import javax.servlet.http.Cookie;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. /**
  5.  * @author Waston
  6.  * 
  7.  * :TODO:passer en config
  8.  */
  9. public class CookieManager {
  10.     private static final long SECOND = 1000;
  11.     private static final long MINUTE = 60 * SECOND;
  12.     private static final long HOUR = 60 * MINUTE;
  13.     private static final long DAY = 24 * HOUR;
  14.     private static final long WEEK = 7 * DAY;
  15.     public static final int MAX_COOKIE_AGE = (int) (WEEK / 1000) * 8;
  16.     private final static int ENCODE_XORMASK = 0x5A;
  17.     private final static char ENCODE_DELIMETER = '/002';
  18.     private final static char ENCODE_CHAR_OFFSET1 = 'A';
  19.     private final static char ENCODE_CHAR_OFFSET2 = 'h';
  20.     public static void setCookie(HttpServletResponse res, String name,
  21.             String value, int maxAge) {
  22.         Cookie oneCookie = new Cookie(name, value);
  23.         oneCookie.setMaxAge(maxAge);
  24.         oneCookie.setPath("/");
  25.         res.addCookie(oneCookie);
  26.     }
  27.     /**
  28.      * Returns the specified Cookie object, or null if the cookie does not
  29.      * exist.
  30.      * 
  31.      * @param request
  32.      *            The HttpServletRequest object, known as "request" in a JSP
  33.      *            page.
  34.      * @param name
  35.      *            the name of the cookie.
  36.      * @return the Cookie object if it exists, otherwise null.
  37.      */
  38.     public static Cookie getCookie(HttpServletRequest request, String name) {
  39.         Cookie cookies[] = request.getCookies();
  40.         if (cookies == null || name == null || name.length() == 0) {
  41.             return null;
  42.         }
  43.         // Otherwise, we have to do a linear scan for the cookie.
  44.         for (int i = 0; i < cookies.length; i++) {
  45.             if (cookies[i].getName().equals(name)) {
  46.                 return cookies[i];
  47.             }
  48.         }
  49.         return null;
  50.     }
  51.     /**
  52.      * Returns the value of the specified cookie as a String. If the cookie does
  53.      * not exist, the method returns null.
  54.      * 
  55.      * @param request
  56.      *            the HttpServletRequest object, known as "request" in a JSP
  57.      *            page.
  58.      * @param name
  59.      *            the name of the cookie
  60.      * @return the value of the cookie, or null if the cookie does not exist.
  61.      */
  62.     public static String getCookieValue(HttpServletRequest request, String name) {
  63.         Cookie cookie = getCookie(request, name);
  64.         if (cookie != null) {
  65.             return cookie.getValue();
  66.         }
  67.         return null;
  68.     }
  69.     /**
  70.      * Invalidate the specified cookie and delete it from the response object.
  71.      * 
  72.      * @param request
  73.      *            The HttpServletRequest object, known as "request" in a JSP
  74.      *            page.
  75.      * @param response
  76.      *            The HttpServletResponse object, known as "response" in a JSP
  77.      *            page.
  78.      * @param cookieName
  79.      *            The name of the cookie you want to delete.
  80.      */
  81.     public static void invalidateCookie(HttpServletRequest request,
  82.             HttpServletResponse response, String cookieName) {
  83.         Cookie cookie = new Cookie(cookieName, null); // invalidate cookie
  84.         cookie.setMaxAge(0); // deletes cookie
  85.         cookie.setPath("/");
  86.         response.addCookie(cookie);
  87.     }
  88.     /**
  89.      * Builds a cookie string containing a username and password.
  90.      * <p>
  91.      * 
  92.      * Note: with open source this is not really secure, but it prevents users
  93.      * from snooping the cookie file of others and by changing the XOR mask and
  94.      * character offsets, you can easily tweak results.
  95.      * 
  96.      * @param username
  97.      *            The username.
  98.      * @param password
  99.      *            The password.
  100.      * @return String encoding the input parameters, an empty string if one of
  101.      *         the arguments equals <code>null</code>.
  102.      */
  103.     public static String encodePasswordCookie(String username, String password) {
  104.         StringBuffer buf = new StringBuffer();
  105.         if (username != null && password != null) {
  106.             byte[] bytes = (username + ENCODE_DELIMETER + password).getBytes();
  107.             int b;
  108.             for (int n = 0; n < bytes.length; n++) {
  109.                 b = bytes[n] ^ (ENCODE_XORMASK + n);
  110.                 buf.append((char) (ENCODE_CHAR_OFFSET1 + (b & 0x0F)));
  111.                 buf.append((char) (ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F)));
  112.             }
  113.         }
  114.         return buf.toString();
  115.     }
  116.     /**
  117.      * Unrafels a cookie string containing a username and password.
  118.      * 
  119.      * @param value
  120.      *            The cookie value.
  121.      * @return String[] containing the username at index 0 and the password at
  122.      *         index 1, or <code>{ null, null }</code> if cookieVal equals
  123.      *         <code>null</code> or the empty string.
  124.      */
  125.     public static String[] decodePasswordCookie(String cookieVal) {
  126.         // check that the cookie value isn't null or zero-length
  127.         if (cookieVal == null || cookieVal.length() <= 0) {
  128.             return null;
  129.         }
  130.         // unrafel the cookie value
  131.         char[] chars = cookieVal.toCharArray();
  132.         byte[] bytes = new byte[chars.length / 2];
  133.         int b;
  134.         for (int n = 0, m = 0; n < bytes.length; n++) {
  135.             b = chars[m++] - ENCODE_CHAR_OFFSET1;
  136.             b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
  137.             bytes[n] = (byte) (b ^ (ENCODE_XORMASK + n));
  138.         }
  139.         cookieVal = new String(bytes);
  140.         int pos = cookieVal.indexOf(ENCODE_DELIMETER);
  141.         String username = (pos < 0) ? "" : cookieVal.substring(0, pos);
  142.         String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);
  143.         return new String[] { username, password };
  144.     }
  145. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值