自定义Login注解
自定义Login注解,实现在controller增加注解登录拦截
1.自定义Login注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Login {String name() default "";
}
2.实现LoginInterceptor拦截类
import com.sankuai.footstep.common.annotation.Login;
import com.sankuai.footstep.common.constant.Constants;
import com.sankuai.footstep.common.enums.ErrorCode;
import com.sankuai.footstep.common.exception.BusinessException;
import com.sankuai.footstep.common.facade.UserServiceFacade;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;@Component
public class LoginInterceptor implements HandlerInterceptor {public static final String USER_KEY = "mtUserId";public static final String TOKEN = "token";private static final List<String> loginKey = new ArrayList<>();{loginKey.add(Constants.COOKIE_TOKEN_2);loginKey.add(Constants.COOKIE_LT);loginKey.add(Constants.COOKIE_MT_C_TOKEN);}@Resourceprivate UserServiceFacade userServiceFacade;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//判断 如果handler不是方法类型 直接通过if (!(handler instanceof HandlerMethod)) {return true;}如果方法没加注解 则直接通过Login annotation = ((HandlerMethod) handler).getMethodAnnotation(Login.class);if (annotation == null) {return true;}String token = null;if (request.getCookies() == null || request.getCookies().length == 0) {throw new BusinessException(ErrorCode.NOT_LOGIN);}for (Cookie cookie : request.getCookies()) {/*** 与前端约定使用token,其他作为补充*/if(Constants.COOKIE_TOKEN.equals(cookie.getName())) {token = cookie.getValue();break;} else if (loginKey.contains(cookie.getName())) {token = cookie.getValue();}}if (null == token) {throw new BusinessException(ErrorCode.NOT_LOGIN);}Long userId = userServiceFacade.getUserIdByToken(token);request.setAttribute(USER_KEY, userId);request.setAttribute(TOKEN, token);return true;}
}
3.编写MvcConfig类
import com.zsl.webdemo.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import javax.annotation.Resource;@Configuration
public class MvcConfig implements WebMvcConfigurer {@Resourceprivate LoginInterceptor loginInterceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(loginInterceptor).addPathPatterns("/**");}
}
4.controller测试
package com.zsl.webdemo.controller;import com.zsl.webdemo.annotation.Login;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@RequestMapping("/get/{name}")public String name(@PathVariable("name")String name){return "hi = "+name;}@GetMapping("/hello")public String hello(){return "hi = ";}@Login@GetMapping("/inter")public String inter(){return "inter";}
}
当未在对应方法上加@Login注解时,自然放行;
若加了注解则检测请求中携带的cookie是否存在匹配,则放行。
自定义Login注解相关推荐
- slf4j注解log报错_SpringBoot自定义日志注解,用于数据库记录操作日志,你用过吗?...
大家好,我是程序员7歌! 今天我将为大家讲解如何通过自定义注解记录接口访问日志.一般的开发中,有两种方式可以记录日志信息,第一种:把接口日志信息保存到日志文件中,第二种:把接口操作日志保存到数据库中, ...
- spring boot之自定义的注解的切面编程
springBoot目录 1. 前言 在上一章中我们知道了,注解的本质其实就是一个标签.但是我们可以通过反射或其他方式从注解的宿主对象上获取到注解对象信息,这就让注解的可玩性变得很强.可以通过注解来描 ...
- SpringBoot conditional注解和自定义conditional注解使用
conditional注解是Springboot starter的基石,自动装配的时候会根据条件确定是否需要注入这个类. 含义:基于条件的注解. 作用:根据是否满足某个特定条件来决定是否创建某个特定的 ...
- 自定义Android注解Part3:绑定
上一节我们已经将自动生成注解代码部分介绍完毕,今天这篇文章是自定义Android注解系列的最后一篇文章.希望大家这一路走来有所收获. 经过前面的了解,我们三大部分:butterknife-annota ...
- 自定义Android注解Part2:代码自动生成
上一期我们已经把butterknife-annotations中的注解变量都已经定义好了,分别为BindView.OnClick与Keep. 如果你是第一次进入本系列文章,强烈推荐跳到文章末尾查看上篇 ...
- Spring Boot自定义缓存注解
自定义缓存注解 使用:
- 自定义Java注解(一)
概念 注解是Java语言5.0版本开始支持加入源代码的特殊语法元数据(描述数据的数据)有点像Class(描述类的类) 要自定义注解,必须先了解Java提供的几个基本的元注解及其相关的语法 Java的几 ...
- 自定义的注解校验器的实现
首先先学习一下注解,注解为我们在代码中添加信息提供了一种形式化的方法,使得我们在稍后的某个时刻可以方便地使用这些数据. 在日常的编码中我们一直都在使用注解,只是没有特别关注过,Java中内置了三种注解 ...
- jsr-303 参数校验—自定义校验注解
1.为什么要自定义? 通过上篇学习,了解到很多常用注解了,但是呢,总是有那么些需求.... 2.案例分析(手机号格式) 2.1.需要验证的实体 Bean public class LoginVo {@ ...
最新文章
- 使用cURL执行HTTP请求(使用PROXY)
- shell训练营Day18
- 初识OneNote 2016
- 高质量C++/C编程指南(林锐)
- LuceneLucene简介
- python分子化学模拟_python简单实现gillespie模拟
- linux系统中如何查看日志 (常用命令2)
- (转)Android属性设置android:noHistory=true
- 小程序php mysql_PHP+TP小程序授权登陆实现
- ERROR: Unrecognized command line argument: #39;use#39;
- VMware Player 12.5.0 中文免费版
- MTK中断控制器简单记录
- 手机上好用的java编译器_手机上好用的java编译器
- 大华工业相机SDK【QT+ C++】vs2015配置环境
- 【读万卷书】《哪有没时间这回事》
- U盘装系统工具哪个好用?
- 表带式无线测温传感器图片(开关柜测温装置传感器)
- cad是计算机辅助设计什么,“什么是cad软件“cad是什么
- python一维数组和二维数组,Python numpy实现二维数组和一维数组拼接的方法
- WEB安全师傅们的博客友情链接