SpringCloud搭建授权中心实现用户登录(中)

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

 

目录

授权中心业务逻辑

抽取配置类

配置路由

编写Controller

新建接口远程调用

编写Service

测试生成cookie

解决cookies无法写入的问题


 

授权中心业务逻辑

 

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);
            }
            // 获取公钥和私钥
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值