用户验证登录拦截器

  1. public class UserInterceptor extends AbstractInterceptor {
  2. @Override
  3. public String intercept(ActionInvocation invocation) throws Exception {
  4. // 得到当前执行的方法
  5. String methodName = invocation.getProxy().getMethod();
  6. // 得到ActionContext对象
  7. ActionContext ac = invocation.getInvocationContext();
  8. // 获取session, 从session中获取登陆的管理员账号
  9. Object obj = ac.getSession().get("adminInfo");
  10. // 判断:
  11. if (!"login".equals(methodName) && !"list".equals(methodName)){
  12. // 验证
  13. if (obj == null){
  14. // 没有登陆
  15. return "login";
  16. } else {
  17. // 执行Action
  18. return invocation.invoke();
  19. }
  20. } else {
  21. // 允许访问登陆、列表展示
  22. return invocation.invoke();
  23. }
  24. }
  25. }
  1. <!-- 拦截器配置 -->
  2. <interceptors>
  3. <interceptor name="userInterceptor" class="cn.itcast.action.UserInterceptor"></interceptor>
  4. <interceptor-stack name="myStack">
  5. <interceptor-ref name="defaultStack"></interceptor-ref>
  6. <interceptor-ref name="userInterceptor"></interceptor-ref>
  7. </interceptor-stack>
  8. </interceptors>
  9. <!-- 执行指定的拦截器 -->
  10. <default-interceptor-ref name="myStack"></default-interceptor-ref>





posted @ 2016-09-21 16:04  无丑不成戏如人生  阅读(330)  评论(0编辑  收藏  举报