解析JWT+SpringBoot+SpringMVC参数解析器

简介

大家以前都使用过session存储信息,有的交给容器创建,有的存储到mysql或者redis,这次项目用到了JWT,我们把用户的信息和登录的过期时间都封装到一个token字符串里,客户端每次请求只需要在头信息里携带token即可,话不多说,下面是目录结构.

解析JWT+SpringBoot+SpringMVC参数解析器

一.annonation注解

package com.demo.annotation;import java.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface IgnoreLogin {}

该注解主要作用是过滤掉请求拦截器,使用该注解就不会对该请求进行拦截(权限校验),具体使用下面讲.

package com.demo.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 登录用户信息 */@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)public @interface LoginUser {}

该注解作用是SpringMVC参数解析器,类似于RequestBody注解(希望大家了解springmvc的参数解析机制),和我们后面的resolver相关联.

二.bean实体

package com.demo.bean;public class User { private long userId; private String userName; private String password; 忽略get/set}

我们的用户信息

package com.demo.bean;public class Business { private String str; private int num; 忽略get/set}

我们的业务参数

三.config配置信息

package com.demo.config;import com.demo.interceptor.AuthorizationInterceptor;import com.demo.resolver.UserArgumentResolver;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.method.support.HandlerMethodArgumentResolver;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.List;/** * MVC配置 */@Configurationpublic class WebMvcConfig implements WebMvcConfigurer { @Autowired private AuthorizationInterceptor authorizationInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(authorizationInterceptor).addPathPatterns("/**"); //注入我们自定义的拦截器,拦截所有请求 } @Override public void addArgumentResolvers(List resolvers) { resolvers.add(new UserArgumentResolver()); //注入我们的用户参数解析器 }}

四.controller

package com.demo.controller;import com.demo.annotation.IgnoreLogin;import com.demo.annotation.LoginU
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

栗子博客 程序 解析JWT+SpringBoot+SpringMVC参数解析器 https://www.lizi.tw/program/21911.html

建筑工地上施工员,闲暇时弄个博客打发时间,

常见问题
  • 1、杰齐1.7仅适用于PHP5.2 2、需Zend支持 3、尽量使用宝塔面板 4、尽量使用Windows 系统,关关对Linux支持不太友好。
查看详情

相关文章

评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

解析JWT+SpringBoot+SpringMVC参数解析器-海报

分享本文封面