日期:2014-05-17 浏览次数:20795 次
<interceptors> <interceptor name="loginedCheck" class="com.natures.core.web.interceptor.LoginedCheckInterceptor"/> <interceptor-stack name="iframeStack"> <interceptor-ref name="loginedCheck"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 定义默认的拦截器 每个Action都会自动引用,如果Action中引用了其它的拦截器 默认的拦截器将无效 --> <default-interceptor-ref name="iframeStack"></default-interceptor-ref>
------解决方案--------------------
利用数据库的表啊
用户表中增加一个字段JUR(权限)number类型
管理员1
普通用户2
超级管理员0
登陆时验证用户名密码信息的时候,顺便验证JUR信息。通过此字段来控制跳转
------解决方案--------------------
可以好好利用struts2的优点,实现权限监听
可以这样做
假如权限存储在一个表中,
或者说用户表只有一个
1、写一个监听类
package com.interceptor;
import com.fdr.pojo.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.*;
public class UserLoginInterceptor extends AbstractInterceptor
{
public String tip;
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
public String intercept(ActionInvocation invocation) throws Exception
{
ActionContext ctx = invocation.getInvocationContext();
Map session = ctx.getSession();
User user =(User)session.get("User");
if (user != null)
{
return invocation.invoke();
}else{
ctx.put("tip" , "请登录!");
setTip("请登录!");
//return Action.LOGIN;
return "userLogin";
}
}
}
第二步,写个struts2的配置文件,也就所谓的xml,
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 定义含“权限检查拦截器”的包,供其他模块引用 -->
<package name="struts-default-with-auth-check" extends="struts-default" >
<interceptors>
<!-- 定义“权限检查拦截器” -->
<interceptor name="userCheck"
class="com.interceptor.UserLoginInterceptor">
</interceptor>
<!-- 定义含“权限检查拦截器”的拦截器栈,注意缺省的拦截器栈“defaultStack”要放在前面 -->
<interceptor-stack name="userStack">
<!-- <interceptor-ref name="defaultStack" /> -->
<interceptor-ref name="userCheck"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 正式应用可能含很多package,为了能从其他package中跳转到登录页面,把登录页面定义成全局result
<default-interceptor-ref name="userStack"></default-interceptor-ref>
-->
<global-results>
<result name="userLogin">login.jsp</result>
<result name="adminLogin&quo