簡體   English   中英

在生成新的密碼哈希時,如何在應用程序的容器中使用相同的憑據處理程序配置?

[英]How can I utilize the same credential handler configuration for my application's container in generating new password hashes?

我為Web應用程序定義的上下文類似於

<Context>
    <Realm className="org.apache.catalina.realm.JDBCRealm" driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://greensuite.database.windows.net:1433;database=greensuite_db;user=greensuiteapp@greensuite;password=K1B&amp;i9i8*1id^dUzhsv^;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;" userTable="[User]" userNameCol="email" userCredCol="password" userRoleTable="UserGlobalRole" roleNameCol="role">
       <CredentialHandler className="org.apache.catalina.realm.MessageDigestCredentialHandler" encoding="UTF-8" algorithm="SHA-512" iterations="1000000" saltLength="20" />
    </Realm>
</Context>

我可以在動作監聽器中執行以下操作:

String passwordHash = credentialHandler.mutate(newPassword);

其中credentialHandler是適用於正在處理的請求的CredentialHandler實例。

關鍵是將配置放在一個地方,而不是在上下文配置中以及在生成和存儲新哈希的代碼中復制配置,而不是在上下文配置中。

基本上,您需要遵循Christopher Schultz 在ApacheCon 2016幻燈片中給出的指示。 假設您不反對在構建過程中引用Tomcat 8.x catalina.jar ,請首先import所需的類:

import javax.servlet.ServletContext;
import org.apache.catalina.CredentialHandler;
import org.apache.catalina.Globals;

然后,獲得CredentialHandler只是使用正確的密鑰來查找ServletContext的屬性的簡單問題:

public static CredentialHandler getCredentialHandler(final ServletContext context) {
  return (CredentialHandler) context.getAttribute(Globals.CREDENTIAL_HANDLER);
}

幻燈片提供了一種使用反射的替代方法,以避免必須在編譯類路徑中包含catalina.jar

請注意 ,這僅在context.xml配置了一個未嵌套的<Realm>時才有效。 例如,如果用LockoutRealm包裝<Realm> ,則查找將僅考慮最外層Realm返回通用CredentialHandler ,而不是深入到包含您配置的CredentialHandlerRealm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM