首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java Web 服務安全進階:WS-Trust 與 WS-SecureConversation 原理與效能提升實戰

     編輯:Java 相關知識

WS-Security 提供企業級 SOAP 安全,但非對稱加密帶來巨大效能開銷。WS-Trust 定義安全權杖服務 (STS) 發行/驗證權杖流程,**WS-SecureConversation** 建立對稱加密安全上下文,消除後續訊息的非對稱加密開銷,大幅提升效能。

核心架構


Client → (1) RST/SCT → STS (與 Service 同端點)
Client ← (2) SCT (Security Context Token) ← STS
Client → (3) Business Request + SCT Reference → Service
Client ← (4) Business Response (對稱加密/簽名) ← Service
  • STS (Security Token Service):實作 WS-Trust 介面的 Web Service,負責發行/續約/取消/驗證權杖
  • SCT:包含共享祕密衍生參數的權杖,雙方以此衍生對稱金鑰
  • Bootstrap Policy:Client-STS 通道用非對稱加密 (X.509 憑證) 保護
  • Secure Conversation Policy:Client-Service 通道用對稱加密 (衍生金鑰) 保護

WS-Trust 訊息流程範例

Request Security Token (RST)

<wst:RequestSecurityToken>
  <wst:RequestType>https://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue</wst:RequestType>
  <wsp:AppliesTo><wsa:EndpointReference><wsa:Address>https://service/endpoint</wsa:Address></wsa:EndpointReference></wsp:AppliesTo>
  <wst:Lifetime><wsu:Created>2010-05-12T10:33:22.774Z</wsu:Created><wsu:Expires>2010-05-12T10:38:22.774Z</wsu:Expires></wst:Lifetime>
  <wst:TokenType>https://docs.oasis-open.org/ws-sx/ws-secureconversation/200512/sct</wst:TokenType>
  <wst:KeySize>128</wst:KeySize>
  <wst:Entropy><wst:BinarySecret Type=".../Nonce">kIYFB/u430k3PlOPfUtJ5A==</wst:BinarySecret></wst:Entropy>
  <wst:ComputedKeyAlgorithm>https://docs.oasis-open.org/ws-sx/ws-trust/200512/CK/PSHA1</wst:ComputedKeyAlgorithm>
</wst:RequestSecurityToken>

Response (RSTR) 包含 SCT

<wst:RequestedSecurityToken>
  <wsc:SecurityContextToken wsu:Id="sctId-A167...">
    <wsc:Identifier>A167EB2B526E0894DA12736604029098</wsc:Identifier>
  </wsc:SecurityContextToken>
</wst:RequestedSecurityToken>
<wst:RequestedProofToken><wst:ComputedKeyAlgorithm>.../CK/PSHA1</wst:ComputedKeyAlgorithm></wst:RequestedProofToken>
<wst:Entropy><wst:BinarySecret>DpkK6qcELTO8dlPdDHMi2A==</wst:BinarySecret></wst:Entropy>

雙方 Entropy 經 PSHA1 衍生共享祕密,再衍生實際對稱金鑰 (DerivedKeyToken)。

WS-Policy 設定關鍵

<wsp:Policy wsu:Id="SecConv">
  <wsp:ExactlyOne><wsp:All>
    <sp:SymmetricBinding>  <!-- 業務通道對稱加密 -->
      <wsp:Policy>
        <sp:ProtectionToken>
          <wsp:Policy>
            <sp:SecureConversationToken sp:IncludeToken=".../AlwaysToRecipient">
              <wsp:Policy>
                <sp:RequireDerivedKeys/>
                <sp:BootstrapPolicy>  <!-- STS 通道非對稱加密 -->
                  <wsp:Policy>
                    <sp:AsymmetricBinding>...</sp:AsymmetricBinding>
                    <sp:SignedParts>...</sp:SignedParts>
                    <sp:Trust13><wsp:Policy><sp:MustSupportIssuedTokens/><sp:RequireClientEntropy/><sp:RequireServerEntropy/></wsp:Policy></sp:Trust13>
                  </wsp:Policy>
                </sp:BootstrapPolicy>
              </wsp:Policy>
            </sp:SecureConversationToken>
          </wsp:Policy>
        </sp:ProtectionToken>
        <sp:AlgorithmSuite><wsp:Policy><sp:Basic128Rsa15/></wsp:Policy></sp:AlgorithmSuite>
      </wsp:Policy>
    </sp:SymmetricBinding>
    <sp:EncryptedParts><sp:Body/></sp:EncryptedParts>
  </wsp:All></wsp:ExactlyOne>
</wsp:Policy>

實際業務訊息 (含 DerivedKeyToken)

<wsse:Security>
  <wsc:SecurityContextToken wsu:Id="sctId-..."><wsc:Identifier>A167...</wsc:Identifier></wsc:SecurityContextToken>
  <wsc:DerivedKeyToken wsu:Id="derivedKeyId-9">
    <wsse:SecurityTokenReference><wsse:Reference URI="#sctId-..."/></wsse:SecurityTokenReference>
    <wsc:Offset>0</wsc:Offset><wsc:Length>16</wsc:Length><wsc:Nonce>AyUGKYBNNQstD9EmZUJqlA==</wsc:Nonce>
  </wsc:DerivedKeyToken>
  <ds:Signature>...<ds:KeyInfo><wsse:SecurityTokenReference><wsse:Reference URI="#derivedKeyId-9"/></wsse:SecurityTokenReference></ds:KeyInfo></ds:Signature>
  <xenc:ReferenceList><xenc:DataReference URI="#EncDataId-12"/></xenc:ReferenceList>
</wsse:Security>
<xenc:EncryptedData>...<xenc:EncryptionMethod Algorithm=".../aes128-cbc"/>...</xenc:EncryptedData>

效能效益

  • 建立上下文:1 次非對稱加密交換 (約 2-3 倍對稱加密耗時)
  • 後續 N 次業務呼叫:全程對稱加密 (AES)、衍生金鑰簽名,CPU 開銷降低 80%+
  • 適用場景:長會話、高頻呼叫、B2B 整合、金融交易

三大 Java 堆棧支援現況

堆棧WS-SecureConversation 支援配置複雜度
Apache CXF原生完整支援,Policy 驅動中
Metro (GlassFish)原生完整支援,WSIT 整合中
Apache Axis2Rampart 模組支援,配置較繁瑣高

下一篇將深入三大堆棧實測效能數據與配置細節。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved