The key to automatic login is to store user information in cookies, and then use the Interceptor to intercept access requests when the website is accessed again, the interceptor first obtains the user information from the cookie. If the cookie contains the user information, it performs the operation that the user wants. Otherwise, it must log on.
The main code is as follows:
First, there is a checkbox in JSP for Automatic Login selection:
<Input type = "checkbox" name = "autologin" id = "autologin"/> next Automatic Login
Make a judgment during logon and save the user information to the cookie:
// Log on to Public String login () {string account = model. getaccount (); jsontip result = new jsontip ("succ", ""); try {string check = accountutil. islegal (account); If (checker. isempty (check) {result. settype ("fail"); result. setmsg ("account does not exist");} else {exuser loginuser = exuserservice. verifylogon (account, model. getpwd (); If (checker. isempty (loginuser) {result. settype ("fail"); result. setmsg ("incorrect account or password");} else {string sta Tus = loginuser. getstatus (); If ("N ". equals (Status) {result. settype ("fail"); result. setmsg ("the account has not been activated <a id = 'sendemail' href = 'javascript: resendemail (); '> click to resend the activation email </a> ");} else if ("Y ". equals (Status) {userinfo Lu = new userinfo (loginuser. GETID () + "", loginuser. getnickname (); gethttprequest (). getsession (). setattribute ("user_info", Lu); string aultlogin = model. getautologin (); If (! Checker. isempty (aultlogin) {int seconds = 30*24*60*60; cookie = new cookie ("exuser", account + "=" + model. getpwd (); cookie. setmaxage (seconds); this. gethttpresponse (). addcookie; // Save the login information to the cookie. settype ("fail"); result. setmsg ("Logon Failed"); _ log. error ("Login [" + account + "] exp:" + E. getmessage ();} print (JSON. tojson (result); return NULL ;}
Determine the interception in the Interceptor:
Public String intercept (actioninvocation Invocation) throws exception {actioncontext = invocation. getinvocationcontext (); httpservletrequest request = (httpservletrequest) actioncontext. get (Org. apache. struts2.strutsstatics. http_request); httpservletresponse response = (httpservletresponse) actioncontext. get (Org. apache. struts2.strutsstatics. http_response); userinfo user = (userinfo) Re Quest. getsession (). getattribute ("user_info"); string url = request. getservletpath (); If (checker. isempty (User )&&! URL. startswith ("/login_out") {// when the user actively clicks out, the getuserfromcookie (request);} If (URL. startswith ("/login_out") {cookie = new cookie ("exuser", null); cookie. setmaxage (0); response. addcookie; // Save the login information to Cookie} log (request); Return invocation. invoke ();} private void getuserfromcookie (httpservletrequest request) throws exception {cookie [] cookies = request. getcookies (); strin G [] cooks = NULL; string username = NULL; string Password = NULL; exuser loginuser = NULL; If (cookies! = NULL) {for (cookie coo: cookies) {If ("exuser ". equals (COO. getname () {string values = coo. getvalue (); Cooks = values. split ("="); If (cooks. length = 2) {username = cooks [0]; Password = cooks [1];} break;} If (null! = Username & null! = PASSWORD) {loginuser = exuserservice. verifylogon (username, password);} If (! Checker. isempty (loginuser) {userinfo Lu = new userinfo (loginuser. GETID () + "", loginuser. getnickname (); Request. getsession (). setattribute ("user_info", Lu );}}}
Configure in struts
<package name="global" extends="struts-default"> <interceptors> <interceptor name="accessLogInterceptor" class="cn.gzjp.common.interceptor.AccessLogInterceptor"></interceptor> <interceptor-stack name="accessLogStack"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="accessLogInterceptor"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="accessLogStack"></default-interceptor-ref> </package>
Cookie + intercepter for Automatic Login