短影片商城系統,session和cookie實現登入
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=" <input type="text" name="username"/> <input type="password" name="password"> <input type="submit" value="登入"> </form> </body> </html>
登入邏輯處理
透過request獲取到登入輸入的使用者名稱和密碼,這裡不做校驗,直接把使用者名稱放到session當中,然後重定向到index.html。
@PostMapping("/login") public void userLogin (HttpServletRequest request, HttpServletResponse response) { String username = request.getParameter("username"); String password = request.getParameter("password"); HttpSession httpSession = request.getSession(); httpSession.setAttribute("username", username); response.sendRedirect("index.html"); }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="獲取使用者名稱</a> </body> </html>
@GetMapping("/index") public String index(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); return (String) session.getAttribute("username"); }
@Component @WebFilter(filterName="LoginFilter",urlPatterns="/*") public class LoginFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse res = (HttpServletResponse) servletResponse; String path = req.getServletPath(); if (!StringUtils.equals("/login", path) && !path.contains("html")) { HttpSession session = req.getSession(); String username = (String)session.getAttribute("username"); if (StringUtils.isEmpty(username)) { res.sendRedirect("login.html"); } } filterChain.doFilter(servletRequest, servletResponse); } }
來自 “ ITPUB部落格 ” ,連結:https://blog.itpub.net/69978258/viewspace-3003740/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Cookie和Session的區別,Koa2+Mysql+Redis實現登入邏輯CookieSessionMySqlRedis
- Laravel 通過 cookie 實現基於 session 的單點登入LaravelCookieSession
- Springboot中登入後關於cookie和session攔截案例Spring BootCookieSession
- Java Servlet session實現登入退出JavaServletSession
- Cookie&Session,登入的那些小事兒~CookieSession
- Selenium使用Cookie實現自動登入Cookie
- 基於 Session 實現簡訊登入Session
- cookie 和 sessionCookieSession
- session和cookieSessionCookie
- Cookie和SessionCookieSession
- Android短影片系統硬編碼—實現音影片編碼(三)Android
- Android短影片系統硬編碼—實現音影片編碼(二)Android
- 【Javaweb】Cookie和SessionJavaWebCookieSession
- express+vue+mongodb+session 實現註冊登入ExpressVueMongoDBSession
- PHP實現Google Oauth的登入系統PHPGoOAuth
- session和cookie關係SessionCookie
- Session和Cookie機制SessionCookie
- 撩下Cookie和SessionCookieSession
- laravel操作session和cookieLaravelSessionCookie
- 關於Session和CookieSessionCookie
- 分散式系統Session 實現方式分散式Session
- mmall_v2.0 Redis + Cookie 實現單點登入RedisCookie
- Servlet+Session+Cookie登入、校驗、退出的邏輯程式碼ServletSessionCookie
- 許可權處理 - 用redis實現分散式session~ (cookie && session )Redis分散式SessionCookie
- 系統登入認證流程對比(cookie方式與jwt)CookieJWT
- localStorage 與 sessionStorage / cookie 和 sessionSessionCookie
- cookie和session的區別CookieSession
- 聊一聊session和cookieSessionCookie
- nodeJS之Cookie和Session(一)NodeJSCookieSession
- 深入分析Session和CookieSessionCookie
- Django框架之Cookie和SessionDjango框架CookieSession
- 對session和cookie的理解SessionCookie
- Tomcat 中的 Session 和 CookieTomcatSessionCookie
- session屬性的清除和非法登入Session
- cookie sessionCookieSession
- cookie & sessionCookieSession
- 短影片直播系統,實現高併發秒殺的多種方式
- 短影片電商系統,編寫延遲訊息實現程式碼