nacos-server(服务端)开启auth鉴权后,客户端请求返回403/503/unknown user问题

本文详细介绍了Nacos部署在外网时的安全隐患,包括服务注册的安全问题,以及如何通过开启鉴权来增强安全性。针对遇到的403错误和503或unknownuser问题,提供了具体的解决办法,包括升级nacos-client版本和调整Dubbo配置。同时,文中提醒了在使用URL参数传递Nacos账号密码时要注意避免特殊字符导致的问题,建议使用简单字符组合的密码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题场景

Nacos要部署到阿里云上面,并给其他服务器访问,但这样就会存在安全问题,这个问题在Nacos的官方开发手册上也有描述,连Nacos控制台的登录界面也写着“内网系统,不可暴露到公网”。所以这时就需要考虑到部署到外网的Nacos安全问题。

疑问

你可能会问:nacos不是有账号密码登录的吗?
nacos控制台(注意!是控制台!)确实有账号密码登录机制,但服务注册和服务发现并没有,所以居心叵测的人如果知道你外网Nacos的IP和端口,理论上可注册一个同名的服务到你的Nacos上,那么这时就会出大问题了。

解决方案

Nacos的服务端(即server)是可在配置文件中(/nacos/conf/application.properties)开启鉴权,这样客户端访问Nacos服务的时候就需要鉴权。具体可参考官方文档:关于鉴权章节

坑1:403问题

这个问题是因为spring-cloud-starter-alibaba-nacos内置的nacos-client依赖版本太低,通过修改pom排除原来的nacos-client,引入更新的1.4.0+版本即可(优快云上也有很多解决这个问题的同类文章可参考):

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.3.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.2.3.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.4.1</version>
        </dependency>

坑2:503或unknown user问题

根据上面更换nacos-client版本一般都能解决问题,因为大部分都是nacos-client版本问题。我当时也通过以上方式成功启动了项目。**但是!!**问题又来了,启动后一直报ERROR,日志显示请求Naocs服务列表及发送心跳请求失败。我当时几乎查遍了度娘、bing、GitHub上的issue,描述遇到的问题都是关于nacos-client的版本问题,但是我已经解决了nacos-client版本问题,所以我确定并不是nacos-client的问题。通过对日志的分析,发现是Dubbo抛出的异常,这时我终于发现问题了!是因为Dubbo的问题(我的项目微服务调用并不是用OpenFeign的,而是Dubbo)
然后我在yml中关于dubbo的配置中加入了username、password

dubbo:
	registry:
		# 省略其他配置项...
		username: nacos账号
		password: nacos密码

重启服务后发现问题依旧…o(╥﹏╥)o
然后又一顿度娘,终于在一篇优快云的文章中找到了原因及解决方案:
Dubbo + Nacos 服务启动报错,返回unknown user!
在这篇文章中提到:

“dubbo的源码中发现 username、paasword并没有实际用到,而在 RegistryConfig 类中发现 address属性值中,对username、password值进行了处理”

所以yml中dubbo关于 username、paasword的配置需要写在address配置项的值中:

dubbo:
	registry:
		# 省略其他配置项...
		address: nacos://你的Nacos地址?username=${你的Nacos的username}&password=${你的Nacos的password}

这里就会有一个小坑,如你们所见,账号密码都是以URL参数形式传递的,这就意味着如果你设置带特殊符号的复杂账号密码(然而大部分的生产环境都会设置带特殊符号的复杂账号密码)就有可能导致破坏了URL的拼接,导致账号密码只传递了一部分过去,导致鉴权失败
所以Nacos的账号密码,特别是密码,最好只由英文大小写+数字组成,不建议设置成带特殊符号的(至少不要用会影响URL拼接的特殊符号,如:#_&之类)

failed to req API:/nacos/v1/ns/instance after all servers([127.0.0.1:8848]) tried: failed to req API:127.0.0.1:8848/nacos/v1/ns/instance. code:403 msg: <html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Wed Jun 25 15:26:48 CST 2025</div><div>There was an unexpected error (type=Forbidden, status=403).</div><div>unknown user!</div></body></html> at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:467) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:389) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:191) ~[nacos-client-1.1.4.jar:na] at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:207) ~[nacos-client-1.1.4.jar:na] at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:64) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:76) ~[spring-cloud-alibaba-nacos-discovery-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:138) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:101) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:88) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47) ~[spring-cloud-commons-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:165) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] at com.atguigu.yygh.hosp.ServiceHospApplication.main(ServiceHospApplication.java:16) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.1.RELEASE.jar:2.2.1.RELEASE] Process finished with exit code 1
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值