ユーレカ戦闘-4 - オープンHTTP基本認証機関]

何のセキュリティ認証がない場合、当社の生産環境では、このようなユーザのログイン、またはユーレカ・サーバなどのセキュリティ問題、検討する必要がある、それは、彼らの残りのAPI外に露出している、残りのAPIを介して自由に他の人ができることを意味していますデータを変更し、これは非常に恐ろしいことで、私たちはこの記事の詳細に入るには、どのようにユーレカのサーバー認証であり、ユーレカクライアントは、認証情報を設定する方法です。

 

公共ポンポンファイルの依存関係:

<親> 
        <groupIdを> org.springframework.boot </ groupIdを> 
        <たartifactId>春・ブート・スターター・親</たartifactId> 
        <バージョン> 2.0.3.RELEASE </バージョン> 
        <relativePath /> <! -検索親リポジトリから- > 
</親> 

<プロパティ> 
        <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> 
        <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding> 
        < java.version> 1.8 </java.version> 
        <スプリングcloud.version> Finchley.RELEASE </spring-cloud.version> 
</プロパティ> 

<dependencyManagement> 
        <依存関係> 
            <依存>依存関係> 
                <groupIdを> org.springframework.cloud </ groupIdを>
                <たartifactId>ばねクラウド依存性</たartifactId> 
                <バージョン> $ {ばねcloud.version} </バージョン> 
                <タイプ> POM </ type>の
                <スコープ> インポート </スコープ> 
            </依存> 
        </依存関係> 
</ dependencyManagement>

 

1、ユーレカサーバープロジェクト

1.1、ユーレカサーバープロジェクトポンポン:

<! -プラス公共の頭には、物品を頼る- > 

<依存> <依存> <groupIdを> org.springframework.cloud </ groupIdを> <たartifactId>春クラウド・スターター・ネットフリックス-ユーレカ・サーバー</たartifactId > </依存関係>
     
     <! - > -権利がある限りこの依存ポンポンファイルがあるとして、依存している、プロジェクトがデフォルトの権限チェックで開かれた <依存> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春スターター・セキュリティ-ブーツ</たartifactId> </依存関係> </依存関係> <構築する> <プラグイン> <プラグイン> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブートのmaven-pluginの</たartifactId> </プラグイン> </プラグイン> </構築>

 

1.2、ユーレカサーバープロジェクトは、カテゴリを開始しました:

輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 

@SpringBootApplication 
@EnableEurekaServer 
パブリック クラスEurkeaServerApplication { 

    公共 静的 ボイドメイン(文字列[]引数){ 
        SpringApplication.run(EurkeaServerApplication。クラス、引数)。
    } 
}

 

1.3、ユーレカサーバープロジェクトの設定ファイル、パス:ユーレカ・サーバー\ SRC \メイン\リソース\

アプリケーションのsecurity.yml:

サーバー:
  ポート: 8761 

春:
  セキュリティ:
    基本:
      有効:真の
    ユーザー:
      名:admin 
      パスワード:Xk38CNHigBP5jK75 
ユーレカ:
  インスタンス:
    ホスト名:localhostの
  クライアント:
    registerWithEureka:
    fetchRegistry:
    のserviceURL:
      defaultZoneます。http:// $ {eureka.instance .hostname}:$ {はserver.port} /ユーレカ/ 
  サーバー:
      waitTimeInMsWhenSyncEmpty: 0 
      enableSelfPreservation:

application.yml:

春:
  プロフィール:
    アクティブ:セキュリティ

 

クライアント側アプリケーションは、このような非インターフェース、やや不適切が、Javaを設定する必要があり、コンフィギュレーション・ファイルを無効にする方法はありませんため、デフォルトのCSRFチェックによって有効春・ブート・スターター・セキュリティ以来、以下のように、無効になりました:

輸入org.springframework.security.config.annotation.web.builders.HttpSecurity。
輸入org.springframework.security.config.annotation.web.configuration.EnableWebSecurity。
輸入org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter。

/ ** 
 *关闭春ブート・スターター・セキュリティ的にCSRF校验
 * / 
@EnableWebSecurity 
パブリック クラス SecurityConfigが拡張WebSecurityConfigurerAdapter { 

    @Overrideは、
    保護された 、ボイド(HttpSecurity HTTP)の設定をスロー{例外を
         スーパー.configure(HTTP)。
        http.csrf()(無効)。
    } 
}

 

1.4、ユーレカサーバープロジェクトを開始するコマンドを実行します。

MVNスプリングブート:実行

コマンド・ライン・ターミナルを開き、次のコマンドを実行します-iます。httpカール:// localhostを:8761 /ユーリカ/アプリケーション

-iます。httpカール:// localhostを:8761 /ユーレカ/アプリの
 HTTP / 1.1 401 
セット -cookie:JSESSIONID = 554BCAF092D8D1ED3936C0CB09E91AF1。パス= / ; HttpOnlyの
WWW -Authenticate:基本レルム= "レルム" 
X -content-タイプ- オプション:nosniff 
X -Xss-保護:1; モード= ブロック
キャッシュ -制御:キャッシュなし、無店舗、最大エージング= 0、must- 再検証
プラグマ:なし - キャッシュが
有効期限: 0 
X -Frame- オプション:DENY 
コンテンツ -Type:アプリケーション/ JSONを;のcharset = UTF -8 
転送 - エンコード:チャンク
日:金、2019年10月4日7時31分57秒GMT 

{ "タイムスタンプ": "2019-10-04T07:31:57.888 + 0000"、 "ステータス":401、 "エラー": "不正"、 "メッセージ": "不正"、 "パス": "/ユーレカ/アプリ"}

図から分かるように、ヘッダが送信されない認証は、401のステータスコードを返します。

 

のHTTP基本認証ヘッダーの転送を使用して、次のアカウントのパスワード:

カール-i --basic -u管理者:Xk38CNHigBP5jK75ます。http:// localhostを:8761 /ユーレカ/アプリの
 HTTP / 1.1 200 
セット -cookie:JSESSIONID = CF1C0DE56415626494EC539A654CC543。パス= / ; HttpOnlyの
X -content-タイプ- オプション:nosniff 
X -Xss-保護:1; モード= ブロック
キャッシュ -制御:キャッシュなし、無店舗、最大エージング= 0は、must- 再検証しない
プラグマを:なし - キャッシュが
有効期限: 0 
X -Frame- オプション:DENY 
コンテンツアプリケーション/:-Type XMLの
転送 - エンコード:チャンク
日付:金、 2019年10月4日7時35分54秒GMT

<アプリケーション> 
  <versions__delta> 1 </ versions__delta> 
  <apps__hashcode> </ apps__hashcode> 
</アプリケーション>

リクエストが成功しました。 

 

2、ユーレカクライアントプロジェクト

2.1、ユーレカクライアントプロジェクトのPOM:

<! -加上文章头部的公共依赖- > 

<依存性> <依存> <groupIdを> org.springframework.cloud </ groupIdを> <たartifactId>春・クラウド・スターター・ネットフリックス-ユーレカ・クライアント</たartifactId > </依存関係> </依存関係> <構築> <プラグイン> <プラグイン> <groupIdを> org.springframework.boot </ groupIdを> <たartifactId>春・ブートのmaven-pluginの</たartifactId> </プラグイン> </プラグイン> </構築>

 

2.2、ユーレカクライアントプロジェクトは、カテゴリを開始しました:

輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.cloud.client.discovery.EnableDiscoveryClient。

@SpringBootApplication 
@EnableDiscoveryClient 
パブリック クラスEurekaClientApplication { 

    公共 静的 ボイドメイン(文字列[]引数){ 
        SpringApplication.run(EurekaClientApplication。クラス、引数)。
    } 
}

 

2.3、ユーレカクライアントプロジェクトの設定ファイル、パス:ユーレカ・クライアント\ SRC \メイン\リソースを\

プロジェクトがユーレカサーバーHTTP基本認証を開設しているので、ユーレカクライアントのプロジェクトはまた、我々は、設定ファイルで指定され、ここで、合格するために、適切なアカウント情報を追加する必要があります。

アプリケーションのsecurity.yml:

サーバー:
  ポート: 8081 

春:
  アプリケーション:
    名前:CLIENT1 

ユーレカ:
  クライアント:
    セキュリティ:
      基本:
        利用者:管理者
        パスワード:Xk38CNHigBP5jK75 
    のserviceURL:
      defaultZoneます。http:// $ {eureka.client.security.basic.user}:$ {ユーレカ.client.security.basic.password} @localhost:8761 /ユーレカ/

application.yml:

春:
  プロフィール:
    アクティブ:セキュリティ

 

执行:Xk38CNHigBP5jK75します。http:// localhost:8761 /ユーレカ/アプリの-i --basic -u管理者カール

カール-i --basic -u管理者:Xk38CNHigBP5jK75ます。http:// localhostを:8761 /ユーレカ/アプリの
 HTTP / 1.1 200 
セット -cookie:JSESSIONID = C7CE372067A44606E9D3DEA6B64AEDCD。パス= / ; HttpOnlyの
X -content-タイプ- オプション:nosniff 
X -Xss-保護:1; モード= ブロック
キャッシュ -制御:キャッシュなし、無店舗、最大エージング= 0は、must- 再検証しない
プラグマを:なし - キャッシュが
有効期限: 0 
X -Frame- オプション:DENY 
コンテンツアプリケーション/:-Type XMLの
転送 - エンコード:チャンク
日付:金、 2019年10月4日午前7時53分40秒GMT

<アプリケーション> 
  <versions__delta> 1 </ versions__delta> 
  <apps__hashcode> UP_1 _ </ apps__hashcode> 
  <アプリケーション> 
    <名前> CLIENT1 </名前> 
    <インスタンス> 
      <するinstanceId> 192.168.50.161:client1:8081 </するinstanceId> 
      で、<hostname> 192.168.50.161 </ホスト名> 
      <アプリケーション> CLIENT1 </アプリ> 
      説明<ipaddr> 192.168.50.161 </ ip-addrで> 
      <状態> UP </状態> 
      <overriddenstatus> UNKNOWN </ overriddenstatus> 
      <ポートが有効= "真"> 8081 </ポート> 
      <securePort有効= "偽"> 443 </ securePort> 
      <countryId> 1 </ countryId>
      <dataCenterInfo クラス = "com.netflix.appinfo.InstanceInfo $ DefaultDataCenterInfo">
        <名前> MyOwn </名前> 
      </ dataCenterInfo> 
      <leaseInfo> 
        <renewalIntervalInSecs> 30 </ renewalIntervalInSecs> 
        <durationInSecs> 90 </ durationInSecs> 
        <registrationTimestamp> 1570175584067 </ registrationTimestamp> 
        <lastRenewalTimestamp> 1570175584067 </ lastRenewalTimestamp> 
        <evictionTimestamp> 0 </ evictionTimestamp> 
        <serviceUpTimestamp> 1570175584067 </ serviceUpTimestamp> 
      </ leaseInfo> 
      <メタデータ> 
        <management.port> 8081 </management.port> 
      </メタデータ> 
      <homePageUrl>のhttp:// 192.168.50.161:8081 / </ homePageUrl> 
      <statusPageUrl>のhttp://192.168.50.161:8081/actuator/info </ statusPageUrl> 
      <healthCheckUrl>のhttp:// 192.168.50.161:8081/actuator/health </ healthCheckUrl> 
      <vipAddress> CLIENT1 </ vipAddress> 
      <secureVipAddress> CLIENT1 </ secureVipAddress> 
      < isCoordinatingDiscoveryServer>  </ isCoordinatingDiscoveryServer> 
      <lastUpdatedTimestamp> 1570175584067 </ lastUpdatedTimestamp> 
      <lastDirtyTimestamp> 1570175583914 </ lastDirtyTimestamp> 
      <actionTypeが> ADDED </ actionTypeが> 
    </インスタンス> 
  </アプリケーション> 
</アプリケーション>                                                                                               

あなたはユーレカクライアントが正常にサーバーに登録している見ることができます。

おすすめ

転載: www.cnblogs.com/idoljames/p/11622343.html