一、配置application.properties
#启用鉴权功能
nacos.core.auth.enabled=true
#关闭User-Agent鉴权白名单
nacos.core.auth.enable.userAgentAuthWhite=false
#配置自定义身份识别
nacos.core.auth.server.identity.key=your_key
nacos.core.auth.server.identity.value=your_value
#配置JWT令牌密钥(为了生成JWT令牌,需要配置一个自定义的密钥。注意,原始密钥长度不得低于32字符,且需要进行Base64编码。)
nacos.core.auth.plugin.nacos.token.secret.key=your_secret_key
#配置权限缓存(可选):为了提高性能,可以开启权限缓存。开启后,权限缓存的更新会有一定的延迟。添加以下配置项:
nacos.core.auth.caching.enabled=true
二、spring cluod配置
#在gateway的application.yml文件加
spring:
cloud:
nacos:
discovery:
username: your_key
password: your_value
/**
* 创建ConfigService
*
* @return
*/
private ConfigService createConfigService() {
try {
Properties properties = new Properties();
properties.setProperty("serverAddr", GatewayRoutersConfiguration.SERVER_ADDR);
properties.setProperty("namespace", GatewayRoutersConfiguration.NAMESPACE);
//增加以下账号密码
properties.setProperty("username", "your_key");
properties.setProperty("password", "your_value");
return configService = NacosFactory.createConfigService(properties);
} catch (Exception e) {
log.error("创建ConfigService异常", e);
return null;
}
}
#配置项目base-boot-starter\base-boot-starter-cloud的yml文件
spring:
profiles:
# 当前激活环境
active: @profile.name@
cloud:
nacos:
config:
# Nacos 认证用户
username: your_key
# Nacos 认证密码
password: your_value