一、目的:通过在方法上加注解控制哪些方法需要登陆后才能访问

 
二、方式:利用拦截器判断用户是否登陆
 
三、实现步骤
  1. 定义配置文件
    struts.xml添加节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <package name="custom-default" extends="struts-default">
            <interceptors>
                <!-- 声明自定义的权限控制拦截器 -->
                <interceptor name="loginInterceptor" class="interceptors.LoginInterceptor" />
                <!-- 把自定义的权限控制拦截器和默认的拦截器栈加到新的自定义的拦截器栈 -->
                <interceptor-stack name="myInterceptors">
                    <interceptor-ref name="loginInterceptor" />
                    <interceptor-ref name="defaultStack" />
                </interceptor-stack>
            </interceptors>
     
            <!-- 指定新的自定义的拦截器栈为默认的拦截器栈,这样自定义的权限控制拦截器就可以发挥作用了 -->
            <!-- 这里name属性值对应的是上述interceptor-stack name属性值 -->
            <default-interceptor-ref name="myInterceptors"></default-interceptor-ref>
             
            <!-- 这里对应拦截器中return Action.LOGIN对应的页面-->
            <global-results>
                <result name="login">/WEB-INF/content/LoginInfo/login.jsp</result>
            </global-results>
        </package>
  2. 定义注解

    注解主要是作用在方法上,拦截器根据方法是否定义注解来处理登陆判断

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package annotations;
     
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
     
    @Target({ElementType.METHOD})
    @Retention(value = RetentionPolicy.RUNTIME)
    public @interface Authority {
         /**
         * @return  The namespace value.
         */
        String value();
    }
  3. 定义拦截器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    package interceptors;
     
    import java.lang.reflect.Method;
    import java.util.Map;
     
    import org.apache.struts2.convention.annotation.InterceptorRef;
     
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionInvocation;
    import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
     
    import annotations.Authority;
    import common.Constants;
     
    @SuppressWarnings("serial")
    public class LoginInterceptor extends AbstractInterceptor {
     
        @Override
        public String intercept(ActionInvocation invocation) throws Exception {
            String methodName = invocation.getProxy().getMethod();
            Class clazz = invocation.getAction().getClass(); // 获取类对象
            Method currentMethod = clazz.getMethod(methodName); //获取拦截的方法
             
            //方法上添加了注解
            if (currentMethod.isAnnotationPresent(Authority.class)) {
                // 取得当前请求的注解的action
                ActionContext context = invocation.getInvocationContext();
                Map session = context.getSession();
                //Constants.UserName=="UserName"
                String user = (String) session.get(Constants.UserName);
     
                System.err.println("拦截器起作用");
                if (user == null) // 未登陆,跳转到登录页
                {
                    System.err.println("进入拦截器:未登陆");
                    context.put("tip", "你还没有登录");
                    return Action.LOGIN;
                } else {   //已登录,继续后续流程
                    System.err.println("进入拦截器:已登录");
                    return invocation.invoke();
                }
            } else {
                System.err.println("进入拦截器:没有使用注解");
                return invocation.invoke();
            }
        }
    }
  4. 定义Action类及方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package action;
 
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
 
import annotations.Authority;
import common.Constants;
 
import org.apache.struts2.convention.annotation.InterceptorRefs;
 
@SuppressWarnings("serial")
@ParentPackage("custom-default")   //这里对应的配置文件中包的名称
public class LoginAction extends SuperActionSupport {
 
    @Action(value = "loginValidate", results = {
            @Result(name = "success", location = "/WEB-INF/content/LoginInfo/success.jsp"),
            @Result(name = "input", location = "/WEB-INF/content/LoginInfo/login.jsp") })
    @Authority(""//定义注解后未登录回到登陆页,否则执行后续步骤
    public String loginValidate() throws Exception {
        return SUCCESS;
    }
}
 
四、实现过程中遇到的坑
  1. 内置的注解@InterceptorRefs/@InterceptorRef只能作用在类文件上,不能作用到方法上(从其@Target属性即可看出)

  2. 类上的注解@ParentPackage("custom-default")必须对应structs.xml中package的name属性,否则注解不起效

  3. structs.xml中default-interceptor-ref的name值对应的是interceptor-stack的name值,否则注解不起效

 

五、参考资料

Struct2_定义拦截器并使用注解方式作用在Action的方法中的更多相关文章

  1. springmvc自己定义拦截器

    Spring MVC也能够使用拦截器对请求进行拦截处理,用户能够自己定义拦截器来实现特定的功能,自己定义的拦截器必须实现HandlerInterceptor接口. 直接看下样例: package co ...

  2. struts自己定义拦截器--登录权限控制

    说明:该自己定义的拦截器实现用户登录的权限控制. login.jsp--->LoginAction--重定向-->MainAction--->main.jsp 一.1.整体的步骤: ...

  3. struts2学习笔记(5)---自己定义拦截器

    什么是拦截器? struts2中拦截器分为Struts2定义好的拦截器和自己定义的拦截器. 其作用是在一个Action运行之前进行拦截,在Action运行之后又增加某些操作. 实现原理 当请求一个Ac ...

  4. Struts2通过自己定义拦截器实现登录之后跳转到原页面

    这个功能对用户体验来说是非常重要的.实现起来事实上非常easy. 拦截器的代码例如以下: package go.derek.advice; import go.derek.entity.User; i ...

  5. jquery.ajax与axios及定义拦截器

    首先导入jquery和axios包 jquery.ajax function reg(){ var username = $("#username").val(); var pas ...

  6. Struts2_day04--自定义拦截器_Struts2的标签库_表单标签

    自定义拦截器 1 在struts2里面有很多的拦截器,这些拦截器是struts2封装的功能,但是在实际开发中,struts2里面的拦截器中可能没有要使用的功能,这个时候需要自己写拦截器实现功能 2 拦 ...

  7. CXF实战之自己定义拦截器(五)

    CXF已经内置了一些拦截器,这些拦截器大部分默认加入到拦截器链中,有些拦截器也能够手动加入,如手动加入CXF提供的日志拦截器.也能够自己定义拦截器.CXF中实现自己定义拦截器非常easy.仅仅要继承A ...

  8. struts2拦截器加自定义注解实现权限控制

    https://blog.csdn.net/paul342/article/details/51436565 今天结合Java的Annotation和Struts2进行注解拦截器权限控制. 功能需求: ...

  9. Dora.Interception,为.NET Core度身打造的AOP框架 [2]:以约定的方式定义拦截器

    上一篇<更加简练的编程体验>提供了最新版本的Dora.Interception代码的AOP编程体验,接下来我们会这AOP框架的编程模式进行详细介绍,本篇文章着重关注的是拦截器的定义.采用“ ...

随机推荐

  1. Atom:优雅迷人的编辑神器

    对于热爱markdown写作的人来说,Atom同样是一款拥有无穷魅力的写作软件.我不怕它无法满足你的需求,就怕你不给一个机会了解它,那么,这将是一场遗憾的错过. 大学的时候,坊间对那些编程高手有一个令 ...

  2. Oracle简述

    Oracle是甲骨文公司推出的一款大型数据库管理系统.甲骨文公司成立于1977年,总部位于美国加利福尼亚州的红木滩.1989年,Oracle正式进入中国市场:2013年,甲骨文超越 IBM ,成为继 ...

  3. Android各层推荐开发书籍及参考资料!!!

    Android各层推荐开发书籍及参考资料 转自:http://blog.csdn.net/fancylovejava/article/details/8657058 Android系统按照架构来说一共 ...

  4. LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence

    1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...

  5. Android与html5交互 -- WebView使用(一)

    Android中使用WebView可加载html5,具体步骤如下: (前提:本地Html5存放到assets文件夹下) 一:使用WebView加载Html5,简单显示 1:清单文件中添加访问权限:an ...

  6. AC日记——[Scoi2010]序列操作 bzoj 1858

    1858 思路: 恶心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct Tree ...

  7. [水煮 ASP.NET Web API2 方法论](1-3)如何接收 HTML 表单请求

    问题 我们想创建一个能够处理 HTML表单的 ASP.NET Web API 应用程序(使用 application/x-www-form-urlencoded 方式提交数据). 解决方案 我们可以创 ...

  8. 前端读者 | 百度前端编码规范(JS)

    本文来自:百度FEX 1 前言 JavaScript在百度一直有着广泛的应用,特别是在浏览器端的行为管理.本文档的目标是使JavaScript代码风格保持一致,容易被理解和被维护. 虽然本文档是针对J ...

  9. 配置nginx作为下载站点

    nginx默认情况是不允许列出整个目录浏览下载 1)autoindex参数详解 autoindex on //on开启目录浏览 autoindex_exact_size off; //off显示出文件 ...

  10. 洛谷——P2388 阶乘之乘

    P2388 阶乘之乘 题目背景 不告诉你…… 题目描述 求出1!*2!*3!*4!*……*n!的末尾有几个零 输入输出格式 输入格式: n(n<=10^8) 输出格式: 有几个零 输入输出样例 ...