Struts1. X is, however, one of the most classic frameworks in the Java open source community. Although the industry spring,webwork plus Apache's recent transition to WebWork's struts2.0 in full swing, struts1.x has a unique process control advantage that will remain open for a while for some time. will be in the project to accumulate some of the experience and share with you, the level is limited, Hinder Fang home discernment.
In struts, the general extension point is on the org.apache.struts.action.RequestProcessor, inside the processpreprocess () and Processroles () Two methods can be extended by the user on demand. In this example, we processpreprocess () to determine whether the user is logged in, and in Processroles () to determine whether the user has permission to request this action. This example is designed to be implemented as: in the database, the user table has a field of 100-bit bits, each bit corresponds to a function, 0 does not have this function, and 1 is there.
New action:
<action input= "/TEMPLATE/USER/MODIEMPLOYEE.FTL" roles= "All-in-all" name= "Systemuserform" path= "/updateSysUser" scope= "Request" type= "Com.test.sysuser.UpdateSysUser" validate= "true"/>
In addition, add in Struts-config.xml:
<controller nocache= "true" inputforward= "false" maxfilesize= "2M" contenttype= "text/html;charset=gb2312" processorclass= "Com.test.common.CustomRequestProcessor"/>
The roles property of this action is three-in-one, which means that access to this action is granted only if the first and last-digit number in the user's permission bit is 1.
The following is an extended Requestprocessor class.
Package Com.test.common;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import org.apache.struts.action.ActionMapping;
Import Org.apache.struts.action.RequestProcessor;
public class Customrequestprocessor extends Requestprocessor {
Protected Boolean processpreprocess (HttpServletRequest request, httpservletresponse response) {
HttpSession session = Request.getsession ();
if (Request.getservletpath (). Equals ("/manage/login.jsp")//set which directories do not need to be logged in to access.
|| Request.getservletpath (). Equals ("/sysuserlogin.do") | | Request.getservletpath (). Equals ("/manage/exitmanage.do")) {
return true;
}
if (session! = NULL && session.getattribute ("Admin_username")! = null) {
Return true;//user has logged in, returns true;
} else {
try {//Otherwise, forward to landing page
Request.getrequestdispatcher ("/TEMPLATE/USER/LOGINDOOR.FTL")
. Forward (request, response);
} catch (Exception ex) {
Ex.printstacktrace ();
}
}
return false;
}
Protected Boolean processroles (httpservletrequest request,httpservletresponse response, actionmapping mapping) throws IOException, Servletexception {
String roles[] = Mapping.getrolenames ();//Gets an array of permissions for this user.
if (roles = = NULL | | Roles.length < 1) {
Return true;//roles is empty, indicating that anyone can access this action
}
HttpSession session = Request.getsession ();
Remove the user permission string from the session, such as: 1010111101 .....
String permission = (string) session.getattribute ("Admin_permission");
if (permission = = null) Permission = "";
for (int i = 0; i < roles.length; i++) {//To determine if the user has permission.
if (Permission.charat (Integer.parseint (Roles[i])-1) = = ' 1 ') {
Return true;//has permission to go back.
}
}
Response.sendredirect ("/TEMPLATE/NOPERMISSION.FTL"); Go to the prompt for no Permissions page.
return false;
}
}
At this point, in the browser input: Http://localhost:8080/updateSysUser.do, before the request, will automatically perform the above written two methods to verify.
Latest Java Communication group: 38366664 38366732 14061272 16183902 38545283 5952964