ส่วนใหญ่แนะนำ struts2 รวมกับคุกกี้เพื่อให้ได้การเข้าสู่ระบบอัตโนมัติ
เมื่อรวม struts2 เข้ากับคุกกี้คุณควรใส่ใจกับการใช้การกระทำ. การกระทำเพื่อตระหนักถึงการอ่านคุกกี้
แพ็คเกจ Jar ของ Struts2
ลิงก์ฐานข้อมูลไฟล์ db.properties
dbDriver = oracle.jdbc.driver.oracledriverurl = jdbc: Oracle: Thin: @localhost: 1521: orclusername = testPassword = รหัสผ่าน
รหัสเลเยอร์ DAO เพื่อรับข้อมูลผู้ใช้ผ่านชื่อเข้าสู่ระบบ
แพ็คเกจ com.struts.dao.impl; นำเข้า java.sql.connection; นำเข้า java.sql.preparedStatement; นำเข้า java.sql.resultset; นำเข้า java.sql.sqlexception; นำเข้า com.struts.dao.userdao; UserDaoimpl ใช้ UserDao {ส่วนตัว beanconnection dbconn = new beanconnection (); เข้าสู่ระบบผู้ใช้สาธารณะ (LOGINNAME String) {Connection Conn = dbConn.getConnection (); ผลลัพธ์ RS = NULL; String selsql = "เลือก * จาก t_scoa_sys_user โดยที่ f_loginname = '"+loginName+"'"; //system.out.println(selsql); PreparedStatement PSTMT = NULL; ผู้ใช้ผู้ใช้ = null; ลอง {pstmt = conn.prepareStatement (SELSQL); //pstmt.setstring(3, LoginName); rs = pstmt.executeQuery (); ในขณะที่ (rs.next ()) {user = user ใหม่ (); user.setId (Rs.getLong (1)); user.setf_username (rs.getString (2)); user.setf_loginname (rs.getString (3)); user.setf_sex (rs.getString (4)); user.setf_state (Rs.getString (5)); user.setf_email (Rs.getString (6)); user.setf_mobilephone (Rs.getString (7)); user.setf_secretaryId (rs.getlong (8)); user.setf_password (rs.getString (9)); user.setf_order (rs.getlong (10)); user.setf_note (rs.getString (11)); user.setf_infomodifytemplateId (Rs.getLong (12)); }} catch (sqlexception e) {e.printstacktrace (); } return user; } โมฆะสาธารณะบันทึก (ผู้ใช้ผู้ใช้) {} โมฆะคงที่สาธารณะหลัก (สตริง [] args) {userdaoimpl daoimpl = ใหม่ userdaoimpl (); daoimpl.login ("ผู้ดูแลระบบ"); -คลาส Cookieutils Tool
แพ็คเกจ com.struts.util; นำเข้า Javax.servlet.http.cookie; นำเข้า Javax.servlet.http.httpservletrequest นำเข้า Javax.servlet.http.httpsession com.struts.action.loginaction; นำเข้า com.struts.proj.user; นำเข้า com.struts.service.userservice; นำเข้า com.struts.service.impl.userserviceimpl; cookieutils ชั้นเรียนสาธารณะ // เพิ่มคุกกี้คุกกี้ AddCookie (ผู้ใช้ผู้ใช้) {คุกกี้คุกกี้ = คุกกี้ใหม่ (user_cookie, user.getf_loginname () + "," + desede.decryptit (user.getf_password ()); COOKIE.SetMaxage (60 * 60 * 24 * 365); กลับมาคุกกี้; } // รับคุกกี้บูลีนสาธารณะ getCookie (คำขอ httpservletRequest, ผู้ใช้ Userservice userservice) {request = servletactionContext.getRequest (); คุกกี้ [] คุกกี้ = request.getCookies (); userservice = ใหม่ userserviceimpl (); if (คุกกี้! = null) {สำหรับ (คุกกี้คุกกี้: คุกกี้) {ถ้า (cookieutils.user_cookie.equals (cookie.getName ())) {ค่าสตริง = cookie.getValue (); // ตรวจสอบว่าอักขระนั้นว่างเปล่าถ้า (stringutils.isnotblank (ค่า)) {string [] spilled = value.split (","); สตริง loginName = หก [0]; สตริงรหัสผ่าน = หก [1]; user user = userservice.login (ชื่อเข้าสู่ระบบ, รหัสผ่าน); if (user! = null) {httpsession session = request.getSession (); เซสชัน. setAttribute (loginaction.user_session, ผู้ใช้); // เพิ่มผู้ใช้ในเซสชันส่งคืนจริง; }}}}} return false; } // ลบคุกกี้คุกกี้สาธารณะ Delcookie (คำขอ httpservletRequest) {request = servletactionContext.getRequest (); คุกกี้ [] คุกกี้ = request.getCookies (); if (คุกกี้! = null) {สำหรับ (คุกกี้คุกกี้: คุกกี้) {ถ้า (user_cookie.equals (cookie.getName ())) {cookie.setValue (""); COOKIE.SETMAXAGE (0); กลับมาคุกกี้; }} return null; -รหัสเลเยอร์บริการเพื่อตรวจสอบว่าชื่อผู้ใช้และรหัสผ่านถูกต้องหรือไม่ ฉันใช้อัลกอริทึมการเข้ารหัสในพื้นที่และจำเป็นต้องถอดรหัส เพื่อนสามารถลบรหัสผ่านได้
แพ็คเกจ com.struts.service.impl; นำเข้า com.struts.dao.userdao; นำเข้า com.struts.dao.impl.userdaoimpl; นำเข้า com.struts.proj.user; นำเข้า com.struts.service.userService; userdaoimpl (); การเข้าสู่ระบบของผู้ใช้สาธารณะ (LOGINNAME String, รหัสผ่านสตริง) {ผู้ใช้ user = userDao.login (ชื่อเข้าสู่ระบบ); ถ้า (user == null) {system.out.println ("ชื่อผู้ใช้ไม่มีอยู่โปรดตรวจสอบและเข้าสู่ระบบอีกครั้ง!"); } if (! desede.decryptit (user.getf_password ()). เท่ากับ (รหัสผ่าน)) {system.out.println ("ข้อผิดพลาดรหัสผ่าน"); } return user; } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {userserviceimpl useimp = ใหม่ userserviceimpl (); System.out.println (useimp.login ("ผู้ดูแลระบบ", "1234")); -การตรวจสอบไฟล์การกำหนดค่า struts2.xml, การเข้าสู่ระบบและรหัสการตรวจสอบ ValidateCodeaction ของ struts2
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.1 // en" "http://struts.apache.org/dtds/struts-2.1.dtd" /> <ค่าคงที่ = "struts.devmode" value = "true"/> <package name = "loginResult" ขยาย = "struts-default" namespace = "/"> <action name = "loginAction"> <result name = "success" type = "Redirect">/success.jsp < type = "redirect">/login.jsp </result> </action> <!-รหัสการตรวจสอบ-> <action name = "ตรวจสอบ"> <param name = "Width"> 60 </param> <param name = "ความสูง"> 20 </param> <param name = "fontsize"> 18 </param> name = "contentType"> image/jpeg </param> <param name = "inputName"> inputStream </param> </result> </action> </package> </struts>
แอคชั่นคลาสไฟล์เข้าสู่ระบบ
แพ็คเกจ com.struts.action; นำเข้า java.util.map; นำเข้า javax.servlet.http.cookie; นำเข้า Javax.servlet.http.httpservletrequest นำเข้า javax.servlet.http.htttpservletresponse; org.apache.struts2.servletactionContext; นำเข้า com.opensymphony.xwork2.actioncontext; นำเข้า com.opensymphony.xwork2.ActionSupport; นำเข้า com.struts.proj.user; นำเข้า com.struts.service.Service.Service; com.struts.util.cookieutils; Import com.struts.util.desede; การเข้าสู่ระบบระดับสาธารณะขยายการดำเนินการ {ส่วนตัวคงที่สุดท้ายคงที่ความยาว serialversionuid = 6650955874307814247L; สตริงส่วนตัว f_loginname; สตริงส่วนตัว f_password; การตอบสนอง HTTPSERVLETRESSESSIVE ส่วนตัว; คำขอ httpservletrequest ส่วนตัว; แผนที่ส่วนตัว <สตริงวัตถุ> เซสชัน; cookieutils ส่วนตัว cookieutils = new Cookieutils (); บูลีนส่วนตัว Usercookie; สตริงส่วนตัว ValidateCode; สตริงสุดท้ายคงที่สาธารณะ user_session = "user.session"; UserserVice UserserVice = ใหม่ UserserViceImpl (); สตริงสาธารณะ autologin () โยนข้อยกเว้น {request = servletActionContext.getRequest (); if (cookieutils.getCookie (คำขอ userservice)) {return "ความสำเร็จ"; } return "เข้าสู่ระบบ"; } @Override สตริงสาธารณะ execute () พ่นข้อยกเว้น {httpsession session = servletactionContext.getRequest (). getSession (); ลอง {String code = (string) session.getAttribute ("ValidateCode"); if (validateCode == null ||! validateCode.equals (รหัส)) {system.out.println ("รหัสการตรวจสอบถูกป้อนไม่ถูกต้องโปรดป้อนอย่างถูกต้อง"); ส่งคืน "ข้อผิดพลาด"; } if (f_loginName! = null &&! "". equals (f_loginName) &&! "". equals (f_password) && f_password! = null) {user user = userservice.login (f_loginname // พิจารณาว่าจะเพิ่มลงในสตริงคุกกี้ psswd = desede.decryptit (user.getf_password ()); if (user! = null && psswd.equals (f_password)) {ถ้า (usercookie) {คุกกี้คุกกี้ = cookieutils.addcookie (ผู้ใช้); ActionContext.getContext () รับ ("การตอบสนอง"); servletactionContext.getResponse (). addCookie (คุกกี้); } session.setAttribute (user_session, ผู้ใช้); กลับ "ความสำเร็จ"; }}} catch (exception e) {e.printstacktrace (); } return "เข้าสู่ระบบ"; } // ผู้ใช้ออกจากการจัดวางสตริงสาธารณะ () {request = servletActionContext.getRequest (); response = servletactionContext.getResponse (); httpsession session = servletactionContext.getRequest (). getSession (); เซสชัน = request.getSession (เท็จ); if (เซสชัน! = null) เซสชัน Removeattribute (user_session); คุกกี้คุกกี้ = cookieutils.delcookie (คำขอ); ถ้า (คุกกี้! = null) response.addcookie (คุกกี้); กลับ "เข้าสู่ระบบ"; } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {loginaction login = ใหม่ loginAction (); ลอง {login.execute (); } catch (exception e) {e.printstacktrace (); }} แผนที่สาธารณะ <String, Object> getSession () {return session; } การตั้งค่าโมฆะสาธารณะ (แผนที่ <สตริงวัตถุ> เซสชัน) {this.session = เซสชัน; } public httpservletResponse getResponse () {return response; } โมฆะสาธารณะ setResponse (การตอบสนอง httpservletResponse) {this.Response = การตอบสนอง; } สาธารณะ httpservletRequest getRequest () {คำขอส่งคืน; } โมฆะสาธารณะ setRequest (คำขอ httpservletRequest) {this.request = คำขอ; } บูลีนสาธารณะ isusercookie () {return usercookie; } โมฆะสาธารณะ setusercookie (บูลีน usercookie) {this.usercookie = usercookie; } สตริงสาธารณะ getf_loginname () {return f_loginname; } โมฆะสาธารณะ setf_loginname (สตริง floginname) {f_loginName = floginName; } สตริงสาธารณะ getf_password () {return f_password; } โมฆะสาธารณะ setf_password (สตริง fpassword) {f_password = fpassword; } สตริงสาธารณะ getValidateCode () {return validateCode; } โมฆะสาธารณะ setValidateCode (String validateCode) {this.validateCode = ValidateCode; -Validacodeaction หลายตัวอย่างของรหัสการตรวจสอบบนอินเทอร์เน็ตคุณสามารถเลือกวิธีการเขียนรหัสการยืนยันของคุณเอง
แพ็คเกจ com.struts.action; นำเข้า java.awt.color; นำเข้า java.awt.font; นำเข้า java.awt.graphics; นำเข้า java.awt.image.bufferedimage; นำเข้า java.io.bytearrayinputstream; javax.imageio.stream.imageoutputstream; นำเข้า com.opensymphony.xwork2.actioncontext; นำเข้า com.opensymphony.xwork2.actionsupport; ValidateCodeaction ระดับสาธารณะขยายการกระทำ Private ByteArrayInputStream InputStream; ความกว้าง int ส่วนตัว; ความสูง int ส่วนตัว; แบบอักษร INT ส่วนตัว; ความยาว codelength ส่วนตัว; Public ValidateCodeaction () {} โมฆะสาธารณะ setCodeLength (int codelength) {this.codelength = codelength; } โมฆะสาธารณะ setFontSize (int fontsize) {this.fontsize = FontSize; } โมฆะสาธารณะ setheight (ความสูง int) {this.height = ความสูง; } โมฆะสาธารณะ setWidth (ความกว้าง int) {this.width = ความกว้าง; } สาธารณะ byteArrayInputStream getInputStream () {return inputStream; } โมฆะสาธารณะ setInputStream (byTeArrayInputStream inputStream) {this.inputStream = inputStream; } สตริงสาธารณะ execute () พ่นข้อยกเว้น {bufferedImage bimage = bufferedImage ใหม่ (ความกว้าง, ความสูง, 1); กราฟิก g = bimage.getGraphics (); สุ่มสุ่ม = ใหม่สุ่ม (); G.SetColor (getRandomColor (สุ่ม, 200, 255)); G.FillRect (0, 0, ความกว้าง, ความสูง); G.SetFont (ตัวอักษรใหม่ ("Times New Roman", 0, FontSize)); G.SetColor (getRandomColor (สุ่ม, 160, 200)); สำหรับ (int i = 0; i <155; i ++) {int x = random.nextint (ความกว้าง); int y = random.nextint (ความสูง); int xl = random.nextint (12); int yl = random.nextint (12); G.Drawline (x, y, x + xl, y + yl); } StringBuffer str = new StringBuffer (); สำหรับ (int i = 0; i <codeLength; i ++) {string randomstr = string.valueof (random.nextint (10)); str.append (randomstr); G.SetColor (สีใหม่ (20 + random.nextint (110), 20 + สุ่ม. nextint (110), 20 + random.nextint (110), 20 + random.nextint (110))); int x = (ความกว้าง / codelength - 1) * i + random.nextint (ความกว้าง / (codelength * 2)); int y = random.nextint (ความสูง - fontsize) + fontsize; G.DrawString (Randomstr, x, y); } actionContext.getContext (). getSession (). ใส่ ("validateCode", str.toString ()); G.Dispose (); ByTeArrayOutputStream output = new ByTeArrayOutputStream (); imageOutputStream iout = imageio.createImageOutputStream (เอาต์พุต); Imageio.write (bimage, "jpeg", iout); iout.close (); output.close (); ByTeArrayInputStream ใน = New ByteArrayInputStream (output.tobyteArray ()); SetInputStream (IN); กลับ "ความสำเร็จ"; } สีส่วนตัว getRandomColor (สุ่มสุ่ม, int fc, int bc) {ถ้า (fc> 255) fc = 255; ถ้า (BC> 255) BC = 255; int r = fc + random.nextint (bc - fc); int g = fc + random.nextint (bc - fc); int b = fc + random.nextint (bc - fc); คืนสีใหม่ (R, G, B); -เข้าสู่ระบบหน้าความสำเร็จ Success.jsp
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ page import = "com.struts.util.cookieutils"%> <%@ page import = prefix = "s"%> <%String Path = request.getContextPath (); String basepath = request.getScheme () + ": //" + request.getServerName () + ":" + request.getServerport () + path + "/";%> <! <title> หน้าความสำเร็จ </title> </head> <body> <% คุกกี้ [] คุกกี้ = request.getCookies (); if (คุกกี้! = null) {สำหรับ (คุกกี้คุกกี้: คุกกี้) {ถ้า (cookieutils.user_cookie.equals (cookie.getName ())) {ค่าสตริง = cookie.getValue (); // ตรวจสอบว่าอักขระนั้นว่างเปล่าถ้า (stringutils.isnotblank (ค่า)) {string [] spilled = value.split (","); สตริง loginName = หก [0]; สตริงรหัสผ่าน = หก [1]; out.println (loginName + "ยินดีต้อนรับสู่การเข้าสู่ระบบ"); }}}}} %> <s: a action = "loginAction!ขอบคุณสำหรับการอ่านฉันหวังว่ามันจะช่วยคุณได้ ขอบคุณสำหรับการสนับสนุนเว็บไซต์นี้!