1、引入依赖
<dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.4.0</version> </dependency>
2、自定义注解
跳过验证注解
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @email 913062247@qq.com * @author:菜园子 * @date: 2019/6/11 * @time: 10:58 */ @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface PassToken { boolean required() default true; }
验证token权限注解
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @email 913062247@qq.com * @author:菜园子 * @date: 2019/6/11 * @time: 11:04 */ @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface UserLoginToken { boolean required() default true; }
3、工具类(jwt.key配置文件application.properties)
import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.diveyun.gas.entity.User; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; /** * @email 913062247@qq.com * @author:菜园子 * @date: 20