Struts2中使用拦截器进行权限控制

时间:2022-09-26 15:29:47


拦截器:interceptor进行身份验证,判断是否是合法用户

  1:jsp(登录

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Struts2拦截器的使用</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  </head>
  <body>
        <h2>用户登录</h2><hr/>
          <font color="red">${requestScope.tip}</font><br/>
          <s:form action="login2action" method="post">
            <s:textfield name="name" label="用户名"></s:textfield>
          <s:password name="pass" label="密码"></s:password>
            <s:submit value="登录"></s:submit>
      </s:form>
  </body>
</html>


 2:struts.xml(配置拦截器)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <package name="struts2login" extends="struts-default">  
        <action name="login" class="com.test.action.LoginAction">  
            <result name="success" >/result.jsp</result>  
            <result name="error">/error.jsp</result>  
        </action>    
        <action name="input" class="com.test.action.InputAction">  
            <result name="success" >/success.jsp</result>  
            <result name="error">/error.jsp</result>  
        </action> 
        <action name="message" class="com.test.action.InputAction">  
            <result name="success" >/message.jsp</result>  
            <result name="error">/error.jsp</result>  
        </action>
       </package>   
         <package name="demo1" extends="struts-default">
           <action name="addRegister" class="com.test.action.RegisterAction">
              <result name="success">/showRegInfo.jsp</result>
           </action>
            <action name="addRegister1" class="com.test.action.Register1Action">
              <result name="success">/showRegInfo1.jsp</result>
           </action>
           <action name="convert" class="com.test.action.ConvertAction">  
           <result name="success">/convertResult.jsp</result>  
    </action>  
         </package>
              <package name="hellointerceptor" extends="struts-default">
                  <interceptors>
                      <interceptor name="helloInterceptor" class="com.test.action.HelloInterceptor">
                      </interceptor>
                  </interceptors>
                <action name="helloaction" class="com.test.action.HelloAction">
                     <result name="success">/success1.jsp</result>
                     <result name="input1">/input1.jsp</result>
                      <interceptor-ref name="defaultStack"></interceptor-ref>
                      <interceptor-ref name="helloInterceptor"></interceptor-ref>
                </action>
             </package>
             <package name="check1interceptor" extends="struts-default">
                  <interceptors>
                     <interceptor name="logincheckinterceptor" class="com.test.action.CheckInterceptor">
                   </interceptor>
                  </interceptors>
                <action name="login2action" class="com.test.action.UserAction">
                   <result name="success">/loginsuccess.jsp</result>
                   <result name="error">/loginerror.jsp</result>
                   <result name="login">/login2.jsp</result>
                   <interceptor-ref name="defaultStack"></interceptor-ref>
                      <interceptor-ref name="logincheckinterceptor"></interceptor-ref>
                </action>
             </package>
</struts>


              3:Action(控制器,负责跳转到不同页面)

package com.test.action;
import java.util.Map;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class UserAction extends ActionSupport {


private static final long serialVersionUID = 1L;
       private String name;
       private String pass;
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
       public String execute()throws Exception
       {
      ActionContext actionContext=ActionContext.getContext();
      if(getName().equals("admin")&&getPass().equals("123"))
      {
      Map sessionMap=actionContext.getSession();
      sessionMap.put("name",getName());
      return "success";
      }
      else
      {
      actionContext.put("tip", "用户名或密码错误");
      return "error";
      }
       }
}


 4:jsp(登陆成功后的页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>拦截器进行了权限控制</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
  </head>
  <body>
      您已经成功登陆了系统!
  </body>
</html>


 5;jsp(登录失败页面)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'loginerror.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
    <font color="red">${requestScope.tip}</font><br/>
      <a href="login2.jsp">重新登录</a>
  </body>
</html>

相关文章