[英]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&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
,而不是深入到包含您配置的CredentialHandler
的Realm
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.