ADMIN
"/>
htmlelement>
security>
Custom label
JspsecurityTAG inherits the Bodytagsupport class. Bodytagsupport has a variable bodycontent pointing between the starting flag and the end flag.
JSPSecurityTag's private static variable RoleList saves the corresponding set of roles and page elements from the XML file, the name of the private variable ElementName corresponds to the name of the page element. When parsing the custom label, first take the name of the page element, then take the role of the current user, if the character has the permissions of the page element, the tag body (ie, the page element) is displayed, otherwise it is not displayed.
Pagekage com.presentation.view .view .view.Viewhelper.
Jspsecuritytag;
Import javax.servlet.
Jsp.tagext. *;
Import javax.servlet.
JSP. *;
Import java.util. *;
Import org.xml.sax. *;
Import org.xml.sax.helpers. *;
Import org.w3c.dom. *;
Import java.io. *;
Import javax.xml.parsers. *;
Public class
JspsecurityTag Extends Bodytagsupport {
/ / Save correspondence from the XML file to the role and page elements
Private static arraylist rolelist;
// Name of page elements
PRIVATE STRING ElementName;
Public void setElementName (String STR)
{
This.ElementName = STR;
}
Public int dressboDy () throws
JSPEXCEPTION {
IF (RoleList == Null)
{
RoleList = getList ();
}
Try {
/ / If the authentication is displayed, it will be as simple as the label body.
IF (isauthenticated (ElementName))
{
IF (BodyContent! = null) {
Jspwriter out = bodycontent.getenclosingwriter ();
BodyContent.writeout (out);
Else
{
}
}
} catch (exception e) {
Throw new
JSPEXCEPTION ();
}
Return Skip_body;
}
// Take the corresponding role and page elements from the XML configuration file, save to static arraylist
Private arraylist getList ()
{
DocumentBuilderFactory DBF =
DocumentBuilderFactory.newInstance ();
DocumentBuilder DB = NULL;
Document Doc = NULL;
Nodelist childlist = NULL;
String elementname;
String rolename;
Int index;
Arraylist thelist ();
Try {
DB = dbf.new
DocumentBuilder ();} catch (Exception E)
{
E.PrintStackTrace ();
}
Try {
DOC = db.parse (New file
SECURITY.XML
"));
} catch (Exception E)
{
E.PrintStackTrace ();
}
// Read page elements list
Nodelist elementlist = doc.getElementsBytagname
HTMLELEMENT
");
For (int i = 0; I
{
Element name = (Element) ElementList.Item (i));
// Name of page elements
ElementName = Name.GetaTRibute
"Name
");
// This page element corresponds to a list of permissions
Nodelist rolnodelist = ((nodelist) Name.GetElementsBytagname
RoleName
"));
For (int J = 0; j
{
// Name of the role with permissions
// ROLENAME = ((Element) Rolnodelist.Item (j)). getnode
Value ();
ROLENAME = (Element) Rolnodelist.Item (j)). GetAttribute (
"Name
");
Thelist.add (New ElementAndrole (ElementName, RoleName);
}
}
Return thelist;
}
/ / Check if the role has the permission of the page element
Private Boolean isauthentificated (String ElementName)
{
String rolename =
"
"
// Save the role of the user to the session when the user logs in, here is only used directly from the session.
Rolename = this.pageContext.getations (). getAttribute (
"ROLENAME");
// RoleList contains the ELEMENTNAME attribute as the // ElementAndrole object with the ROLENAME, the role is the permission of the page element.
IF (New ElementAndrole (ElementName, RoleName)))))
{
Return True;
}
}
Return False;
}
/ / Indicate the internal class of the corresponding relationship of the role and page elements
Class ElementAndrole {
String elementname;
String rolename;
Public ElementAndrole (String ElementName, String Rolename)
{
this.ElementName = ElementName;
this.RoleName = ROLENAME;
}
Public Boolean Equals (Object Obj)
{
Return ((ElementAndrole) .ElementName.Equals (this.ElementName) && ((ElementAndrole) Obj). RoleName.Equals (this.roleName));
}
}
}
In label library
Before using the JSP page, do three steps below, in
A Taglib element is included in the JSP page that determines the label library that needs to be loaded into the memory. front
The first line of the JSP file: <% @ Taglib URI =
"http:// mytag
"Prefix =
Custtag
"%> Is what this is.
2. Use the Taglib element to determine the location of the TLD file in the configuration file web.xml. Added in Web.xml:
http: // mytag <; / taglib-uri>
/Web-inf/mytag.tld
taglib-location>
taglib>
3, the TLD file must use the Taglib element to identify each custom label extreme attribute.
Here is the TLD file corresponding to this label library.
XML Version =
"1.0
"ENCoding =
"ISO-8859-1
"?>
DOCTYPE TAGLIB
Public
"- // Sun microsystems, inc .//dtd
JSP Tag Library 1.1 // en
"
"http://java.sun.com/j2ee/dtds/web-
JSptaglibrary_1_1.dtd
">
1.0 TLIBVERSION>
<
JSPVERSION> 1.1
JSPVersion>
MyTag ShortName>
JSPSecurity name>
com.presentation.viewhlper.
JSPSecurityTag tagclass>
Jspsecuritytag
info>
ElementName Name>
True reguired>
Value> True RTEXPR
Value>
attribute>
tag>
taglib>
转载请注明原文地址:https://www.9cbs.com/read-73940.html