Spring MVC 方法注解拦截器
应用场景,在方法级别对本次调用进行鉴权,如api接口中有个用户唯一标示accessToken,对于有accessToken的每次请求可以在方法加一个拦截器,获得本次请求的用户,存放到request或者session域。
python中,之前在python flask中可以使用装饰器来对方法进行预处理,进行权限处理
先看一个实例,使用@access_required拦截:
|
1
2
3
4
5
6
7
8
|
@api.route('/post_apply')@access_requireddef apply(): """ 活动报名 """ print '报名者是:'+g.user return jsonify(response_data) |
实现很简单:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# 验证access_token并保存当前用户到g中def access_required(f): @wraps(f) def decorated_func(*args, **kwargs): access_token = request.values.get('access_token') if access_token == None: return error_msg('500', 'access_token required') if access_token == "": return error_msg('500', 'access_token can not empty') if is_access_token(access_token) == False: return error_msg('500', 'invalid_access_token') return f(*args, **kwargs) return decorated_func |
java中,自定义注解拦截器来实现,在需要的拦截的方法上面加上一个注解@AccessRequired
spring mvc Controller中的使用实例
|
1
2
3
4
5
6
7
8
9
|
/** * 注解拦截器方法 * @return */ @RequestMapping(value="/urlinter",method=RequestMethod.GET) @AccessRequired public @ResponseBody String urlInterceptorTest() { return "通过拦截器:user"+request.getAttribute("currUser"); } |
如何实现以上实例呢?
定义一个注解:
|
1
2
3
4
5
6
7
8
9
10
|
import java.lang.annotation.ElementType;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import java.lang.annotation.Retention;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface AccessRequired { } |
搞一个拦截器:
|
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
|
/** * 拦截url中的access_token * @author Nob * */public class UserAccessApiInterceptor extends HandlerInterceptorAdapter { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); AccessRequired annotation = method.getAnnotation(AccessRequired.class); if (annotation != null) { System.out.println("你遇到了:@AccessRequired"); String accessToken = request.getParameter("access_token"); /** * Do something */ response.getWriter().write("没有通过拦截,accessToken的值为:" + accessToken); } // 没有注解通过拦截 return true; }} |
在spring mvc配置文件中:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- 拦截器 --> <mvc:interceptors> <mvc:interceptor> <!-- 对所有的请求拦截使用/** ,对某个模块下的请求拦截使用:/myPath/* --> <mvc:mapping path="/**" /> <ref bean="userAccessInterceptor" /> </mvc:interceptor> </mvc:interceptors> <bean id="userAccessInterceptor" class="com.banmacoffee.web.interceptor.UserAccessApiInterceptor"> </bean> |
大功告成,你可以在拦截器里为所欲为,并且把它加载任何你想的Controller 请求的方法上。
Spring MVC 方法注解拦截器的更多相关文章
- Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用
一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...
- spring mvc中的拦截器小结 .
在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下. preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Control ...
- 1.5(Spring MVC学习笔记) 拦截器(Interceptor)
一.拦截器 1.1拦截器简介 Spring中的拦截器类似Servlet中的过滤器(Filter),主要用于拦截用户请求, 并进行一定的处理操作(如验证权限.记录日志.设置编码方式等). 1.2拦截器实 ...
- Spring Mvc 的自定义拦截器
spring mvc的拦截器 SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户 ...
- 关于springmvc 方法注解拦截器的解决方案,多用于方法的鉴权
最近在用SpringMvc写项目的时候,遇到一个问题,就是方法的鉴权问题,这个问题弄了一天了终于解决了,下面看下解决方法 项目需求:需要鉴权的地方,我只需要打个标签即可,比如只有用户登录才可以进行的操 ...
- Spring MVC中自定义拦截器的简单示例
1. 引言 拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter. 我们可以让普通的Bean实现HandlerIntercpetor接口或继承 ...
- Spring MVC中的拦截器Interceptor
谈谈spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先设置数据以及统计方法的执行效率等等.今天就来详细的谈一下spring中的拦截器.spring中拦截器主 ...
- Spring mvc interceptor配置拦截器,没有登录跳到登录页
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- [转]Spring mvc interceptor配置拦截器,没有登录跳到登录页
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.s ...
随机推荐
- golang: multiple http.writeHeader calls
背景: golang的http服务,读取文件,提供给client下载时候. 出现 multiple http.writeHeader calls 错误. func DownloadFile(w htt ...
- NoSQL 数据库概览及其与 SQL 语法的比较
NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用的难题. 本文对NoSQL数据库的定义.分类.特征.当前比较流行的NoSQL数据库系统等进行了简单的介绍,并对N ...
- 1.新建项目出现包名有一道红线The SDK platform-tools version ((23)) is too old to check APIs compiled with API 20
原因分析: 就是platform-tools的版本太低导致的 解决方法: 1.点开SDK Manager,打开SDK Tools面板,将Platform-tools更新 2.更新完之后重启as即可
- Oracle记录登录失败的触发器
前言:实现的功能主要是,oracle登录成功记录登录用户ip地址,登录失败记录登录失败ip地址 1,需要建立一个触发器记录登录成功的客户端用户的ip地址 大家都知道在v$session 中记录着客户端 ...
- Code signing is required for product type 'Application' in SDK 'iOS 11.2'
在打包的时候出现这样一个错误,Code signing is required for product type 'Application' in SDK 'iOS 11.2' ,就是说代码签名证书 ...
- lua在线手册汇总
1. Lua官方参考手册 Lua 4.0 : http://www.lua.org/manual/4.0/Lua 5.0 : http://www.lua.org/manual/5.0/Lua 5.1 ...
- python全栈开发从入门到放弃之装饰器函数
什么是装饰器#1 开放封闭原则:对扩展是开放的,对修改是封闭的#2 装饰器本身可以是任意可调用对象,被装饰的对象也可以是任意可调用对象#3 目的:''' 在遵循 1. 不修改被装饰对象的源代码 2. ...
- SqoopFlume、Flume、HDFS之间比较
Sqoop Flume HDFS Sqoop用于从结构化数据源,例如,RDBMS导入数据 Flume 用于移动批量流数据到HDFS HDFS使用 Hadoop 生态系统存储数据的分布式文件系统 Sqo ...
- 2016-2017 National Taiwan University World Final Team Selection Contest J - Zero Game
题目: You are given one string S consisting of only '0' and '1'. You are bored, so you start to play w ...
- Salesforce中通过SOAP API和Metadata API开发java的web server服务
1.下载Salesforce平台中WSDL文件 在Salesforce中创建了自己需要用到的对象后,我们想要在别的应用中读写纪录到对象中,首先需要的是自己Salesforce平台的权限通过.登陆自己的 ...