如果用戶直接輸入了地址,不也可以直接訪問(wèn)嗎?理論上是,我們可以加入session進(jìn)行跟蹤,以杜絕此類型事件發(fā)生,我們是不是要把每次對(duì)session的判斷依次拷到每個(gè)頁(yè)里呢,之后下次需要驗(yàn)證的SESSION換了,我們?cè)贀Q?太浪費(fèi)了,我的做法是做了一個(gè)自定義標(biāo)簽,來(lái)解決這個(gè)問(wèn)題。
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class CheckTag extends TagSupport
{
??? private static final long serialVersionUID = 879137944441282895L;
??? private String check = "";//用來(lái)驗(yàn)證的變量
??? private String url = "index.jsp";//出現(xiàn)錯(cuò)誤要去的頁(yè)面
??? private String msg = "";//錯(cuò)誤的提示
??? private String scope = "";//要嚴(yán)整變量的范圍
??? private String to = "go";
//如果驗(yàn)證失敗,是將頁(yè)面后退,還是定位到哪里?
??? public String getTo()
??? {
??????? return to;
??? }
??? public void setTo( String to )
??? {
??????? this.to = to;
??? }
??? public String getMsg()
??? {
??????? return msg;
??? }
??? public void setMsg( String msg )
??? {
??????? this.msg = msg;
??? }
??? public String getScope()
??? {
??????? return scope;
??? }
??? public void setScope( String scope )
??? {
??????? this.scope = scope;
??? }
??? public String getCheck()
??? {
??????? return check;
??? }
??? public void setCheck( String check )
??? {
??????? this.check = check;
??? }
??? public String getUrl()
??? {
??????? return url;
??? }
??? public void setUrl( String url )
??? {
??????? this.url = url;
??? }
??? public int doStsrtTag() throws JspException
??? {
??????? return SKIP_BODY;
??? }
??? public int doEndTag() throws JspException
??? {
??????? boolean valid = false;//先設(shè)為不可用
??????? if ( scope.equalsIgnoreCase( "request" ) )//如果要檢查request范圍
??????? {
??????????? valid = CheckUtil.checkRequestAttribute( pageContext.getRequest(),
??????????????????? check );
??????? }
??????? else if ( scope.equalsIgnoreCase( "session" ) )
??????? {
??????????? valid = CheckUtil.checkSession( pageContext.getSession(), check );
??????? }
??????? else if ( scope.equalsIgnoreCase( "parameter" ) )
??????? {
??????????? valid = CheckUtil.checkParameter( pageContext.getRequest(), check );
??????? }
??????? else if ( scope.equalsIgnoreCase( "application" ) )
??????? {
??????????? valid = CheckUtil.checkApp( pageContext.getServletContext(), check );
??????? }
??????? if ( valid ) return EVAL_PAGE;//如果可用就繼續(xù)執(zhí)行此頁(yè)的其余部分
??????? else
??????? {//否則,哈哈
??????????? try
??????????? {
??????????????? if ( to.equalsIgnoreCase( "go" ) ) //現(xiàn)在失敗了,就看怎么回到你該到的地方
??????????????????? HtmlUtil.callParentGo(
??????????????????????? pageContext.getOut(), msg, url );//將瀏覽器定位到URL?
??????????????? else
??????????????????? HtmlUtil.callBack( pageContext.getOut(), msg );//后退一下頁(yè)面來(lái)阻止
??????????????? return SKIP_PAGE;//跳過(guò)頁(yè)面的其余部分,不執(zhí)行
??????????? }
??????????? catch ( Exception e )
??????????? {
??????????????? throw new JspException( e.toString() );
??????????? }
??????? }
??? }
??? public void release()
??? {
??????? super.release();
??????? check = "";
??????? url = "";
??????? msg = "";
??????? scope = "";
??? }
}
下面是用到的htmlUtil部分:
public static void callParentGo( Writer out, String msg, String url )
??????????? throws IOException
??? {
??????? out.write( "<script language=\"javascript\">" );
??????? out.write( "alert(\"" + msg + "\");" );
??????? out.write( "parent.location.href=\"" + url + "\";" );
??????? out.write( "</script>" );
??? }
public static void callBack( Writer out, String msg ) throws IOException
??? {
??????? out.write( "<script language=\"javascript\">" );
??????? out.write( "alert(\"" + msg + "\");" );
??????? out.write( "parent.history.back();" );
??????? out.write( "</script>" );
??? }
寫個(gè)check.tld部署吧,
<?xml version = "1.0"?>
<taglib>
?<tlibversion>1.0</tlibversion>
?<jspversion>1.1</jspversion>
?<tag>
??<name>check</name>
??<tag-class>com.boya.subject.util.CheckTag</tag-class>
??<attribute>
???<name>check</name>
???<required>true</required>
??</attribute>
??<attribute>
???<name>url</name>
???<required>false</required>
??</attribute>
??<attribute>
???<name>msg</name>
???<required>true</required>
??</attribute>
??<attribute>
???<name>scope</name>
???<required>true</required>
??</attribute>
??<attribute>
???<name>to</name>
???<required>false</required>
??</attribute>
?</tag>
</taglib>
你
只要在每個(gè)頁(yè)面里寫下這個(gè)就可以判定用戶是否登陸了
<%@ taglib prefix="boya" uri="/WEB-INF/check.tld" %>
<boya:check check="admin" msg="管理員尚未登陸,請(qǐng)登陸!" scope ="session"/>
如果沒有登陸那么,會(huì)自動(dòng)提示到首頁(yè)登陸,不錯(cuò),很完美吧?
當(dāng)然不是,您可以提出您的見解。。。。