詳解JavaEE使用過濾器實(shí)現(xiàn)登錄(用戶自動登錄 安全登錄 取消自動登錄黑用戶禁止登錄)
在我們生活中,對于賬戶的自動登錄已經(jīng)很常見了,所以利用過濾器實(shí)現(xiàn)這個(gè)功能。
主要介紹用戶的自動登錄和取消自動登錄,以及實(shí)現(xiàn)一天自動登錄或者n天實(shí)現(xiàn)自動登錄,當(dāng)用戶ip被加入到黑名單之后,直接利用過濾器返回一個(gè)警告頁面。
過濾器的功能很是強(qiáng)大,我們只需要在寫好的前臺后servlet之后進(jìn)行添加就可以實(shí)現(xiàn)這個(gè)功能
Ps:這個(gè)僅僅只是一個(gè)演示而已,里面的訪問數(shù)據(jù)庫的部分,自己隨意模擬了下,主要是突出實(shí)現(xiàn)自動登錄的功能。
前臺代碼:
前臺代碼是成功與否都在這個(gè)頁面顯示。用到的技術(shù):jstl標(biāo)簽的應(yīng)用,session讀取值
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <c:if test="${!empty sessionScope.error }"> 你的密碼或用戶名錯(cuò)誤。<!-- 顯示后就需要把里面的值移走 --> <c:remove var="error" scope="session"/> </c:if> <c:if test="${empty sessionScope.user }" var="boo"> <h2>這是登錄的頁面</h2> <form action="<c:url value='/LoginServlet'/>" method="post"> NAME:<input type="text" name="name" /><br/> PWD:<input type="text" name="pwd" /><br/> 不自動登錄:<input type="radio" name="time" value="0" /><br/> 一天:<input type="radio" name="time" value="1" /><br/> 七天:<input type="radio" name="time" value="7" /><br/> <input type="submit" value="提交" /> </form> </c:if> <c:if test="${!boo }"> 歡迎您,${sessionScope.user },登錄成功 <a href="">模塊一 </a> <a href="">模塊2 </a> <a href="<c:url value='/CancelAutoLogin'/>">取消自動登錄</a> </c:if> </body> </html>
servlet的實(shí)現(xiàn)代碼:
和以前的代碼一樣,只負(fù)責(zé)和前臺交互即可:里面用到的技術(shù)有url編碼,值存在cookie里面,存在session里面,頁面跳轉(zhuǎn)(轉(zhuǎn)發(fā))
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name=request.getParameter("name"); String pwd=request.getParameter("pwd"); String time=request.getParameter("time"); if(name!=null && pwd!=null && name.equals(pwd)){//此處隨意寫寫,后面應(yīng)該去servvice-->dao訪問數(shù)據(jù)庫 //這里假設(shè)登錄成功了,我們把信息存入session里面 request.getSession().setAttribute("user", name); //兼容中文,我們需要進(jìn)行編碼 name=URLEncoder.encode(name, "utf-8"); pwd=URLEncoder.encode(pwd, "utf-8"); Cookie c =new Cookie("autologin", name+","+pwd);//這個(gè)value不能采用這種方式的,安全性考慮,我們必須知道采用加密,或者二次加密, int _time=60*60*24*Integer.valueOf(time); c.setMaxAge(_time); response.addCookie(c); response.sendRedirect(request.getContextPath()+"/index.jsp");//在過濾器中默認(rèn)的設(shè)置是攔截重定向,轉(zhuǎn)發(fā)是內(nèi)部直接轉(zhuǎn)發(fā),不過過濾器,不好辦,但是只需要在web.xml中配置就可以了。 }else{ request.getSession().setAttribute("error", "1"); response.sendRedirect(request.getContextPath()+"/index.jsp"); } }
到現(xiàn)在為止,都感覺沒有什么技術(shù),和以前的代碼一個(gè),現(xiàn)在就是Filter的作用了。
安全登錄:
之前我們采用過動態(tài)導(dǎo)入來進(jìn)行安全登錄,防止用戶進(jìn)入項(xiàng)目之后,不用登錄,隨意輸入都可以進(jìn)入界面,動態(tài)導(dǎo)入能夠?qū)崿F(xiàn)這個(gè)功能,但是,采用過濾器更好。
在過濾器中一般寫的都是dofilter();只需要判斷session容器里面是否為null,為null這說明這是沒有登錄的,直接踢回登錄界面,否,則放行
代碼呈上:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest) request; HttpServletResponse resp=(HttpServletResponse) response; String session=(String) req.getSession().getAttribute("user"); if(session==null){ System.out.println("非正常登錄"); resp.sendRedirect(req.getContextPath()+"/index.jsp"); }else{ System.out.println("成功登錄"); chain.doFilter(req, resp); } }
字符編碼:
字符編碼的問題,以前每一次都需要在servlet的dopost()里面自己手動輸入,request.setCharacterEncoding("utf-8");每一個(gè)servlet都需要輸入,太麻煩,我們采用過濾器實(shí)現(xiàn);
代碼呈上:
<span style="font-size:18px;">public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding(character);//去客戶端接收的編碼 response.setContentType("text/html;charset=utf-8");//設(shè)置發(fā)出去的編碼 chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException { character=config.getInitParameter("character");//a</span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;">haracter 設(shè)置為全局變量,</span><span style="font-size:18px;"> }</span>
再上面的character定義為全局變量,初始值在web.xml中配置。
web.xml代碼呈上:
<filter> <filter-name>character</filter-name> <filter-class>cn.hncu.Filter.CharacterFilter</filter-class> <init-param> <param-name>character</param-name> <param-value>UTF-8</param-value> </init-param> </filter>
自動登錄:
主要思路:自動登錄需要判斷session里面是都存有值,有,則登錄過了,沒有,就去本地cookie查找,存在,去數(shù)據(jù)庫匹配,若匹配成功,就把session容器添加值。
代碼呈上:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //自動登錄,必須要設(shè)置session里面是都有值,有,則當(dāng)前登錄過,沒有,就要去訪問cookie里面的數(shù)據(jù),cookie里面的數(shù)據(jù) //是否和數(shù)據(jù)庫里面的匹配,是,將session里面的值在這里設(shè)置,否,放走 HttpServletRequest req=(HttpServletRequest) request; HttpServletResponse resp =(HttpServletResponse) response; String session =(String) req.getSession().getAttribute("user"); if(session==null){//說明當(dāng)前沒有登錄過 Cookie cs[]=req.getCookies(); if(cs!=null){ for(Cookie c:cs){ if(c.getName().equals("autologin")){ String value=c.getValue();//這是經(jīng)過加密的,但是我們僅僅只是采用逗號連接了一下。 String[] strs=value.split(",");//在logserlvet里面采用的是先編碼,再采用逗號連接,我們這里需要反過來 String name=URLDecoder.decode(strs[0], "utf-8"); String pwd=URLDecoder.decode(strs[1], "utf-8"); //將name,pwd數(shù)據(jù)拿到后臺訪問數(shù)據(jù)庫,我們這里只是隨便寫寫 if(name.equals(pwd)){ req.getSession().setAttribute("user", name);//設(shè)置session里面的參數(shù) break; } } } } } chain.doFilter(req, resp);//一定要放走哦。。 }
黑名單用戶
黑名單用戶,不準(zhǔn)登錄,直接告訴它結(jié)果
代碼呈上:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest) request; HttpServletResponse resp=(HttpServletResponse) response; String ip=req.getRemoteAddr();//獲取訪問的ip; System.out.println(ip+"IIPP"); if(set.contains(ip)){//在黑名單之內(nèi) System.out.println("set"); resp.getWriter().print("您屬于黑名單..<a href='"+req.getContextPath()+"/index.jsp'>返回</a>"); //返回也是不行的,因?yàn)閕ndex向服務(wù)器請求的時(shí)候就直接攔截了 }else{ chain.doFilter(req, resp); } }
黑名單返回的類型為list最好,我這里是自己手動添加,原本應(yīng)該從寫一個(gè)工具類從數(shù)據(jù)庫讀取,不止能查,還能增刪改--黑名單。
代碼呈上:
Hashset 定義為全局變量,set里面含有contain,效率很高。
public void init(FilterConfig arg0) throws ServletException { //這里是黑名單列表,從數(shù)據(jù)庫中調(diào)取出來。這里只是簡單的模擬下 set.add("192.132.0.12");//這是黑IP,這個(gè)是從后臺數(shù)據(jù)庫拿到的。 set.add("localhost"); set.add("192.132.32.4"); set.add("127.0.0.1"); }
取消自動登錄
當(dāng)一直自動登錄認(rèn)為不安全,所以我們設(shè)置沒有自動登錄
之前我們知道,自動登錄靠的是cookie里面存的技術(shù),所以這里我們只需要將cookie刪除就可以了
因?yàn)槿∠詣拥卿浭浅溄?,所以寫的是servlet.
代碼呈上:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Cookie cc=new Cookie("autologin", "");//刪除cookie的方法,就建立一個(gè)同名connkie,然后設(shè)置cookie的setmaxage=0; cc.setMaxAge(0); cc.setPath(req.getContextPath()); resp.addCookie(cc); resp.sendRedirect(req.getContextPath()+"/index.jsp"); }
上面這些就能實(shí)現(xiàn)這些簡答的功能了。
以上所述是小編給大家介紹的詳解JavaEE使用過濾器實(shí)現(xiàn)用戶自動登錄 安全登錄 取消自動登錄黑用戶禁止登錄的相關(guān)知識,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用
這篇文章主要介紹了詳解XML,Object,Json轉(zhuǎn)換與Xstream的使用的相關(guān)資料,需要的朋友可以參考下2017-02-02Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)
這篇文章主要為大家介紹了Java?C++題解leetcode801使序列遞增的最小交換次數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Java的Cglib動態(tài)代理實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Java的Cglib動態(tài)代理實(shí)現(xiàn)方式詳解,CGLIB是強(qiáng)大的、高性能的代碼生成庫,被廣泛應(yīng)用于AOP框架,它底層使用ASM來操作字節(jié)碼生成新的類,為對象引入間接級別,以控制對象的訪問,需要的朋友可以參考下2023-11-11