目录
授权中心业务逻辑
1.远程调用queryUser方法验证用户名和密码
2.判断user是否为null
3.通过jwt工具类生成token
4.设置到cookie中
抽取配置类
我们在上一篇配置了生成的公私钥路径以及加盐的规则,这些东西我们应该给它放到配置文件中,先在application.yml文件中写入如下代码(以下操作均在auth-service微服务中进行)

然后我们还得新建一个类,用来读取配置文件。我们给它放到这里

代码如下,记得手动生产getset方法
@ConfigurationProperties(prefix = "leyou.jwt")
public class JwtProperties {
private String secret; // 密钥
private String pubKeyPath;// 公钥
private String priKeyPath;// 私钥
private int expire;// token过期时间
private String cookieName;
private PublicKey publicKey; // 公钥
private PrivateKey privateKey; // 私钥
private static final Logger logger = LoggerFactory.getLogger(JwtProperties.class);
/**
* @PostContruct:在构造方法执行之后执行该方法
*/
@PostConstruct
public void init(){
try {
File pubKey = new File(pubKeyPath);
File priKey = new File(priKeyPath);
if (!pubKey.exists() || !priKey.exists()) {
// 生成公钥和私钥
RsaUtils.generateKey(pubKeyPath, priKeyPath, secret);
}
// 获取公钥和私钥

该博客详细介绍了如何在SpringCloud中搭建授权中心,包括验证用户名和密码、生成JWT Token、配置路由、编写Controller和服务。同时,针对cookies无法写入前端的问题,提出了修改nginx配置和网关配置的解决方案。
最低0.47元/天 解锁文章
168万+

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



