Struts2使用过滤器和拦截器进行简单权限校验

在一些用到用户登录的目录应该进行权限判断,以防非法登录,我在这里进行一个简单的权限校验。

1.使用过滤器进行/admin 目录下jsp页面的过滤,首先在web.xml进行过滤器配置:

  1.     <filter>
  2.         <filter-name>access filter</filter-name>
  3.         <filter-class>
  4.              com.test.news.util.AccessFilter
  5.         </filter-class>
  6.     </filter>
  7.     <filter-mapping>
  8.         <filter-name>access filter</filter-name>
  9.         <url-pattern>/admin/*</url-pattern>
  10.     </filter-mapping>

下面是过滤的实现类:

  1. package com.test.news.util;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. public class AccessFilter implements Filter {
  13. /**
  14. * @author chaoyin
  15. */
  16.     
  17.     public void destroy() {
  18.      }
  19.     public void doFilter(ServletRequest arg0, ServletResponse arg1,
  20.              FilterChain filterChain) throws IOException, ServletException {
  21.          HttpServletRequest request = (HttpServletRequest)arg0;
  22.          HttpServletResponse response = (HttpServletResponse)arg1;
  23.          HttpSession session = request.getSession();
  24.         if(session.getAttribute("user")== null && request.getRequestURI().indexOf("login.jsp")==-1 ){
  25.              response.sendRedirect("login.jsp");
  26.             return ;
  27.          }
  28.          filterChain.doFilter(arg0, arg1);
  29.      }
  30.     public void init(FilterConfig arg0) throws ServletException {
  31.      }
  32. }


这样/admin 目录下需经过login.jsp这个页面才能正常访问。

2.使用Struts2的拦截器功能进行.action的过滤,主要是校验那些与后台管理相关的action,首先在struts.xml中进行拦截器的配置:

  1. <package name="newsDemo" extends="struts-default"
  2.         namespace="/admin">
  3.         <interceptors>
  4.             <interceptor name="auth" class="com.test.news.util.AccessInterceptor" />
  5.             <interceptor-stack name="authStack">
  6.                 <interceptor-ref name="auth" />
  7.             </interceptor-stack>
  8.         </interceptors>
  9.         <!-- action -->
  10.         <action name="newsAdminView!*" class="newsAction"
  11.             method="{1}">
  12.             <interceptor-ref name="defaultStack"/>
  13.             <interceptor-ref name="authStack">
  14.             </interceptor-ref>

下面是我实现的Interceptor class:

  1. package com.test.news.util;
  2. import java.util.Map;
  3. import com.opensymphony.xwork2.ActionContext;
  4. import com.opensymphony.xwork2.ActionInvocation;
  5. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  6. import com.test.news.action.AdminLoginAction;
  7. /**
  8. * @author chaoyin
  9. */
  10. public class AccessInterceptor extends AbstractInterceptor {
  11.     private static final long serialVersionUID = -4291195782860785705L;
  12.     @Override
  13.     public String intercept(ActionInvocation actionInvocation) throws Exception {
  14.          ActionContext actionContext = actionInvocation.getInvocationContext();
  15.          Map session = actionContext.getSession();
  16.         
  17.         //except login action
  18.          Object action = actionInvocation.getAction();
  19.         if (action instanceof AdminLoginAction) {
  20.             return actionInvocation.invoke();
  21.          }
  22.         //check session
  23.         if(session.get("user")==null ){
  24.             return "logout";
  25.          }
  26.         return actionInvocation.invoke();//go on
  27.      }
  28. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值