当前位置:文档之家› 权限的表设计

权限的表设计

基于RBAC的权限分配:在做权限分配时:首先需要几张表。

见下图用户表角色表:用户角色表:角色权限表:struts2 角色权限filter(过滤器)和interceptor(拦截器)分类:mvc框架-struts22012-12-23 11:27 358人阅读评论(0) 收藏举报Struts2项目通过使用Struts的if标签进行了session判断,使得未登录的用户不能看到页面,但是这种现仅仅在view层进行,如果未登录用户直接在地址栏输入登录用户才能访问的地址,那么相应的action还是会执行,仅仅是不让用户看到罢了。

这样显然是不好的,所以研究了一下Struts2的权限验证。

权限最核心的是业务逻辑,具体用什么技术来实现就简单得多。

通常:用户与角色建立多对多关系,角色与业务模块构成多对多关系,权限管理在后者关系中。

对权限的拦截,如果系统请求量大,可以用Struts2拦截器来做,请求量小可以放在filter中。

但一般单级拦截还不够,要做到更细粒度的权限控制,还需要多级拦截。

不大理解filter(过滤器)和interceptor(拦截器)的区别,遂google之。

博文中有介绍:1、拦截器是基于java的反射机制的,而过滤器是基于函数回调。

2、过滤器依赖与servlet容器,而拦截器不依赖与servlet容器。

3、拦截器只能对action请求起作用,而过滤器则可以对几乎所有的请求起作用。

4、拦截器可以访问action上下文、值栈里的对象,而过滤器不能。

5、在action的生命周期中,拦截器可以多次被调用,而过滤器只能在容器初始化时被调用一次。

为了学习决定把两种实现方式都试一下,然后再决定使用哪个。

权限验证的Filter实现:web.xml代码片段1<!-- authority filter 最好加在Struts2的Filter前面-->2<filter>3<filter-name>SessionInvalidate</filter-name>4<filter-class>filter.SessionCheckFilter</filter-class>5<init-param>6<param-name>checkSessionKey</param-name>7<param-value>loginName</param-value>8</init-param>9<init-param>10<param-name>redirectURL</param-name>11<param-value>/entpLogin.jsp</param-value>12</init-param>13<init-param>14<param-name>notCheckURLList</param-name>15<param-value>/entpLogin.jsp,/rois/loginEntp.action,/entpRegister.jsp,/test.jsp,/ rois/registerEntp.action</param-value>16</init-param>17</filter>18<!--过滤/rois命名空间下所有action -->19<filter-mapping>20<filter-name>SessionInvalidate</filter-name>21<url-pattern>/rois/*</url-pattern>22</filter-mapping>23<!--过滤/jsp文件夹下所有jsp -->24<filter-mapping>25<filter-name>SessionInvalidate</filter-name>26<url-pattern>/jsp/*</url-pattern>27</filter-mapping>SessionCheckFilter.java代码28package filter;29import java.io.IOException;30import java.util.HashSet;31import java.util.Set;32import javax.servlet.Filter;33import javax.servlet.FilterChain;34import javax.servlet.FilterConfig;35import javax.servlet.ServletException;36import javax.servlet.ServletRequest;37import javax.servlet.ServletResponse;38import javax.servlet.http.HttpServletRequest;39import javax.servlet.http.HttpServletResponse;40import javax.servlet.http.HttpSession;41/**42 * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面配置参数checkSessionKey 需检查的在 Session 中保存的关键字43 * redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPathnotCheckURLList44 * 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath45 */46public class SessionCheckFilter implements Filter {47protected FilterConfig filterConfig = null;48private String redirectURL = null;49private Set<String> notCheckURLList = new HashSet<String>();50private String sessionKey = null;51@Override52public void destroy() {53 notCheckURLList.clear();54 }55@Override56public void doFilter(ServletRequest servletRequest,57 ServletResponse servletResponse, FilterChain filterChain)58throws IOException, ServletException {59 HttpServletRequest request = (HttpServletRequest) servletRequest;60 HttpServletResponse response = (HttpServletResponse) servletResponse;61 HttpSession session = request.getSession();62if (sessionKey == null) {63 filterChain.doFilter(request, response);64return;65 }66if ((!checkRequestURIIntNotFilterList(request))67 && session.getAttribute(sessionKey) == null) {68 response.sendRedirect(request.getContextPath() + redirectURL);69return;70 }71 filterChain.doFilter(servletRequest, servletResponse);72 }73private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {74 String uri = request.getServletPath()75 + (request.getPathInfo() == null ? "" : request.getPathInfo());76 String temp = request.getRequestURI();77 temp = temp.substring(request.getContextPath().length() + 1);78// System.out.println("是否包括:"+uri+";"+notCheckURLList+"=="+notCheckURLList.contains(uri));79return notCheckURLList.contains(uri);80 }81@Override82public void init(FilterConfig filterConfig) throws ServletException {83this.filterConfig = filterConfig;84 redirectURL = filterConfig.getInitParameter("redirectURL");85 sessionKey = filterConfig.getInitParameter("checkSessionKey");86 String notCheckURLListStr = filterConfig87 .getInitParameter("notCheckURLList");88if (notCheckURLListStr != null) {89 System.out.println(notCheckURLListStr);90 String[] params = notCheckURLListStr.split(",");91for (int i = 0; i < params.length; i++) {92 notCheckURLList.add(params[i].trim());93 }94 }95 }96}权限验证的Interceptor实现:使用Interceptor不需要更改web.xml,只需要对struts.xml进行配置struts.xml片段97<!-- 用户拦截器定义在该元素下 -->98<interceptors>99<!-- 定义了一个名为authority的拦截器 -->100<interceptor name="authenticationInterceptor"class="interceptor.AuthInterceptor"/>101<interceptor-stack name="defualtSecurityStackWithAuthentication">102<interceptor-ref name="defaultStack"/>103<interceptor-ref name="authenticationInterceptor"/>104</interceptor-stack>105</interceptors>106<default-interceptor-ref name="defualtSecurityStackWithAuthentication"/> 107<!-- 全局Result -->108<global-results>109<result name="error">/error.jsp</result>110<result name="login">/Login.jsp</result>111</global-results>112<action name="login"class="action.LoginAction">113<param name="withoutAuthentication">true</param>114<result name="success">/WEB-INF/jsp/welcome.jsp</result>115<result name="input">/Login.jsp</result>116</action>117<action name="viewBook"class="action.ViewBookAction">118<result name="sucess">/WEB-INF/viewBook.jsp</result>119</action>AuthInterceptor.java代码120package interceptor;121import java.util.Map;122import com.opensymphony.xwork2.Action;123import com.opensymphony.xwork2.ActionContext;124import com.opensymphony.xwork2.ActionInvocation;125import com.opensymphony.xwork2.interceptor.AbstractInterceptor;126public class AuthInterceptor extends AbstractInterceptor {127private static final long serialVersionUID = -5114658085937727056L;128private String sessionKey="loginName";129private String parmKey="withoutAuthentication";130private boolean excluded;131@Override132public String intercept(ActionInvocation invocation) throws Exception { 133134 ActionContext ac=invocation.getInvocationContext();135 Map<?, ?> session =ac.getSession();136 String parm=(String) ac.getParameters().get(parmKey);137138if(parm!=null){139 excluded=parm.toUpperCase().equals("TRUE");140 }141142 String user=(String)session.get(sessionKey);143if(excluded || user!=null){144return invocation.invoke();145 }146 ac.put("tip", "您还没有登录!");147//直接返回 login 的逻辑视图148return Action.LOGIN;149 }150}使用自定义的default-interceptor的话有需要注意几点:1.一定要引用一下Sturts2自带defaultStack。

相关主题