下面列出了怎么用org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder的API类实例代码及写法,或者点击链接到github查看源代码。
private void addSessionContextToLogging() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String tokenValue = "EMPTY";
if (authentication != null && !Strings.isNullOrEmpty(authentication.getDetails().toString())) {
MessageDigestPasswordEncoder encoder = new MessageDigestPasswordEncoder("SHA-1");
tokenValue = encoder.encodePassword(authentication.getDetails().toString(), "not_so_random_salt");
}
MDC.put(TOKEN_SESSION_KEY, tokenValue);
String userValue = "EMPTY";
if (authentication != null && !Strings.isNullOrEmpty(authentication.getPrincipal().toString())) {
userValue = authentication.getPrincipal().toString();
}
MDC.put(USER_SESSION_KEY, userValue);
}
public static String encodePassword(String clearTextPassword) {
return new MessageDigestPasswordEncoder("MD5", true).encodePassword(clearTextPassword, null);
}
private static String encode(String password) {
return new MessageDigestPasswordEncoder("MD5", true).encodePassword(password, null);
}
/**
* Gets the MD5 hash and encodes it in Base 64 notation
*
* @param clearTextPassword
* @return
* @throws NoSuchAlgorithmException
*/
public static String getEncodedPassword(String clearTextPassword) {
return new MessageDigestPasswordEncoder("MD5", true).encodePassword(clearTextPassword, null);
}