Struct2_定义拦截器并使用注解方式作用在Action的方法中
一、目的:通过在方法上加注解控制哪些方法需要登陆后才能访问
- 定义配置文件
struts.xml添加节点1234567891011121314151617181920<
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
>
定义注解
注解主要是作用在方法上,拦截器根据方法是否定义注解来处理登陆判断
123456789101112131415package
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();
}
定义拦截器
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748package
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();
}
}
}
- 定义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;
}
}
|
内置的注解@InterceptorRefs/@InterceptorRef只能作用在类文件上,不能作用到方法上(从其@Target属性即可看出)
类上的注解@ParentPackage("custom-default")必须对应structs.xml中package的name属性,否则注解不起效
structs.xml中default-interceptor-ref的name值对应的是interceptor-stack的name值,否则注解不起效
- http://coolfire9201207034819.iteye.com/blog/1583802
- http://www.codes51.com/article/detail_120593.html
- http://www.cnblogs.com/linjiqin/archive/2013/06/21/3148129.html
- http://blog.csdn.net/z69183787/article/details/38440421
- http://blog.csdn.net/linchunhua/article/details/8494822
转载于:https://www.cnblogs.com/gossip/p/5195764.html
Struct2_定义拦截器并使用注解方式作用在Action的方法中相关推荐
- Struts2--自定义拦截器三种方式(实现Interceptor接口、继承抽象类AbstractInterceptor、继承MethodFilterInterceptor)
实现自定义拦截器 在实际的项目开发中,虽然 Struts2 的内建拦截器可以完成大部分的拦截任务,但是,一些与系统逻辑相关的通用功能(如权限的控制和用户登录控制等),则需要通过自定义拦截器实现.本节将 ...
- SpringBoot+拦截器+自定义异常+自定义注解+全局异常处理简单实现接口权限管理...
点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/weixin_44102992/article/ details/107335702 前言 提到权限管理这块肯定很多人第一想到的 ...
- struts2开发4--自定义拦截器把不文明用语改变成***
struts2拦截器是在访问某个Action或者Action的某个方法.字段之前或者之后实施拦截,并且struts2拦截器是可插拔的,拦截器是AOP的一种实现.这里重点介绍下自定义文字过滤拦截器,把我 ...
- 真正理解mybatis拦截器以及Interceptor和Plugin作用
看了很多博客文章和,mybatis 的拦截器概念还是不能很好理解, 可能是因为自己基础不好或者理解方式和他人不同吧,所以决定自己花时间好好捋捋, 然后把理解后的总结记录下来,供他人参考,也许你们的理解 ...
- Struts2的拦截器只允许有权限用户访问action
1.定义拦截器,继承MethodFilterInterceptor package com.life.stuts.interceptor;import java.util.Map;import com ...
- Struts2自己定义拦截器实例—登陆权限验证
版本号:struts2.1.6 此实例实现功能:用户须要指定username登陆,登陆成功进入对应页面运行操作,否则返回到登陆页面进行登陆,当直接訪问操作页面(登陆后才干訪问的页面)时则不同意,须返回 ...
- 自定义Flume拦截器,并将收集的日志存储到Kafka中(案例)
1.引入POM文件 如果想调用Flume,需要引入flume相关的jar包依赖,jar包依赖如下: <?xml version="1.0" encoding="UT ...
- spring拦截器传递传递参数到controller+Aop切面环绕方法传递参数到目标方法
一.前言 由于业务需要,在网上找了很久得到了这两种方式.本文代码偏少,主要讲解一下怎么传,如何接收,也记录一下,以便之后查看. 二.Spring拦截器传递参数到controller 下面是我拦截器中的 ...
- Spring Boot 拦截器无效,不起作用
这个问题一般是解决WebMvcConfigurerAdapter过时问题造成的.导致这个问题可能有两个原因:一个是拦截器写错了,另一个是拦截器配置错了. 1.需求是这样的 拦截所有的api请求,判断其 ...
最新文章
- windows7下安装MongoDB数据库
- MySQL五大约束详解(我有多详细只有我知道小白都能懂哦)
- Android的多任务之路
- CodeForces - 475B Strongly Connected City(最短路+判断强联通图/思维)
- JVM的内存配置参数
- SQL Server之索引
- 前端学习(489):文本格式化
- java test circle_TestCircle.java
- 【OpenCv】cvWaitKey获取键盘值
- css-font字体和文本样式
- (风中奇缘) Pocahontas
- fzu2181(点的双连通分量+求奇环)
- 领导问“BUG解决了没有”,吾一听就知道完蛋了
- mybatis自增主键回填
- c语言筛选法求素数,筛选法求素数
- 服务器的固态硬盘使用raid非ssd,畅谈固态硬盘搭建RAID的方法
- 怎么查看电脑最大支持扩展内存
- 微信开发服务器端口号,微信公众号开发步骤
- 阿里云服务器使用xshell连接
- BOSS招聘网站数据分析岗位分析详情