springsecurity2學習筆記三(登陸後與struts結合、自定義訪問拒絕頁面)

一、登陸後與struts結合:

      ①測試action(user.action):

      public String execute() {
      Map session = ActionContext.getContext().getSession();
     
this.userName = (String) session.get("SPRING_SECURITY_LAST_USERNAME");
      System.out.println("userName:" + userName);
        return "success";
       }

 

      ②applicationContex-security.xml中配置:

      <http auto-config='true'  access-denied-page="/deny.jsp">(自定義訪問拒絕頁面)

      <form-login login-page="/login.jsp"
      authentication-failure-url="/login.jsp?error=true"
      always-use-default-target="true" default-target-url="/user.action" />(登陸後跳轉到action)

  

      ③login.jsp中主要:

       <form
  
action="${pageContext.request.contextPath}/j_spring_security_check"   (固定的)
   method="post" style="width: 260px; text-align: center;">
   <fieldset>
    <legend>
     登陸
    </legend>
    用戶:
    <input type="text" name="j_username" style="width: 150px;"
     value="${sessionScope['SPRING_SECURITY_LAST_USERNAME']}" />
    <br />
    密碼:
    <input type="password" name="j_password" style="width: 150px;" />
    <br />

 

       deny.jsp中主要:    

     <body>
    <font size="5">Access Deny!</font><br>
    ${requestScope['SPRING_SECURITY_403_EXCEPTION'].message}
  </body>

 

     ④在web.xml中配置struts攔截器:(忘記配置了導致404錯誤)

     <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

 

 二、工程:

   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章