《微服务之间鉴权的流程》
02、微服务间鉴权:(2)定时获取Token-定时任务
说明:之前我们在网关微服务已经测试通过了生成token的方法,现在我们需要编写定时来获取token。
我们这里用到了SpringTask定时任务功能,接着我们在网关开启SpringTask进行测试。
1)在网关微服务中开启定时任务
2)定时任务测试之固定频率定时任务测试
package com.leyou.gateway.scheduler;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 定时任务类
*/
@Component
public class SchedulingDemo {
/**
* 固定频率(每隔多久) 单位:毫秒
*/
@Scheduled(fixedRate = 5000)
public void rateJob(){
System.out.println("固定频率任务触发:"+ DateTime.now());
}
}
3)定时任务测试之cron表达式定时任务测试
上面不管是固定频率还是固定延迟,执行规则都比较简单,假如我们有非常复杂的执行要求,就要用到cron表达式了。
cron表达式分七个域,分别为:
年:一般不指定
周:? * ,- /
月:* , - /
日:? * , - /
时:* , - /
分:* , - /
秒:* , - /
使用cron表达式指定一个,每年,每月,1到6号,每天上午9点,从3分开始,每隔5分钟的第八秒执行一次。
package com.leyou.gateway.scheduler;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 定时任务类
*/
@Component
public class SchedulingDemo {
/**
* cron表达式
* cron表达式:SpringTask的cron只有6位(不能设置年)
* 秒 分 时 日 月 周
*/
@Scheduled(cron = "0/5 * * * * ?")
public void cronJob(){
System.out.println("cron任务触发:"+ DateTime.now());
}
}
4)SpringBoot中定时任务优化
spring:
task:
scheduling:
pool:
size: 1 #一般有几个定时任务就开启几个线程
03、微服务间鉴权:(2)定时获取Token-网关微服务
接下来我们分别在网关微服务和搜索微服务定时获取Token。
首先,在网关微服务完成Token获取。
1) 导入jar包(已完成)
<!--导入feign相关jar包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.leyou</groupId>
<artifactId>ly-client-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2) 在启动类上开启定时任务(已完成)
package com.leyou;
import com.leyou.gateway.config.FilterProperties;
import com.leyou.gateway.config.JwtProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringCloudApplication
@EnableScheduling //开启定时任务
@EnableFeignClients
public class LyGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(LyGatewayApplication.class, args);
}
}
3) 提供获取token相关的配置文件
在application.yml添加当前服务的服务名称和服务密钥
ly:
jwt:
pubKeyPath: D:\studyProject\rsa\ras-key.pub # 公钥地址
cookie:
cookieName: LY_TOKEN # cookie名称
app:
serviceName: api-gateway
secret: api-gateway
4) 修改解析配置文件的配置类
package com.leyou.gateway.config;
import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import javax.annotation.PostConstruct;
import java.security.PublicKey;
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {
……
private AppPojo app = new AppPojo();
@Data
public class AppPojo{
private String serviceName;
private String secret;
}
……
}
5) 编写定时任务获取服务认证的token
package com.leyou.gateway.scheduler;
import com.leyou.auth.client.AuthClient;
import com.leyou.gateway.config.JwtProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 定时向授权中心申请服务token
*/
@Component
@Slf4j
public class AppTokenScheduler {
@Autowired
private AuthClient authClient;
@Autowired
private JwtProperties jwtProps;
//设计一个成员变量,用于存放当前服务的token
private String appToken;
/**
* token刷新间隔
*/
private static final long TOKEN_REFRESH_INTERVAL = 86400000L; //24小时
/**
* token获取失败后重试的间隔
*/
private static final long TOKEN_RETRY_INTERVAL = 10000L;
/**
* 固定频率(1天调用1次)
*/
@Scheduled(fixedRate = TOKEN_REFRESH_INTERVAL)
public void appToken(){
//需要设计重试机制,避免1次申请不到没有token可用的情况
while(true) {
try {
//向授权中心申请服务token
String appToken = authClient.authorization(
jwtProps.getApp().getServiceName(),
jwtProps.getApp().getSecret());
//进行token赋值
this.appToken = appToken;
log.info("【申请token】申请token成功,服务名称:"+jwtProps.getApp().getServiceName());
//一旦toke获取成功,则退出
break;
} catch (Exception e) {
e.printStackTrace();
log.error("【申请token】申请token失败,10秒后重试...");
//10秒后重试
try {
Thread.sleep(TOKEN_RETRY_INTERVAL);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
//提供get方法给外部获取服务token
public String getAppToken() {
return appToken;
}
}
04、微服务间鉴权:(2)定时获取Token-搜索微服务
1) 导入jar包
<dependency>
<groupId>com.leyou</groupId>
<artifactId>ly-client-auth</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2) 在启动类上开启定时任务
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 搜素微服务
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling
public class LySearchApplication {
public static void main(String[] args) {
SpringApplication.run(LySearchApplication.class,args);
}
}
3) 提供获取token相关的配置文件
ly:
jwt:
pubKeyPath: D:\studyProject\rsa\ras-key.pub # 公钥地址
cookie:
cookieName: LY_TOKEN # cookie名称
app:
serviceName: search-service
secret: search-service
4) 解析配置文件的配置类
package com.leyou.search.config;
import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.security.PublicKey;
/**
* 读取Jwt相关配置
*/
@Data
@Component
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties {
private String pubKeyPath;//公钥路径
private PublicKey publicKey;//公钥
private CookiePojo cookie = new CookiePojo();
private AppTokenPojo app = new AppTokenPojo();
@Data
public class CookiePojo{
private String cookieName;
}
@Data
public class AppTokenPojo{
private String serviceName;
private String secret;
}
/**
* 读取公钥
*/
@PostConstruct
public void initMethod() throws Exception {
publicKey = RsaUtils.getPublicKey(pubKeyPath);
}
}
5) 编写定时任务获取服务认证的token
package com.leyou.search.scheduled;
import com.leyou