maven:中央仓库验证方式改变:401 Content access is protected by token

前几天向maven中央仓库发布版本,执行上传命令mvn release:perform时报错了:

ERROR\] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.13:deploy (injected-nexus-deploy) on project xxxxx: Failed to deploy artifacts: Could not transfer artifact xxxxx:pom:6.0.7-20240619.183701-1 from/to ossrh (https://oss.sonatype.org/content/repositories/snapshots): authentication failed for https://oss.sonatype.org/content/repositories/snapshots/xxxxxx.pom, status: 401 Content access is protected by token -\> \[Help 1

ERROR

ERROR\] To see the full stack trace of the errors, re-run Maven with the -e switch. \[ERROR\] Re-run Maven using the -X switch to enable full debug logging. \[ERROR

ERROR\] For more information about the errors and possible solutions, please read the following articles: \[ERROR\] \[Help 1\] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我的maven中央仓库的账号注册了好多年了,一直正常使用,没有出现过问题。

最终找到mave官方说明:《401 Unauthorized error》知道了原因:

此错误消息表示您可能通过提供用户名密码明文验证执行到OSSRH的发布。然而这是以前允许的与OSSRH交互的方式,现在OSSRH发布的身份验证过程的更改了,要求使用用户令牌机制。我们的有关于《设置用户令牌》的文档会对您有所帮助。

如果你是maven中央仓库的老用户,而不是新注册用户,而且忽略了最近Sonatype启用了新的基于令牌的身份验证机制,则可能遇到与我同样的情况。

以前我们在$HOME/.m2/settings.xml如下以明文形式保存maven中央仓库的验证信息

xml 复制代码
    <server>
      <id>sonatype-nexus-snapshots</id>
      <username>${username}</username>
      <password>${password}</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>${username}</username>
      <password>${password}</password>
    </server>

新的基于令牌的身份验证机制要求将<username><password>字段以加密令牌形式保存。

需要参照《Generate a Token on NXRM servers》登录Nexus Repository Manager后台,在Profile/User Token/Access User Token界面如下 获取你的令牌

将得到的令牌数据如下更新$HOME/.m2/settings.xml中的 <username><password>字段就解决问题了。

参考资料

《401 Unauthorized error》
《Generate a Token on NXRM servers》
《Sonatype -- 401 Content access is protected by token》

相关推荐
我是咸鱼不闲呀6 分钟前
力扣Hot100系列22(Java)——[图论]总结(岛屿数量,腐烂的橘子,课程表,实现Trie(前缀树))
java·leetcode·图论
1104.北光c°6 分钟前
深入浅出 Elasticsearch:从搜索框到精准排序的架构实战
java·开发语言·elasticsearch·缓存·架构·全文检索·es
MSTcheng.12 分钟前
【优选算法必修篇——位运算】『面试题 01.01. 判定字符是否唯一&面试题 17.19. 消失的两个数字』
java·算法·面试
蹦哒14 分钟前
Kotlin 与 Java 语法差异
java·python·kotlin
左左右右左右摇晃15 分钟前
Java并发——并发编程底层原理
java·开发语言
一个有温度的技术博主20 分钟前
Redis系列八:Jedis连接池在java中的使用
java·redis·bootstrap
cyforkk22 分钟前
Java 并发编程教科书级范例:深入解析 computeIfAbsent 与方法引用
java·开发语言
后青春期的诗go25 分钟前
泛微OA-E9与第三方系统集成开发企业级实战记录(八)
java·接口·金蝶·泛微·oa·集成开发·对接
dreamxian29 分钟前
苍穹外卖day09
java·spring boot·tomcat·log4j·maven
剑海风云30 分钟前
JDK 26之安全增强
java·开发语言·安全·jdk26