1
开发一个博客系统

文章数:菜单文章都没更新完
搭建eureka注册中心 (请先阅读此文章) https://segmentfault.com/a/11...
搭建Beans编写Token解析注解 https://segmentfault.com/a/11...
搭建权限管理系统 https://segmentfault.com/a/11...
搭建后台管理系统 https://segmentfault.com/a/11...
前端Fetch请求封装 https://segmentfault.com/a/11...

1.创建一个maven的空项目,写自己的Entity和工具类在打包之后在其他项目中引入 就可以了;

IDEA打包部署项目到maven

在Idea中的最右边的导航栏中可以看到一个Maven Projects,单击打开,如图所示:
图片描述

引入Beans项目到当前项目:
<dependency>
    <groupId>com.server</groupId>
    <artifactId>beans</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
编写Token解析注解(Token的字符串-解析为->为TokenEntity实体)

注解解析Token实例

TokenEntity实体
public class TokenEntity{
    private String userId;
    private String token;
    private Date expiresDate; //过期时间
    private Date createDate;  //创建时间

    public TokenEntity(String userId, String token, Date expiresDate, Date createDate) {
        this.userId = userId;
        this.token = token;
        this.expiresDate = expiresDate;
        this.createDate = createDate;
    }

    public TokenEntity() {
        super();
    }

    get/set方法略
解析Token的工具类中的方法(此方法根据自己具体如何解析token来编写)
public static Object getToken(String token,String key) throws Exception {
        DecodedJWT jwt = verifyToken(token,key);
        Map<String, Claim> map = jwt.getClaims();
        TokenEntity bean = new TokenEntity();
        bean.setUserId(map.get("userId").asString());
        bean.setToken(token);
        bean.setExpiresDate(jwt.getIssuedAt());
        bean.setCreateDate(jwt.getExpiresAt());
        return bean;
    }
主要注解相关内容来了
注解类
import java.lang.annotation.*;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestToken {
    String value();
}
解析类
import com.server.authority.util.TokenUtil;
import com.server.config.SysBaseConfig;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

public class RequestTokenAnalysis implements HandlerMethodArgumentResolver{
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.hasParameterAnnotation(RequestToken.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        RequestToken token = methodParameter.getParameterAnnotation(RequestToken.class);
        String value = token.value();
        Class clazz = methodParameter.getParameterType();
        String data = nativeWebRequest.getParameter(value);
        if (data == null) {
            return clazz.newInstance();
        }
        Object object = TokenUtil.getToken(data, SysBaseConfig.TOKEN_KEY);
        return object;
    }
}
测试注解类
@RequestMapping(value = "/token",method = RequestMethod.GET)
    @ResponseBody
    public TResponse userToken(@RequestToken(ParameterNames.ACCESS_TOKEN) TokenEntity token){
        try{
            return authorityService.userVerify(token.getToken());
        }catch (Exception e){
            return tResponse.getTResponse("oxs-pai-appMenu","zh_CN", e.getMessage(), SResponse.getInstance().getSResponse(StatusCode.USER_DEFINED,"异常"));
        }
    }
例如地址:http://127.0.0.1:8762/auth/token
请求头中添加:at=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1MzI2NTI3MTIsInVzZXJJZCI6IjEiLCJpYXQiOjE1MzI1NjYzMTJ9.z1MweHILT6f2KeUumVSNuuY0F9EYsePQp5JmKJiwDuc
注入注解

import com.server.authority.annotation.RequestTokenAnalysis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@SpringBootApplication
@EnableEurekaClient
public class AuthorityApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(AuthorityApplication.class, args);
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new RequestTokenAnalysis());
        super.addArgumentResolvers(argumentResolvers);
    }
}
总结:能够成功解析出TokenEntity

代码牛
1.5k 声望655 粉丝