登录
注册
写文章
发现
工具
java获取客户端请求中cookie中的sessionId,销毁session重新建立sessionId
_3t3lfz KEKfID
编辑文章
java获取客户端请求中cookie中的sessionId,销毁session重新建立sessionId
asfx站长
2020.10.31 13:31:47
阅读
759
``` import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @Controller public class HomeController { private HttpSession httpSession=null; private String sessionId=null; private Cookie cookie=null; @RequestMapping("/") public String home(){ return "login"; } @RequestMapping("/login") public String login(HttpServletRequest request, HttpServletResponse response,String name,String pwd){ if(request.getCookies() ==null){ System.out.println("No Cookies"); System.out.println("Creating Cookie"); httpSession=request.getSession(true); sessionId=httpSession.getId(); cookie=new Cookie("u_id",sessionId); response.addCookie(cookie); }else{ for (Cookie c:request.getCookies()){ if(c.getName().equals("u_id")){ System.out.println("uid="+c.getName()); if(c.getValue().equals(sessionId)){ cookie.setValue(null); cookie.setMaxAge(0); cookie.setPath("/"); /*销毁session*/ request.getSession().invalidate(); sessionId= request.getSession(true).getId(); cookie=new Cookie("u_id",sessionId); System.out.println("New sessionId"+sessionId); response.addCookie(cookie); } } } } return ""; } } ```
我的主页
退出