1.首先先定义一个拦截器注解

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginRequired { }

2.在定义一个拦截器

/**
* 登录拦截器
*/
public class LoginInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
LoginRequired loginRequired = findAnnotation((HandlerMethod) handler, LoginRequired.class);
//没有声明需要权限,或者声明不验证权限
if(loginRequired==null){
return true;
}else{
String token=request.getHeader("token");
if(StringUtils.isEmpty(token)){
token=request.getParameter("token");
}
//在这里实现自己的权限验证逻辑
if(!StringUtils.isEmpty(token)){//如果验证成功返回true(这里直接写false来模拟验证失败的处理)
return true;
}else{//如果验证失败
response.getWriter().write("您还未登录");
return false;
}
}
}else{
return true;
}
} private <T extends Annotation> T findAnnotation(HandlerMethod handler, Class<T> annotationType) {
T annotation = handler.getBeanType().getAnnotation(annotationType);
if (annotation != null) return annotation;
return handler.getMethodAnnotation(annotationType);
}
}

3.拦截器spring配置文件

<!-- spring 3.1版本后才支持拦截方法名,需要引入一下配置 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<mvc:annotation-driven />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.xxx.xxxx.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

4.Controller层数直接使用方法

@ResponseBody
@RequestMapping(value="",method=RequestMethod.GET)
@LoginRequired
protected Map<String,Object> index(){
return null;
}

文章来源于:https://www.cnblogs.com/binz/p/6564606.html

springMvc基于注解登录拦截器的更多相关文章

  1. 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s

     1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2  spring-mv ...

  2. SpringMVC中的Interceptor拦截器及与Filter区别

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  3. 用户登录拦截器查询到登录用户后如何将用户信息传递到后面的Controller

    taotao创建订单代码中之前忘了加入用户信息,那么加上呢? 分析:用户创建订单的时候,我们会强制要求用户先登录,也就是说,创建订单的Controller执行时,一定是用户已经登录了的,而用户只要登录 ...

  4. SpringMVC之七:SpringMVC中使用Interceptor拦截器

    SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...

  5. springmvc基于注解的权限控制

    一.权限码 /** * @Title: AuthCode.java * @Package cn.com.kamfu.auth * @Description: TODO(用一句话描述该文件做什么) * ...

  6. spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,guava限流,定时任务案例, 发邮件

    本文介绍spring boot集成swagger,自定义注解,拦截器,xss过滤,异步调用,定时任务案例 集成swagger--对于做前后端分离的项目,后端只需要提供接口访问,swagger提供了接口 ...

  7. java框架之SpringMVC(2)-补充及拦截器

    高级参数绑定 package com.zze.springmvc.web.controller; import org.springframework.stereotype.Controller; i ...

  8. 大型运输行业实战_day05_1_登录+注销+表单重复提交+登录拦截器

    1.登录 登录实现如下步骤: 1.在首页中添加登录按钮 html代码如下: <%@ page contentType="text/html;charset=UTF-8" la ...

  9. SpringMVC学习记录(七)--拦截器的使用

    SpringMVC的请求如以下这样的图所看到的: 能够看出全部的请求都要通过Dispatherservlet来接收,然后通过Handlermapping来决定使用哪个控制器,再依据ViewResolv ...

随机推荐

  1. Spring+Velocity(平台升级至Spring Framework 5.0.2)

    下载: http://repo.spring.io/release/org/springframework/spring/ Dear Spring community, I’m pleased to ...

  2. 无耻之徒(美版)第七季/全集Shameless US迅雷下载

    英文全名Shameless (US),第7季(2016).本季看点:<无耻之徒>(Shameless)第七季.本季故事起始于「一个月之后」,Frank从昏迷中醒来后得知亲人背叛了他,于是向 ...

  3. Redis 性能问题的记录

    最近线上使用redis, 查询的情况不甚理想, 这个查询操作是个 lua 脚本, 包含如下操作 开发机 redis, 没有其他干扰, 插入的 zset 有 5000 member 左右, 使用的 re ...

  4. idea 2018.1破解激活方法,有效期至2099年 idea 激活 破解

    最近笔者测试了好多破解Idea的方法,最简单操作方法莫过于用license server激活,但是此类方法对最新的2017.3.2版已经无效了,亲测哦,如下图所示.  针对新版的IntelliJ ID ...

  5. Linq to Sql并发冲突及处理策略

    0. 并发冲突的示例 单用户的系统现在应该比较罕见了,一般系统都会有很多用户在同时进行操作:在多用户系统中,涉及到的一个普遍问题:当多个用户“同时”更新(修改或者删除)同一条记录时,该如何更新呢?   ...

  6. CentOS7配置Mysql热备份

    Mysql 的安装: 本人习惯讲安装包放在 /usr/local/src/ ①.[root@localhost ~]#cd /usr/local/src/ ②.[root@localhostsrc]# ...

  7. 操作系统重点双语阅读 - 上下文切换 Context Switch

    The context is represented in the PCB of the process. It includes the value of the CPU registers, th ...

  8. NLP资源

    http://www.cs.columbia.edu/~mcollins/notes-spring2013.html http://web.stanford.edu/class/cs224n/syll ...

  9. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十五)Structured Streaming:同一个topic中包含一组数据的多个部分,按照key它们拼接为一条记录(以及遇到的问题)。

    需求: 目前kafka的topic上有一批数据,这些数据被分配到9个不同的partition中(就是发布时key:{m1,m2,m3,m4...m9},value:{records items}),m ...

  10. VS2013开发asmx接口根据ID查询对象

    代码如下 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syst ...