nexus配置https及其客户端工具的配置

nexus配置https

场景:

公司要求关闭nexus的匿名登录功能,这样一来所有使用此nexus仓库的客户端命令行工具就要配置登录认证才可以使用nexus仓库的依赖包或镜像。

在大多数语言的命令行构建工具,比如:mvn、npm、pip等配置好登录认证信息后即可使用nexus,即使nexus没有配置https。

但是在实践中,go语言的命令行构建工具配置完登录认证是不可以的,报:“refusing to pass credentials to insecure URL”的错误,由此判断go可能要求nexus的协议为https的。

通过实验证实:go 在nexus以https协议提供服务后,即使关闭匿名登陆,只要配置好仓库地址和登录信息就可以正常使用了。

安装 Nexus 3

安装比较简单,下载并解压即可
下载地址:https://download.sonatype.com/nexus/3/nexus-3.24.0-02-unix.tar.gz (Nexus Repository Manager 3.24.0-02)

解压后目录结构

tar xf nexus-3.24.0-02-unix.tar.gz -C /usr/local/
tree /usr/local/nexus-3.24.0-02/ -L 1
/usr/local/nexus-3.24.0-02/
├── bin    # nexus可执行文件,通过./nexus start启动nexus
├── deploy
├── etc
├── lib
├── NOTICE.txt
├── OSS-LICENSE.txt
├── PRO-LICENSE.txt
├── public
└── system

配置自签名证书文件

运行该脚本,会在当前目录生成一个 keystore.jks

将 keystore.jks 放到/usr/local/nexus-3.24.0-02/etc/ssl 目录,方便管理

#!/bin/bash
NEXUS_DOMAIN=192.168.199.12       ##更改为你自己的nexus的IP
NEXUS_IP_ADDRESS=192.168.199.12   ##更改为你自己的nexus的IP
PASSWD=Nexus123					##这个密码我写的是nexus的登录密码,好像随便写也可以
keytool -genkeypair -keystore keystore.jks -storepass ${PASSWD}  -keypass ${PASSWD} -alias nexus -keyalg RSA -keysize 2048 -validity 5000 -dname "CN=${NEXUS_DOMAIN}, OU=Nexus, O=Nexus, L=Beijing, ST=Beijing, C=CN" -ext "SAN=IP:${NEXUS_IP_ADDRESS}" -ext "BC=ca:true"

生成客户端证书文件

keytool -export -alias nexus -keystore keystore.jks -file keystore.cer -storepass Nexus123

这里的Nexus123要对应脚本里面的passwd,运行上面的命令后会生成 keystore.cer文件。

自签名证书配置完毕

修改nexus配置文件

/usr/local/nexus-3.24.0-02/etc/nexus-default.propertie为nexus配置文件,可以修改端口等基本配置,修改前先备份。

cp nexus-default.propertie nexus-default.propertie.bak
vim nexus-default.propertie
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port-ssl=8443    ##ssl端口
application-port=8081        ##http端口
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml,${jetty.etc}/jetty-https.xml
nexus-context-path=/

# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=
 nexus-pro-feature

nexus.hazelcast.discovery.isEnabled=true

## 注:application-port-ssl和application-port都定义的有端口,则两个端口同时监听,要想访问http则访问8081,要想访问https则访问8443

再修改/usr/local/nexus-3.24.0-02/etc/jetty/jetty-https.xml文件。

 <Set name="KeyStorePath">/opt/nexus-3.10.0-04/etc/ssl/keystore.jks</Set>
    <Set name="KeyStorePassword">Nexus123</Set>
    <Set name="KeyManagerPassword">Nexus123</Set>
    <Set name="TrustStorePath">/opt/nexus-3.10.0-04/etc/ssl/keystore.jks</Set>
    <Set name="TrustStorePassword">Nexus123</Set>

或
    <Set name="KeyStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
    <Set name="KeyStorePassword">Nexus123</Set>
    <Set name="KeyManagerPassword">Nexus123</Set>
    <Set name="TrustStorePath"><Property name="ssl.etc"/>/keystore.jks</Set>
    <Set name="TrustStorePassword">Nexus123</Set>

两个Path区别,如果生成的证书文件在/usr/local/nexus-3.24.0-02/etc/ssl/目录下,则使用默认的,及下面的,否则不在ssl目录下,需要指明证书所在目录的绝对路径。

启动nexus

cd /usr/local/nexus-3.24.0-02/bin/
./nexus start

ss -tnl
State      Recv-Q Send-Q		Local Address:Port		Peer Address:Port              
LISTEN     0      50         	*:8081                   *:*                  
LISTEN     0      1             127.0.0.1:41778          *:*                  
LISTEN     0      128           *:22                     *:*                  
LISTEN     0      100           127.0.0.1:25             *:*                  
LISTEN     0      50            *:8443                   *:*                  
LISTEN     0      128           :::22                    :::*                  
LISTEN     0      100           ::1:25                   :::* 
## 若启动失败,则查看日志报错
## 日志所在目录:/usr/local/sonatype-work/nexus3/log/nexus.log

浏览器访问

http访问

https访问

命令行编译工具的配置

mvn

<!-- maven的settings.xml文件-->
<server>
      <id>nexus</id>
      <username>admin</username>
      <password>Nexus123</password>
   </server>      
........
<mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://192.168.199.12:8081/repository/maven-public/</url>
    </mirror>
........
<profile>
      <id>nexus</id>
      <repositories>
          <repository>
              <id>nexus</id>
              <url>http://192.168.199.12:8081/repository/maven-public/</url>
          <releases>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
          </releases>
          <snapshots>
              <enabled>true</enabled>
              <updatePolicy>always</updatePolicy>
          </snapshots>
      </repository>
      </repositories>
  </profile>
...........
<activeProfile>nexus</activeProfile>

npm

## 删除~/.npmrc文件
npm config set metrics-registry "http://192.168.199.12:8081/repository/npm-group/"
npm config set registry "http://192.168.199.12:8081/repository/npm-group/"
npm login http://192.168.199.12:8081/repository/npm-group/
username:
passwd:
email:

## ~/.npmrc文件
metrics-registry=http://192.168.199.12:8081/repository/npm-group/
registry=http://192.168.199.12:8081/repository/npm-group/
//192.168.199.12:8081/repository/npm-group/:_authToken=NpmToken.f237c4f7-fdee-3650-9d6f-84fec4de2124

最后一行是执行完npm login操作以后自动生成的。

pip

## 修改 ~/.pip/pip.conf
## 没有pip.conf则创建即可

[global]
index-url = http://admin:Nexus123@192.168.199.12:8081/repository/pip-proxy-ali/simple
[install]
trusted-host = 192.168.199.12

go

go env -w GO111MODULE="on" 
go env -w GOSUMDB=off
go env -w GOPROXY=https://admin:Nexus123@192.168.199.12:8443/repository/go-group/
export GOPROXY=https://admin:Nexus123@192.168.199.12:8443/repository/go-group/


## GO111MODULE变量说明:GO111MODULE 为开启或关闭模块的支持,它有三个可选值:off、on、auto,默认值是 auto。
GO111MODULE=off 无模块支持,go 会从 GOPATH 和 vendor 文件夹寻找包。
GO111MODULE=on 模块支持,go 会忽略 GOPATH 和 vendor 文件夹,只根据 go.mod 下载依赖。
GO111MODULE=auto 在 $GOPATH/src 外面且根目录有 go.mod 文件时,开启模块支持。

特别注意:

对于使用http或https的登录认证请求,一般可以直接将登录信息写在http或https://username:password@ip或域名。但是如果密码中存在特殊符号,如“#”、“!”等,则填写时需要将特殊符号百分比保留字符编码,特殊符号替换为表格第二行的内容。如:密码为:Smbands!,则要写为:https://admin:Smbands%21@IP或域名

具体可以参考:https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters

原文地址:https://www.cnblogs.com/Smbands/p/14430775.html