1:java注解使用是相当频繁,特别是在搭建一些框架时,用到类的反射获取方法和属性,用的尤其多。

java中元注解有四个: @Retention     @Target     @Document   @Inherited;

        @Retention:注解的保留位置         
    @Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
     @Retention(RetentionPolicy.CLASS) //默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
     @Retention(RetentionPolicy.RUNTIME) //注解会在class字节码文件中存在,在运行时可以通过反射获取到
  
   @Target:注解的作用目标
        
        @Target(ElementType.TYPE) //接口、类、枚举、注解
        @Target(ElementType.FIELD) //字段、枚举的常量
        @Target(ElementType.METHOD) //方法
        @Target(ElementType.PARAMETER) //方法参数
        @Target(ElementType.CONSTRUCTOR) //构造函数
        @Target(ElementType.LOCAL_VARIABLE)//局部变量
        @Target(ElementType.ANNOTATION_TYPE)//注解
        @Target(ElementType.PACKAGE) ///包 @Document:说明该注解将被包含在javadoc中   @Inherited:说明子类可以继承父类中的该注解

创建自定义注解类:

 package com.liveyc.eloan.util;

 import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* 要求登录标签
* @author Administrator
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequireLogin { }

2:编写拦截器 java中拦截是向下传递的 所以要return false不要继续向下传递了

 package com.liveyc.eloan.base.interceptor;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.liveyc.eloan.util.RequireLogin;
import com.liveyc.eloan.util.UserContext; /**
* 登录拦截器
*
* @author Administrator
*
*/
public class LoginInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// 处理handler;
if (handler instanceof HandlerMethod) {
// 判断当前method上是否有标签;
HandlerMethod hm = (HandlerMethod) handler;
if (hm.getMethodAnnotation(RequireLogin.class) != null
&& UserContext.getCurrent() == null) {
// r如果有,判断当前是否用户登录,如果没有登录,跳转到登录页面
response.sendRedirect("/login.html");
return false;
}
}
return super.preHandle(request, response, handler);
} }

3:在 spring的 xml配置文件中添加

     <mvc:interceptors>
<!-- 配置登录拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.liveyc.eloan.base.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

4:在springmvc的 controller层 需要登入以后才能访问的方法中添加自定义注解  @RequireLogin

 @RequireLogin
@RequestMapping("personal")
public String personal(Model model) {
Logininfo current = UserContext.getCurrent();
model.addAttribute("userinfo",this.userinfoService.get(current.getId()));
model.addAttribute("account", this.accountService.get(current.getId()));
return "personal";
}

5:启动项目 开始测试

因为没登入所以直接跳转到登录页面了

访问一个没加自定义注解的方法 页面报错

登录以后 页面正常访问

在springMVC中使用自定义注解来进行登录拦截控制的更多相关文章

  1. 【Spring】5、利用自定义注解在SpringMVC中实现自定义权限检查

    转自:http://www.tuicool.com/articles/6z2uIvU 先描述一下应用场景,基于Spring MVC的WEB程序,需要对每个Action进行权限判断,当前用户有权限则允许 ...

  2. JavaEE开发之SpringMVC中的自定义拦截器及异常处理

    上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...

  3. springmvc中如何自定义类型转换器

    package com.hope.utils;import org.springframework.core.convert.converter.Converter;import org.spring ...

  4. 如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  5. 关于springmvc中常用的注解,自己也整理一下

    1.@Controller 在springMVC中@controller主要用在控制层的类上,之前只知道用注解开发的时候必须加一个@controller ,今天看了别的大佬整理的才知道为什么这么用,控 ...

  6. SpringMVC中的常用注解

    RequestParam 作用: 用于  将请求参数区数据  映射到  功能处理方法的参数上. 属性: value  请求参数中的名称 required   请求参数中是否必须提供此参数. 默认值: ...

  7. Java中的自定义注解

    ## 元注解 要声明一个注解, 我们需要元注解, 元注解是指注解的注解,包括@Retention, @Target, @Document, @Inherited. @Retention 注解的保留位置 ...

  8. AOP中获取自定义注解的参数值

      目录 一.利用注解实现AOP的基本流程 1.1.创建一个注解,用来注解切点(pointcut) 1.2.创建一个service,使用上面定义的注解来指定切点 1.3.创建Aspect,增加业务逻辑 ...

  9. springMvc中获取通过注解获取properties配置文件(转)

    springMvc的项目中,通过注解@Value获取properties配置文件中的配置,使用该注解必须引入的包: spring-beans-4.1.4.RELEASE.jar 下面是需要在sprin ...

随机推荐

  1. cmd 中运行testng代码

    说明:classpath是jvm执行class时所加载的路径:--个人理解,如有不同:QQ:316567803 1.先下载插件 https://plugins.jetbrains.com/plugin ...

  2. xshell 常用快捷键

    1.连接mysql数据库mysql -uroot -p -h127.0.0.1 -P3306 2.列出所有sessionshow full processlist; 3.查看20条执行时间最长的SQL ...

  3. [转帖]overlay文件系统解析

    overlay文件系统解析 来源:http://dockone.io/article/1511 原作者: 陈爱珍 布道师@七牛云 一个 overlay 文件系统包含两个文件系统,一个 upper 文件 ...

  4. [转帖] SQLNET.ORA的处理.

    被一个客户端连接远程数据库阻塞超时的问题困扰了好久,最后终于找到了答案  https://blog.csdn.net/herobox/article/details/16985097   Oracle ...

  5. 微信小程序 功能函数 替换字符串内的指定字符

    var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1);    // 打印结果: obcadeacf   var st ...

  6. swusec的构想,顺便送开学福利——校园网一号多登录演示

    前言: 我不是什么大牛,我只想通过我的努力,打造swu网络安全爱好者的圈子.期待你加入. swusec是什么? swusec (SouthWestUniversity SecurityTeam),西南 ...

  7. 【Java线程】SwingWorker的用法

    Swing应用程序员常见的错误是误用Swing事件调度线程(Event DispatchThread,EDT).他们要么从非UI线程访问UI组件:要么不考虑事件执行顺序:要么不使用独立任务线程而在ED ...

  8. 如何处理UIVIew addsubview 不显示subview

    老代码: addsubview不显示uilabel -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSIn ...

  9. python print输出到文件

    要将程序的输出送到一个文件中,需要在 print 语句后面使用 >> 指定一个文件,如下所示: principal = # 初始金额 rate = 0.05 # 利率 numyears = ...

  10. 【译】关于vertical-align你应知道的一切

    原文地址:Vertical-Align: All You Need To Know 通常我们需要垂直对齐并排的元素. CSS提供了一些可实现的方法:有时我用浮动float来解决,有时用position ...