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中央仓库的验证信息

    <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》

### OAuth2 登录时出现 'Full authentication is required to access this resource' 的解决方案 当遇到 `Full authentication is required to access this resource` 错误时,通常是因为 Spring Security 或者 OAuth2 配置不正确导致的认证失败。以下是详细的分析和解决方法: #### 1. **检查依赖配置** 确保项目的 Maven 或 Gradle 文件中包含了必要的依赖项。如果缺少某些核心库可能导致认证流程异常中断。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security.oauth.boot</groupId> <artifactId>spring-security-oauth2-autoconfigure</artifactId> </dependency> ``` 上述依赖对于实现基于 OAuth2 的安全机制至关重要[^3]。 --- #### 2. **Spring Security 和 OAuth2 配置文件调整** 在应用启动类或者配置类中,需确认已正确定义了 WebSecurityConfigurerAdapter 并允许特定路径无需身份验证即可访问。例如,在开发阶段可以开放 `/oauth/token` 接口供外部调用。 ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/oauth/token").permitAll() // 允许 /oauth/token 不受保护 .anyRequest().authenticated(); http.csrf().disable(); // 如果不需要 CSRF,则禁用它以简化调试过程 } } ``` 此设置可防止初次请求因未完成的身份验证而被拒绝[^2]。 --- #### 3. **Token 请求头缺失问题排查** 部分情况下,客户端发送 POST 请求至 `/oauth/token` 时忘记携带 Basic Auth 认证信息(即 client_id 和 client_secret)。这会触发服务器端的安全拦截器抛出 `Unauthorized` 响应码。 建议通过工具如 Postman 测试 API 是否正常工作,并按照以下方式构建 HTTP Header 参数: - Authorization: Basic base64(client_id:client_secret) 其中 Base64 编码后的字符串可以通过 Python 实现快速转换: ```python import base64 credentials = f"your_client_id:your_client_secret" encoded_credentials = base64.b64encode(credentials.encode()).decode() print(encoded_credentials) ``` 随后将其作为 Value 设置到 Request Headers 中[^4]。 --- #### 4. **Session 管理冲突处理** 有时由于 Session Cookie 存储不当也会引发重复登录现象。尝试修改 sessionManagement 方法来强制每次新连接都创建独立会话实例。 ```java @Override protected void configure(HttpSecurity http) throws Exception { http.sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS); super.configure(http); } ``` 以上代码片段表明应用程序不会保存任何状态数据于服务端内存里,从而规避潜在风险[^1]。 --- #### 5. **日志级别提升辅助诊断** 为了更精准定位具体环节中的错误源头,临时提高框架的日志记录等级有助于收集更多上下文线索。 编辑 application.properties 添加如下内容: ```properties logging.level.org.springframework.security=DEBUG logging.level.org.apache.tomcat.util.http=FINEST ``` 启用这些选项后重新运行程序并观察控制台输出变化情况。 --- ### 总结 综合考虑以上几点因素逐一排除干扰源直至恢复正常运作为止。最终目的是让整个授权链条顺畅衔接无阻塞点存在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值