微服务: Token 相关的重构

简介

在上一篇 微服务-Token的处理 中,写了一个 MSAuthTokenUtil 类,用来生成、刷新、校验 token,该类的方法都是 static 的。后续想了一下,还是将其改为普通的组件较好,在最新代码中对其做了两个较大的重构。

重构为组件

将其改名为 MSAuthTokenHelper,并将其中的所有 static 方法改为实例方法,用注解 @Component 修饰。

@Slf4j
@Component
public class MSAuthTokenHelper {
    ///....
}

在所有使用到 MSAuthTokenHelper 的地方,增加对应的自动注入后调用即可。如下:

private MSAuthTokenHelper tokenHelper;

@Autowired
public void setTokenHelper(MSAuthTokenHelper tokenHelper) {
    this.tokenHelper = tokenHelper;
}

@Override
public MSResponse refreshUserToken(String token) {
    // ...
    
    String refreshToken = tokenHelper.refreshToken(token);

    if (null != refreshToken) {
        String userID = tokenHelper.userIDfromToken(token);
    }
    
    // ...
}

这里顺便提一下,@Controller@Service@Repository 以及 @Component 的区别以及联系,如下表所示:

注解含义
@Component最普通的组件,可以被注入到spring容器进行管理
@Repository作用于持久层
@Service作用于业务逻辑层
@Controller作用于表现层(spring-mvc的注解)

@Controller@Service@Repository 都继承了 @Component 的功能,可以看这几个注解的源码得知。

/// Repository 注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

/// Controller 注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

/// Service 注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

当一个类被 @Component 所注解,那么就意味着同样可以用 @Repository, @Service, @Controller 来替代它,同时这三个注解会具备有更多的功能,而且功能各异,可以根据自己的需要使用不同的注解来表示不同的业务和逻辑。具体可以参考 Spring/Spring-Boot 学习 @Controller,@Component,@Service,@Repository的异同 这篇文章,写的很清楚了。

从配置文件读取 token 的配置

在之前的文章中,分享过如何通过 SpringBoot 的 @ConfigurationProperties 注解来读取配置文件,可以参考 微服务-ConfigurationProperties配置 这篇文章。

properties 文件中,新增如下的配置信息:

# Auth token config
# --------------------------------------------
msconfig.authtoken.claims_jwtsid=restful_api
msconfig.authtoken.claims_subject=admin
msconfig.authtoken.claims_audience=client
#token 过期时间24小时(24 * 60 * 60 * 1000)
msconfig.authtoken.token_expire_time=86400000
#密钥盐
msconfig.authtoken.token_secret=token123

这些配置信息对应的 model 是 MSAuthTokenPropertyConfig,如下:

@Setter
@Getter
@Component
@ConfigurationProperties(prefix = "msconfig.authtoken")
public class MSAuthTokenPropertyConfig {

    private String claims_jwtsid;
    private String claims_subject;
    private String claims_audience;
    private long token_expire_time;//token 过期时间24小时
    private String token_secret;//密钥盐
}

MSAuthTokenHelper直接使用即可,关键代码如下所示:

@Slf4j
@Component
public class MSAuthTokenHelper {
    
    private MSAuthTokenPropertyConfig authTokenPropertyConfig;

    @Autowired
    public void setAuthTokenPropertyConfig(MSAuthTokenPropertyConfig authTokenPropertyConfig) {
        this.authTokenPropertyConfig = authTokenPropertyConfig;
    }

    public String generateToken(String userID) {
        String token = "";

        long tokenExpireTime = authTokenPropertyConfig.getToken_expire_time();
        String jwtsid = authTokenPropertyConfig.getClaims_jwtsid();
        String subject = authTokenPropertyConfig.getClaims_subject();
        String audience = authTokenPropertyConfig.getClaims_audience();
        String tokenSecret = authTokenPropertyConfig.getToken_secret();
        
        //...

        return token;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值