| | |
| | | import org.springframework.web.context.request.RequestContextHolder; |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | |
| | | |
| | | /** |
| | | * 获取用户ip |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static String getIpAddress(HttpServletRequest request) { |
| | | String ip = request.getHeader("X-Forwarded-For"); |
| | |
| | | |
| | | /** |
| | | * 获取当前时间指定分钟数后的Timestamp |
| | | * |
| | | * @param min 分钟数 |
| | | * @return |
| | | */ |
| | | public static Timestamp getTimestamp(int min) { |
| | | Calendar now = Calendar.getInstance(); |
| | |
| | | |
| | | /** |
| | | * 从Cookie中获取token |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static String getTokenFromCookie(HttpServletRequest request) { |
| | | Cookie[] cookies = request.getCookies(); |
| | |
| | | |
| | | /** |
| | | * 向Cookie中添加token |
| | | * |
| | | * @param token |
| | | * @param request |
| | | * @param response |
| | | */ |
| | | public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) { |
| | | // 先删除 |
| | |
| | | |
| | | /** |
| | | * 保存Cookie |
| | | * |
| | | * @param key |
| | | * @param value |
| | | * @param response |
| | | */ |
| | | public static void saveCookie(String key, String value, HttpServletResponse response) { |
| | | Cookie cookie = new Cookie(key, value); |
| | |
| | | |
| | | /** |
| | | * 删除cookie中的值 |
| | | * |
| | | * @param cookieKey |
| | | * @param request |
| | | */ |
| | | public static void deleteCookie(String cookieKey, HttpServletRequest request, HttpServletResponse response) { |
| | | Cookie[] cookies = request.getCookies(); |
| | |
| | | |
| | | /** |
| | | * 删除所有Cookie |
| | | * |
| | | * @param request |
| | | * @param response |
| | | */ |
| | | public static void deleteCookies(HttpServletRequest request, HttpServletResponse response) { |
| | | Cookie[] cookies = request.getCookies(); |
| | |
| | | |
| | | /** |
| | | * 获取Token |
| | | * |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static String getToken(HttpServletRequest request) { |
| | | // 1.从url参数中,获取token |
| | |
| | | |
| | | /** |
| | | * 获取Request |
| | | * |
| | | * @return |
| | | */ |
| | | public static HttpServletRequest getRequest() { |
| | | ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | |
| | | |
| | | /** |
| | | * 获取Response |
| | | * |
| | | * @return |
| | | */ |
| | | public static HttpServletResponse getResponse() { |
| | | ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | |
| | | |
| | | /** |
| | | * 获取Session |
| | | * |
| | | * @return |
| | | */ |
| | | public static HttpSession getSession() { |
| | | return getRequest().getSession(); |
| | | } |
| | | |
| | | /** |
| | | * 获取真实路径 |
| | | */ |
| | | public static String getRealPath(String path) { |
| | | HttpServletRequest req = getRequest(); |
| | | ServletContext ctx = req.getSession().getServletContext(); |
| | | |
| | | return ctx.getRealPath("/" + path); |
| | | } |
| | | |
| | | /** |
| | | * 输出json数据到前端 |
| | | * |
| | | * @param response |
| | | * @param jsonPack |
| | | * @throws Exception |
| | | */ |
| | | public static boolean write2Page(HttpServletResponse response, String jsonPack) throws IOException { |
| | | response.setContentType("application/json;charset=UTF-8"); |