日期:2014-05-18 浏览次数:20850 次
package com.lil.test_ch10_03.interceptor;
import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class AuthenticationInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ctx=ActionContext.getContext();
Map session=ctx.getSession();
Object user=session.get("user");
if(user==null) {
ActionSupport action=(ActionSupport)invocation.getAction();
action.addActionError("you have to login");
return Action.LOGIN;
}
return invocation.invoke();
}
}
------解决方案--------------------
Struts2使用拦截器完成权限控制示例
struts2 权限控制
示例需求:
要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源;否则,系统直接转入登陆页面。
一、页面部分
1、登陆页面代码(login.jsp)
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title><s:text name="loginPage" /></title>
</head>
<body>
<!-- 使用form标签生成表单元素 -->
<s:form action="login">
<s:textfield name="username" label="%{getText('user')}" />
<s:textfield name="password" label="%{getText('pass')}" />
<s:submit value="%{getText('login')}" />
</s:form>
</body>
</html>