Using interceptors (Interceptor) to control logins and permissions in Struts2
Source: Internet
Author: User
using interceptors (Interceptor) to control logins and permissions in Struts2 In a JSP servlet, we typically use servlet filter to control whether a user is logged in or not, and has permission to go to a page. In Struts2 we should think of his interceptor (interceptor), interceptor plays a very important role in STRUTS2. Many of the features in STRUTS2 are implemented using interceptor.
Requirement: Simple login interface, let user enter user name, password, remember password (remember me). If the user selects remember me, the next time you do not need to log in (using the cookie implementation, with the need to click Logout Cancel the Remeber me function). If the user entered the address is not the login page, after the user login needs to go to the user input start address.
Let's see Logininterceptor.java first.
Java code package com.javaeye.dengyin2000.wallet.interceptor; import java.util.map; import javax.servlet.http.cookie; import javax.servlet.http.httpservletrequest; import org.apache.commons.lang.stringutils; import org.apache.struts2.strutsstatics; import com.javaeye.dengyin2000.wallet.dao.userdao; import com.javaeye.dengyin2000.wallet.dao.usernotfoundexception; import com.javaeye.dengyin2000.wallet.domains.user; import com.opensymphony.xwork2.actioncontext; import com.opensymphony.xwork2.actioninvocation; import com.opensymphony.xwork2.interceptor.abstractinterceptor; public class logininterceptor extends abstractinterceptor { public static final string user_session_key= "Wallet.session.user"; public static final string cookie_rememberme_key= "Wallet.cookie.rememberme"; public static final string going_to_url_key= "GOING_TO"; private UserDAO userDao; @Override public string intercept ( actioninvocation invocation) throws Exception { actioncontext actioncontext = invocation.getinvocationcontext (); httpservletrequest request= (HttpServletRequest) actioncontext.get (StrutsStatics.HTTP_REQUEST); &NBsp; map session = actioncontext.getsession (); if (Session != null && session.get (user_session_key) != null) { return Invocation.invoke (); } Cookie[] Cookies = request.getcookies (); if ( Cookies!=null) { for (cookie cookie : cookies) { if (Cookie_rememberme_key.equals (Cookie.getname ())) { string value = cookie.getvalue (); if (Stringutils.isnotblank ( Value)) { string[] split = value.split ("= ="); String userName = split[0]; string password = split[1]; try { User user = userdao .attemptlogin (Username, password); session.put (user_session_key, user); } catch ( Usernotfoundexception e) { Setgoingtourl (session, invocation); return "Login"; } } else { setgoingtourl (session, invocation); return "Login";
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.