| | |
| | | package com.lf.server.helper; |
| | | |
| | | import org.springframework.context.annotation.Configuration; |
| | | import com.lf.server.entity.all.StaticData; |
| | | |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.sql.Timestamp; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | |
| | | |
| | | private final static String COMMA = ","; |
| | | |
| | | /** |
| | | * 获取GUID |
| | | */ |
| | | public static String getGuid() { |
| | | /** |
| | | * 获取GUID |
| | | */ |
| | | return UUID.randomUUID().toString(); |
| | | } |
| | | |
| | |
| | | |
| | | return new Timestamp(now.getTimeInMillis()); |
| | | } |
| | | |
| | | /** |
| | | * 从Cookie中获取token |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static String getTokenFromCookie(HttpServletRequest request) { |
| | | Cookie[] cookies = request.getCookies(); |
| | | if (cookies == null || cookies.length == 0) { |
| | | return null; |
| | | } |
| | | |
| | | for (Cookie cookie : cookies) { |
| | | switch (cookie.getName()) { |
| | | case StaticData.TOKEN_COOKIE_KEY: |
| | | return cookie.getValue(); |
| | | default: |
| | | break; |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 向Cookie中添加token |
| | | * |
| | | * @param token |
| | | * @param request |
| | | * @param response |
| | | */ |
| | | public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) { |
| | | // 先删除 |
| | | deleteCookie(StaticData.TOKEN_COOKIE_KEY, request); |
| | | |
| | | // 再保存 |
| | | saveCookie(StaticData.TOKEN_COOKIE_KEY, token, response); |
| | | } |
| | | |
| | | /** |
| | | * 保存Cookie |
| | | * |
| | | * @param cookieKey |
| | | * @param value |
| | | * @param response |
| | | */ |
| | | public static void saveCookie(String cookieKey, String value, HttpServletResponse response) { |
| | | Cookie cookie = new Cookie(cookieKey, value); |
| | | // 设置cookie失效时间,单位为秒 |
| | | cookie.setMaxAge(240 * 60); |
| | | cookie.setHttpOnly(false); |
| | | cookie.setPath("/"); |
| | | // cookie.setDomain("") |
| | | |
| | | response.setHeader("P3P", "CP=CAO PSA OUR"); |
| | | response.addCookie(cookie); |
| | | } |
| | | |
| | | /** |
| | | * 删除cookie中的值 |
| | | * |
| | | * @param cookieKey |
| | | * @param request |
| | | */ |
| | | public static void deleteCookie(String cookieKey, HttpServletRequest request) { |
| | | Cookie[] cookies = request.getCookies(); |
| | | if (cookies != null && cookies.length > 0) { |
| | | for (Cookie c : cookies) { |
| | | if (cookieKey.equalsIgnoreCase(c.getName())) { |
| | | c.setMaxAge(0); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取Token |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static String getToken(HttpServletRequest request) { |
| | | // 1.从url参数中,获取token |
| | | String token = request.getParameter(StaticData.TOKEN_KEY); |
| | | |
| | | // 2.为空,则从header里获取 |
| | | if (token == null) { |
| | | token = request.getHeader(StaticData.TOKEN_KEY); |
| | | } |
| | | |
| | | // 3.还为空,则从cookie里获取 |
| | | if (token == null) { |
| | | token = getTokenFromCookie(request); |
| | | } |
| | | |
| | | return token; |
| | | } |
| | | } |