KYLIN-2589 fix MessageDigest not thread safe in KylinAuthenticationProvider
Project: http://git-wip-us.apache.org/repos/asf/kylin/repo Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/ce09d6c7 Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/ce09d6c7 Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/ce09d6c7 Branch: refs/heads/KYLIN-2606 Commit: ce09d6c7f027cdc55c10fb96ea21a70c4002f244 Parents: fd3bfed Author: shaofengshi <shaofeng...@apache.org> Authored: Mon May 22 11:24:38 2017 +0800 Committer: shaofengshi <shaofeng...@apache.org> Committed: Mon May 22 11:25:13 2017 +0800 ---------------------------------------------------------------------- .../security/KylinAuthenticationProvider.java | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kylin/blob/ce09d6c7/server-base/src/main/java/org/apache/kylin/rest/security/KylinAuthenticationProvider.java ---------------------------------------------------------------------- diff --git a/server-base/src/main/java/org/apache/kylin/rest/security/KylinAuthenticationProvider.java b/server-base/src/main/java/org/apache/kylin/rest/security/KylinAuthenticationProvider.java index d0dd06a..ffe9811 100644 --- a/server-base/src/main/java/org/apache/kylin/rest/security/KylinAuthenticationProvider.java +++ b/server-base/src/main/java/org/apache/kylin/rest/security/KylinAuthenticationProvider.java @@ -18,10 +18,9 @@ package org.apache.kylin.rest.security; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Arrays; - +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; +import org.apache.kylin.common.util.ByteArray; import org.apache.kylin.rest.service.UserService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,26 +54,21 @@ public class KylinAuthenticationProvider implements AuthenticationProvider { //Embedded authentication provider private AuthenticationProvider authenticationProvider; - MessageDigest md = null; + private HashFunction hf = null; public KylinAuthenticationProvider(AuthenticationProvider authenticationProvider) { super(); Assert.notNull(authenticationProvider, "The embedded authenticationProvider should not be null."); this.authenticationProvider = authenticationProvider; - try { - md = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException("Failed to init Message Digest ", e); - } + hf = Hashing.murmur3_128(); } @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { Authentication authed = null; Cache userCache = cacheManager.getCache("UserCache"); - md.reset(); - byte[] hashKey = md.digest((authentication.getName() + authentication.getCredentials()).getBytes()); - String userKey = Arrays.toString(hashKey); + byte[] hashKey = hf.hashString(authentication.getName() + authentication.getCredentials()).asBytes(); + ByteArray userKey = new ByteArray(hashKey); Element authedUser = userCache.get(userKey); if (null != authedUser) {