يقدم أساسًا Struts2 مع ملفات تعريف الارتباط لتحقيق تسجيل الدخول التلقائي
عند الجمع بين Struts2 مع ملفات تعريف الارتباط ، يجب عليك الانتباه إلى استخدام إجراءات التشغيل لتحقيق قراءة ملفات تعريف الارتباط
حزمة جرة Struts2
ارتباط ملف قاعدة بيانات db.properties
dbdriver = oracle.jdbc.driver.oracledriverurl = jdbc: oracle: thin:@localhost: 1521: orclusername = testpassword = password = password
رمز طبقة داو للحصول على معلومات المستخدم من خلال اسم تسجيل الدخول
حزمة com.struts.dao.impl ؛ استيراد java.sql.connection ؛ استيراد java.sql.preparedstatement userDaoImpl ينفذ userDao {private beanconnection dbconn = new BeanConnection () ؛ تسجيل الدخول للمستخدم العام (سلسلة loginName) {connection conn = dbconn.getConnection () ؛ resultset rs = null ؛ String selsql = "SELECT * from t_scoa_sys_user حيث f_loginname = '"+loginName+"' '؛ //system.out.println(selsql) ؛ ReparedStatement PSTMT = NULL ؛ مستخدم المستخدم = فارغ ؛ حاول {pstmt = conn.preparestatement (selsql) ؛ //pstmt.setstring(3 ، loginName) ؛ rs = pstmt.executequery () ؛ بينما (rs.next ()) {user = new 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 () ؛ } إرجاع المستخدم ؛ } public void save (user user) {} public static void main (string [] args) {userDaoImpl daoimpl = new userDaoimpl () ؛ daoimpl.login ("admin") ؛ }}أداة cookieutils فئة
package com.struts.util ؛ import javax.servlet.http.cookie ؛ import javax.servlet.http.httpservletrequest ؛ import javax.servlet.http.httpaction ؛ import org.apache.commons.lang.xwork.xwork.xtringutils ؛ import orgache.struts.struts.struts.struts.Stract.Stract.StracT.StracT.StracT.StractrageTr. com.struts.action.loginAction ؛ استيراد com.struts.proj.user ؛ استيراد com.struts.service.userservice ؛ استيراد com.struts.service.impl.userserviceimpl ؛ cookieutils من الطبقة العامة {public static string user_cookie = "user.cokie" ؛ // إضافة ملف تعريف الارتباط Cookie Public AddCoOkie (مستخدم المستخدم) {cookie cookie = new cookie (user_cookie ، user.getf_loginname () + "،" + desede.decryptit (user.getf_password ())) ؛ cookie.setMaxage (60 * 60 * 24 * 365) ؛ إرجاع ملف تعريف الارتباط } // Get Cookie Public Boolean getCoookie (طلب httpservletrequest ، مستخدمي المستخدمين) {request = servleTactionContext.getRequest () ؛ ملفات تعريف الارتباط [] ملفات تعريف الارتباط = request.getCoOkies () ؛ userService = new UserviceRviceImpl () ؛ if (cookies! = null) {for (ملفات تعريف الارتباط: ملفات تعريف الارتباط) {if (cookieutils.user_cookie.equals (cookie.getName ())) {string value = cookie.getValue () ؛ // حدد ما إذا كان الحرف فارغًا إذا (stringUtils.isNotBlank (value)) {string [] spilled = value.split ("،") ؛ سلسلة logInName = سكيل [0] ؛ كلمة مرور السلسلة = سكيل [1] ؛ مستخدم المستخدم = userservice.login (loginName ، كلمة المرور) ؛ if (user! = null) {httpsession session = request.getSession () ؛ Session .SetAttribute (loginAction.user_session ، user) ؛ // إضافة مستخدم إلى إرجاع الجلسة True ؛ }}}}} إرجاع false ؛ } // حذف Cookie Public Cookie Delcookie (طلب httpservletrequest) {request = servleTactionContext.getRequest () ؛ ملفات تعريف الارتباط [] ملفات تعريف الارتباط = request.getCoOkies () ؛ if (ملفات تعريف الارتباط! = null) {for (ملفات تعريف الارتباط: ملفات تعريف الارتباط) {if (user_cookie.equals (cookie.getName ())) {cookie.setValue ("") ؛ cookie.setMaxage (0) ؛ إرجاع ملف تعريف الارتباط }} الإرجاع null ؛ }}رمز طبقة الخدمة للتحقق مما إذا كان اسم المستخدم وكلمة المرور صحيحين. يمكنني استخدام خوارزمية التشفير محليًا وأحتاج إلى فك تشفيرها. يمكن للأصدقاء إزالة كلمة المرور
package com.struts.service.impl ؛ استيراد com.struts.dao.userdao ؛ استيراد com.struts.dao.impl.userdaoimpl ؛ استيراد com.struts.proj.user ؛ استيراد com.struts.service.userservice ؛ userDaoimpl () ؛ تسجيل الدخول للمستخدم العام (سلسلة تسجيل الدخول ، كلمة مرور السلسلة) {user user = userDao.login (loginName) ؛ if (user == null) {system.out.println ("اسم المستخدم غير موجود ، يرجى التحقق من الدخول وتسجيل الدخول مرة أخرى!") ؛ } if (! desede.decryptit (user.getf_password ()). يساوي (كلمة المرور)) {system.out.println ("خطأ كلمة المرور") ؛ } إرجاع المستخدم ؛ } public static void main (string [] args) {userServiceImpl usep = new userserviceImpl () ؛ System.out.println (usepr.login ("admin" ، "1234")) ؛ }}التحقق من struts2 strots2
<؟ xml version = "1.0" isding = "utf-8"؟> <! doctype struts public "-// Apache Software Foundation // dtd struts configuration 2.1 // en" /> <name name = "struts.devmode" value = "true"/> <package name = "loginResult" تمديد = "Struts-default" namepace = "/"> <action name = "logInAction"> <result name = "success" = "redirect">/success.jsp </result> <s regur error. type = "redirect">/login.jsp </50> </usace> <!-رمز التحقق-> <Action name = "validate"> <param name = "width"> 60 </param> <param name = "height" name = "contentType"> image/jpeg </param> <param name = "inputName"> inputStream </param> </redress> </sociat
تسجيل الدخول إلى فئة ملف الإجراء
package com.struts.action ؛ import java.util.map ؛ import javax.servlet.http.cookie ؛ import javax.servlet.http.httpservletrequest ؛ import javax.servlet.http.httpservletsponse ؛ import javax.servlet.http.htpession ؛ org.apache.struts2.servletactionContext ؛ import com.opensymphony.xwork2.ActionContext ؛ import com.opensymphony.xwork2.actionsupport ؛ import com.struts.proj.user ؛ import com.struts.service.userservice ؛ com.struts.util.desede ؛ الطبقة العامة تسجيل الدخول يمتد الإجراءات {private Static Final Long SerialVersionuid = 6650955874307814247L ؛ سلسلة خاصة f_loginname ؛ سلسلة خاصة f_password ؛ استجابة خاصة httpservletresponse ؛ طلب httpservletrequest الخاص ؛ خريطة خاصة <سلسلة ، كائن> جلسة ؛ ملفات تعريف الارتباط الخاصة cookieutils = ملفات تعريف الارتباط الجديدة () ؛ خاص منطقي Usercookie ؛ سلسلة خاصة ValyAteCode ؛ Static Final String user_session = "user.session" ؛ UserService userService = New UserServiceImpl () ؛ Public String Autologin () يلقي استثناء {request = servleTactionContext.getRequest () ؛ if (cookieutils.getCoOkie (request ، userService)) {return "success" ؛ } مرة أخرى إرجاع "تسجيل الدخول" ؛ } Override Public String Execute () يلقي استثناء {httpsession session = servleTactionContext.getRequest (). getSession () ؛ حاول {string code = (string) session.getAttribute ("validateCode") ؛ if (validateCode == null ||! validateCode.equals (رمز)) {system.out.println ("يتم إدخال رمز التحقق بشكل غير صحيح ، الرجاء إدخاله بشكل صحيح") ؛ إرجاع "خطأ" ؛ } if (f_loginname! = null &&! "". يساوي (f_loginname) &&! ".". // تحديد ما إذا كان يجب إضافة إلى سلسلة ملفات تعريف الارتباط psswd = desede.decryptit (user.getf_password ()) ؛ if (user! = null && psswd.equals (f_password)) {if (UserCookie) {cookie cookie = cookieutils.addcookie (user) ؛ ActionContext.getContext (). get ("الاستجابة") ؛ servleTactionContext.getResponse (). AddCookie (ملف تعريف الارتباط) ؛ } session.setAttribute (user_session ، user) ؛ إرجاع "النجاح" ؛ }}} catch (استثناء e) {E.PrintStackTrace () ؛ } إرجاع "تسجيل الدخول" ؛ }. استجابة = servleTactionContext.getResponse () ؛ جلسة httpsession = servleTactionContext.getRequest (). getSession () ؛ الجلسة = request.getSession (false) ؛ if (الجلسة! = null) session.remoVeatTribute (user_session) ؛ ملف تعريف الارتباط = cookieutils.delcookie (طلب) ؛ إذا (ملف تعريف الارتباط! = null) استجابة. addcookie (ملف تعريف الارتباط) ؛ إرجاع "تسجيل الدخول" ؛ } main static void main (string [] args) {loginAction login = new loginAction () ؛ حاول {login.execute () ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }} الخريطة العامة <string ، object> getSession () {getting session ؛ } setSession public void (Map <String ، Object>) {this.session = session ؛ } httpservletresponse getResponse () {return response ؛ } public void setResponse (httpservletresponse response) {this.response = response ؛ } httpservletRequest getRequest () {refert request ؛ } public void setRequest (طلب httpservletrequest) {this.request = request ؛ } boolean public isUsuserCooke () {return UserCookie ؛ } public void setusercookie (boolean usercookie) {this.usercookie = userCookie ؛ } السلسلة العامة getf_loginname () {return f_loginname ؛ } public void setf_loginname (سلسلة floginname) {f_loginname = faloginname ؛ } السلسلة العامة getf_password () {return f_password ؛ } public void setf_password (string fpassword) {f_password = fpassword ؛ } السلسلة العامة getValidateCode () {return validateCode ؛ } public void setValIdateCode (String ValidateCode) {this.validateCode = validateCode ؛ }}ValidAcodeAction ، العديد من أمثلة رموز التحقق على الإنترنت ، يمكنك اختيار طريقتك الخاصة لكتابة رمز التحقق
package com.struts.action ؛ استيراد java.awt.color ؛ استيراد java.awt.font ؛ استيراد java.awt.graphics ؛ استيراد java.awt.image.bufferedImage ؛ import java.io.bytearrayinputstread javax.imageio.imageio ؛ استيراد javax.imageio.stream.imageOutputStream ؛ استيراد com.opensymphony.xwork2.actionContext ؛ استيراد com.opensymphony.xwork2 خاص bytearrayinputstream inputStream ؛ عرض int الخاص ارتفاع int الخاص خاص int fontsize ؛ Private Int CodeLength ؛ Public ValidateCodeAction () {} public void setCodelength (int codeLength) {this.codelength = codeLength ؛ } public void setFontSize (int fontsize) {this.fontSize = fontSize ؛ } public void setheight (int height) {this.height = height ؛ } public void setWidth (int width) {this.width = width ؛ } public bytearrayinputStream getInputStream () {return inputStream ؛ } public void setInputStream (bytearRayInputStream inputStream) {this.inputStream = inputStream ؛ } السلسلة العامة تنفيذ () يلقي استثناء {bufferedImage bimage = جديد bufferedImage (العرض ، الارتفاع ، 1) ؛ الرسومات g = bimage.getGraphics () ؛ عشوائي عشوائي = جديد عشوائي () ؛ G.SetColor (GetRandomColor (Random ، 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 (width) ؛ 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 = (العرض / الطول codeL - 1) * i + random.nextint (العرض / (codeLength * 2)) ؛ int y = random.nextint (الارتفاع - fontsize) + fontsize ؛ G.DrawString (RandomStr ، X ، Y) ؛ } ActionContext.getContext (). G.Dispose () ؛ bytearrayoutputstream output = new ByTearRayOutputStream () ؛ ImageOutputStream iout = imageio.createImageOutputStream (الإخراج) ؛ imageio.write (bimage ، "jpeg" ، iout) ؛ iout.close () ؛ output.close () ؛ ByteArrayInputStream في = bytearrayinputStream جديد (output.tobytearray ()) ؛ setInputStream (in) ؛ إرجاع "النجاح" ؛ } color getRandomColor (عشوائي عشوائي ، int fc ، int bc) {if (fc> 255) fc = 255 ؛ إذا (BC> 255) قبل الميلاد = 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) ؛ }}نجاح صفحة نجاح تسجيل الدخول. jsp
<٪@ page language = "java" import = "java.util.*" pageencoding = "utf-8" ٪> <٪@ page import = "com.struts.util.cookieUtils" ٪> <٪@ page import = "org.apache.commons.commons.xwork.stringutils prefix = "s" ٪> <٪ string path = request.getContextPath () ؛ String basePath = request.getScheme () + ": //" + request.getServerName () + ":" + request.getServerPort () + path + "/" ؛ ٪> <! doctype html public "-// w3c // dtd html 4.01 transitional // en"> <title> صفحة النجاح </title> </head> <body> <٪ cookie [] cookies = request.getCoOkies () ؛ if (cookies! = null) {for (ملفات تعريف الارتباط: ملفات تعريف الارتباط) {if (cookieutils.user_cookie.equals (cookie.getName ())) {string value = cookie.getValue () ؛ // حدد ما إذا كان الحرف فارغًا إذا (stringUtils.isNotBlank (value)) {string [] spilled = value.split ("،") ؛ سلسلة logInName = سكيل [0] ؛ كلمة مرور السلسلة = سكيل [1] ؛ Out.println (logInName + "مرحبًا بك في تسجيل الدخول") ؛ }}}}}} ٪> <s: a action = "loginAction! logout.action" namepace = "/"> exit Safe </s: a> </body> </html>شكرا لك على القراءة ، آمل أن تساعدك. شكرا لك على دعمك لهذا الموقع!