nacos开启鉴权+springboot配置用户名密码
作者:mmseoamin日期:2024-03-20

nacos默认没有开启鉴权,springboot无需用户名密码即可连接nacos。从2.2.2版本开始,默认控制台也无需登录直接可进行操作。

因此本文记录一下如何开启鉴权,基于nacos2.3.0版本。

  1. 编辑nacos服务端的application.properties:
# 开启鉴权
nacos.core.auth.enabled=true
# 用户服务间通信鉴权的header的key和value
nacos.core.auth.server.identity.key=abc
nacos.core.auth.server.identity.value=def
# 自定义指定生成JWT的密钥,使用BASE64进行编码,编码前的key长度必须不小于32个字符
nacos.core.auth.plugin.nacos.token.secret.key=MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=
  1. 启动nacos后,使用默认的用户名nacos和密码nacos进行登录:

    nacos开启鉴权+springboot配置用户名密码,在这里插入图片描述,第1张

    登录后可进行密码修改:

    nacos开启鉴权+springboot配置用户名密码,在这里插入图片描述,第2张

  2. springboot配置文件中指定自己的用户名和密码:

    spring:
      cloud:
        nacos:
          username: nacos
          password: xxxxxx
          discovery:
            server-addr: http://xxx.xxx.xxx.xxx:8848
          config:
            server-addr: http://xxx.xxx.xxx.xxx:8848
            file-extension: yaml
    

参考:https://nacos.io/docs/v2/guide/user/auth/