点赞再看,养成习惯
开发环境:
- jdk 8
- intellij idea
- maven 3.6
所用技术:
- springboot
- restful
项目介绍
基于restful风格做的设计实例,即可jwt做token效验,实现增删查改,同时搭配自定义注解,方便过滤token验证
自定义注解
1.需要做验证的注解
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserLoginToken {
boolean required() default true;
}
//拦截类(AuthenticationInterceptor)代码
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
String token = httpServletRequest.getHeader("token");// 从 http 请求头中取出 token
// 如果不是映射到方法直接通过
if(!(object instanceof HandlerMethod)){
return true;
}
HandlerMethod handlerMethod=(HandlerMethod)object;
Method method=handlerMethod.getMethod();
//检查是否有passtoken注释,有则跳过认证
if (method.isAnnotationPresent(PassToken.class)) {
PassToken passToken = method.getAnnotation(PassToken.class);
if (passToken.required()) {
return true;
}
}
//检查有没有需要用户权限的注解
if (method.isAnnotationPresent(UserLoginToken.class)) {
UserLoginToken userLoginToken = method.getAnnotation(UserLoginToken.class);
if (userLoginToken.required()) {
// 执行认证
if (token == null) {
throw new RuntimeException("无token,请重新登录"

这是一个基于SpringBoot和RESTful风格的项目实例,使用JWT进行token验证,涵盖增删查改操作。通过自定义注解实现方便的token过滤。项目结构清晰,包括token获取、各种HTTP请求的token验证。此外,还引入了Json Schema来定义数据约束。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



