KazankovMarch

nginx session with cookies

Nov 19th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. *Добавил в  класс Request:*
  2.     public static final String JUNIT_SESSION_ID_COOKIE_NAME = "JUNIT_ID";
  3.  
  4.     @Override
  5.     public HttpSession getSession()
  6.     {
  7.         log("getSession");
  8.         return getSession(true);
  9.     }
  10.  
  11.     @Override
  12.     public HttpSession getSession(boolean create)
  13.     {
  14.         log("getSession: " + create);
  15.         SessionManager manager = SessionManager.getInstance();
  16.         Cookie cookie = getCookie(); //finding cookie
  17.         HttpSession result = null;
  18.         if(cookie!=null){ //cookie found
  19.             result = manager.getSession(cookie.getValue()); //finding session with id = cookie's value
  20.             if(result==null && create) { //session not found, but must be created
  21.                 result = new SessionImpl();
  22.                 manager.putSession(result.getId(), result); //saving session
  23.                 cookie.setValue(result.getId());
  24.                 addCookie(cookie); //saving session's id in cookie
  25.             }
  26.         }
  27.         else if(create) { ////cookie not found, but session must be created
  28.                 result = new SessionImpl();
  29.                 manager.putSession(result.getId(), result); //saving session
  30.                 cookie = new Cookie(JUNIT_SESSION_ID_COOKIE_NAME, result.getId()); //creation cookie
  31.                 cookies.addCookieField(JUNIT_SESSION_ID_COOKIE_NAME);
  32.                 addCookie(cookie);
  33.         }
  34.  
  35.         result = manager.checkTimeOut(result);
  36.  
  37.         return result;
  38.     }
  39.  
  40.     private void addCookie(Cookie newCookie) {
  41.         Cookie[] cs = cookies.getCookies();
  42.         for(Cookie c : cs){
  43.             if(c.getName().equals(newCookie.getName())){
  44.                 c = newCookie;
  45.                 break;
  46.             }
  47.         }
  48.         cookies.setCookies(cs);
  49.     }
  50.  
  51.     private Cookie getCookie() {
  52.         Cookie cookie = null;
  53.         Cookie[] cookies = this.getCookies();
  54.         if(cookies!=null){
  55.             for (Cookie next : cookies) {
  56.                 if (next.getName().equals(JUNIT_SESSION_ID_COOKIE_NAME)) {
  57.                     cookie = next;
  58.                     break;
  59.                 }
  60.             }
  61.         }
  62.         return cookie;
  63.     }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. *Изменил метод service в классе Context:*
  71.     public static void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
  72.         ctx_.callServlets(req, resp);
  73.  
  74.         if(resp instanceof HttpServletResponse && req instanceof HttpServletRequest){
  75.             Cookie[] cookies =  ((HttpServletRequest) req).getCookies();
  76.             for(Cookie cookie : cookies)
  77.                 ((HttpServletResponse)resp).addCookie(cookie);
  78.         }
  79.     }
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. *Упростил класс SessionManager:*
  88. package nginx.unit.session;
  89. import javax.servlet.http.HttpSession;
  90. import javax.swing.*;
  91. import java.awt.event.ActionEvent;
  92. import java.awt.event.ActionListener;
  93. import java.util.Date;
  94. import java.util.HashMap;
  95. import java.util.Map;
  96.  
  97. public class SessionManager implements ActionListener {
  98.  
  99.  
  100.     private static SessionManager ourInstance = new SessionManager();
  101.     public static SessionManager getInstance() {
  102.         return ourInstance;
  103.     }
  104.     private SessionManager() {
  105.         cleaner.start();
  106.     }
  107.  
  108.  
  109.     private int timeOut = 60_000;
  110.  
  111.     private Timer cleaner = new Timer(timeOut, this); //removes "timeouted" session every $timeOut milliseconds
  112.  
  113.     private Map<String, HttpSession> map = new HashMap<>();
  114.  
  115.     public void invalidate(SessionImpl session) {
  116.         map.remove(session.getId());
  117.     }
  118.  
  119.     @Override
  120.     public void actionPerformed(ActionEvent e) {
  121.         long now = new Date().getTime();
  122.         map.values().removeIf(httpSession -> httpSession.getCreationTime() +  timeOut >  now);
  123.     }
  124.  
  125.     public int getTimeOut() {
  126.         return timeOut;
  127.     }
  128.  
  129.     public void setTimeOut(int timeOut) {
  130.         this.timeOut = timeOut;
  131.         cleaner = new Timer(timeOut, this);
  132.         cleaner.start();
  133.     }
  134.  
  135.     public HttpSession getSession(String value) {
  136.         return map.get(value);
  137.     }
  138.  
  139.     public void putSession(String id, HttpSession session) {
  140.         map.put(id,session);
  141.     }
  142.  
  143.     public HttpSession checkTimeOut(HttpSession result) {
  144.         if(result!=null && result.getCreationTime() + timeOut < new Date().getTime()){
  145.             map.remove(result);
  146.             result = null;
  147.         }
  148.         return result;
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment