简介
在上一篇 微服务-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;
}
}
本文介绍了微服务中Token处理类的重构过程,从静态方法转换为组件实例方法,并通过Spring Boot从配置文件加载Token相关参数。
7744

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



