一个权限系统建立起来是没什么问题的,问题在于如何能够很好地利用起来。
以前做struts1的时候,由于spring的AOP也不是那么好用,所以最后只好将权限应用部分写在了action中,这样,总是感觉到有那么些不爽。
记得以前用ofbiz的时候,它的权限应用就很简单了,mini-lang中只要添加一个标签就可以了,当时没细细看它的具体实现,到了用webwork2的时候,看到webwork2的拦截器,再想想ofbiz中的,也不就是个拦截器么。
struts2叫是叫"struts",但是很多还是webwork2中的,前两天打算在新架构中将权限管理放进去,由于我看struts2附带例子的时候看到拦截器中可以有初始化参数,所以就想把权限中一些内容通过初始化参数传递进去,后来试了下,是可行的,这样就方便了。
具体的权限系统是怎样的,我就不再这里赘述了,只说说拦截器部分。
下面是一个action配置文件的片断:
<interceptors>
<interceptor name="checkAccess" class="cn.wsrf.perms.PermsInterceptor"/>
</interceptors>
<global-results>
<result name="accessDeny">/accessDeny.ftl</result>
</global-results>
<action name="index">
<interceptor-ref name="checkAccess">
<param name="whiteList">test1,test2</param>
<!--param name="allowUserType">super,system</param>
<param name="permString">1|发布产品|read,insert</param-->
</interceptor-ref>
<result>index.ftl</result>
</action>
至于怎么更好利用拦截器我也不多说了,上面的“whiteList”、“allowUserType”、“permString”是3种权限验证方式,这是我写的,各人可以随意。
下面是拦截器的代码:
package cn.wsrf.perms;
import java.util.Map;
import org.apache.log4j.Logger;
import cn.wsrf.pojo.UserLogin;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class PermsInterceptor extends AbstractInterceptor
{
/**
* Logger for this class
*/
private static final Logger logger = Logger
.getLogger(PermsInterceptor.class);
private String whiteList = null;
private String allowUserType = null;
private String permString = null;
private static final String ACCESS_DENY = "accessDeny";
public String getWhiteList()
{
return whiteList;
}
public void setWhiteList(String whiteList)
{
this.whiteList = whiteList;
}
public String getAllowUserType()
{
return allowUserType;
}
public void setAllowUserType(String allowUserType)
{
this.allowUserType = allowUserType;
}
public String getPermString()
{
return permString;
}
public void setPermString(String permString)
{
this.permString = permString;
}
public String intercept(ActionInvocation ai) throws Exception
(阅读次数:)
共2页: 上一页 1 [2] 下一页