posts - 14, comments - 22, trackbacks - 0, articles - 4
            BlogJava :: 首頁 ::  :: 聯系 :: 聚合  :: 管理

          如果用戶直接輸入了地址,不也可以直接訪問嗎?理論上是,我們可以加入session進行跟蹤,以杜絕此類型事件發生,我們是不是要把每次對session的判斷依次拷到每個頁里呢,之后下次需要驗證的SESSION換了,我們再換?太浪費了,我的做法是做了一個自定義標簽,來解決這個問題。

          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 = "";//用來驗證的變量
          ??? private String url = "index.jsp";//出現錯誤要去的頁面
          ??? private String msg = "";//錯誤的提示
          ??? private String scope = "";//要嚴整變量的范圍
          ??? private String to = "go";
          //如果驗證失敗,是將頁面后退,還是定位到哪里?

          ??? 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;//先設為不可用
          ??????? 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;//如果可用就繼續執行此頁的其余部分
          ??????? else
          ??????? {//否則,哈哈
          ??????????? try
          ??????????? {
          ??????????????? if ( to.equalsIgnoreCase( "go" ) ) //現在失敗了,就看怎么回到你該到的地方
          ??????????????????? HtmlUtil.callParentGo(
          ??????????????????????? pageContext.getOut(), msg, url );//將瀏覽器定位到URL?
          ??????????????? else
          ??????????????????? HtmlUtil.callBack( pageContext.getOut(), msg );//后退一下頁面來阻止
          ??????????????? return SKIP_PAGE;//跳過頁面的其余部分,不執行
          ??????????? }
          ??????????? 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>" );
          ??? }


          寫個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>


          只要在每個頁面里寫下這個就可以判定用戶是否登陸了

          <%@ taglib prefix="boya" uri="/WEB-INF/check.tld" %>
          <boya:check check="admin" msg="管理員尚未登陸,請登陸!" scope ="session"/>

          如果沒有登陸那么,會自動提示到首頁登陸,不錯,很完美吧?

          當然不是,您可以提出您的見解。。。。


          評論

          # re: 體驗Struts(6)---阻止非法的登陸方式  回復  更多評論   

          2009-11-02 14:17 by dawnlch
          這樣是可以阻止用戶的登錄,但是不能阻止登錄的用戶通過輸入網址去操作他并沒有的權限啊!請問這要怎么解決啊

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          有事在這里給我留言噢!
          主站蜘蛛池模板: 桦南县| 罗田县| 九龙城区| 浦东新区| 新平| 扬中市| 营山县| 布尔津县| 依兰县| 旺苍县| 灵武市| 亳州市| 崇仁县| 宣城市| 平阴县| 桓台县| 赣榆县| 河西区| 通城县| 枣庄市| 岗巴县| 金溪县| 棋牌| 博罗县| 家居| 枝江市| 大港区| 平和县| 小金县| 芮城县| 光山县| 阳朔县| 商洛市| 女性| 乌拉特前旗| 浠水县| 阳西县| 麻城市| 邵阳市| 杭州市| 乌兰浩特市|