关闭

使用拦截器进行权限控制

标签: strutsstruts2.0
166人阅读 评论(0) 收藏 举报
分类:

 代码:

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts2 拦截器应用</title>
</head>
<body>
<h1>用户登录</h1>
<font color="red">${requestScope.tip}</font>
<s:form id="id" action="login">
  <s:textfield name="name" label="用户名"></s:textfield>
  <s:password name="pass" label="密码"></s:password>
  <s:submit value="注册"></s:submit>
</s:form>
 <a href=chakan.action>看咪咪^_^</a>
</body>
</html>

loginsuccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>拦截器权限控制</title>
</head>
<body>
你已成功登录系统!<a href=chakan.action>看咪咪^_^</a>
</body>
</html>

info.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>这是一个伟大的咪咪</title>
</head>
<body>
              一个人把自己看得太高,就会被别人看低;<br/>
	一个人把自己看得低一点,就会被别人看高和尊重。<br/>
	故意抬高自己是一种心虚,故意贬低自己也会矫揉造作。<br/>
	平和的神情、真诚的态度和不在意别人眼中是否贵贱的肚量,是祥和生活的保证。<br/>
										-俞敏洪
</body>
</html>

UserAction.java

package com.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;

public class UserAction extends ActionSupport {
  private static final long serialVersionUID = 1L;
  private String name;
  private String pass;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getPass() {
	return pass;
}
public void setPass(String pass) {
	this.pass = pass;
}
@Override
@SuppressWarnings("unchecked")
public String execute() throws Exception {
	// TODO Auto-generated method stub
	ActionContext actionContext=ActionContext.getContext();
	if(getName().equals("haige") && getPass().equals("haige")){
		Map sessionMap = actionContext.getSession();
		sessionMap.put("name", getName());
		return SUCCESS;
	}
	else{
		actionContext.put("tip", "用户名或密码不对!");
		return ERROR;
	}
}

}

CheckInterceptor.java(权限控制的拦截器)

package com.inter;

import java.util.Map;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;
	public String intercept(ActionInvocation arg0) throws Exception {
		ActionContext actionContext=arg0.getInvocationContext();
        Map<?, ?> sessionMap = actionContext.getSession();
		String user = (String)sessionMap.get("name");
		if(user != null && user.equals("haige")){
			return arg0.invoke();
		}
		actionContext.put("tip","请先登录!");
		return Action.LOGIN;
	}

}

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
  <package name="intercertor" extends="struts-default">
     <interceptors>
         <interceptor name="loginInterceptor" class="com.inter.CheckInterceptor"></interceptor>
     </interceptors>
     
     <action name="login" class="com.action.UserAction">
        <result name="error">/login.jsp</result>
		<result name="success">/loginsuccess.jsp</result>
     </action>
     
     <action name="chakan">
         <result>/info.jsp</result>
		 <result name="login">/login.jsp</result>
		 <interceptor-ref name="defaultStack"></interceptor-ref>
		 <interceptor-ref name="loginInterceptor"></interceptor-ref>
     </action>
  </package>
</struts>

截图:








0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:314008次
    • 积分:10304
    • 等级:
    • 排名:第1828名
    • 原创:742篇
    • 转载:6篇
    • 译文:0篇
    • 评论:80条
    最新评论