本文整理匯總了Java中org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.matches方法的典型用法代碼示例。如果您正苦於以下問題:Java BCryptPasswordEncoder.matches方法的具體用法?Java BCryptPasswordEncoder.matches怎麽用?Java BCryptPasswordEncoder.matches使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
的用法示例。
在下文中一共展示了BCryptPasswordEncoder.matches方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: validatePassword
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; //導入方法依賴的package包/類
public boolean validatePassword(String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder.matches(password, this.encryptedPassword);
}
示例2: authenticate
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; //導入方法依賴的package包/類
@Override
public Authentication authenticate(Authentication a) throws AuthenticationException {
/**
* Performing Timings of Authentication Interactions...
*/
TimeDuration overall_duration = new TimeDuration();
TimeDuration db_duration = new TimeDuration();
TimeDuration pw_duration = new TimeDuration();
overall_duration.start();
String remoteIp = null;
if (a.getDetails() != null) {
WebAuthenticationDetails details = (WebAuthenticationDetails) a.getDetails();
remoteIp = details.getRemoteAddress();
}
String userName = null;
String rawPass;
try {
db_duration.start();
userName = a.getPrincipal().toString();
YourMicroserviceUserDetails userDetails =
(YourMicroserviceUserDetails) detailsService.loadUserByUsername(userName);
db_duration.stop();
pw_duration.start();
rawPass = a.getCredentials().toString();
String storedHashedPass = userDetails.getPassword();
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(YourMicroserviceSecurityConstants.BCRYPT_STRENGTH_SETTING);
if (!encoder.matches(rawPass, storedHashedPass)) {
pw_duration.stop();
logger.info("{} IP:[{}]", USERNAME_OR_PASSWORD_IS_INVALID_MESSAGE, remoteIp);
throw new BadCredentialsException(USERNAME_OR_PASSWORD_IS_INVALID_MESSAGE);
}
pw_duration.stop();
if (!userDetails.isEnabled()) {
if (!userDetails.isAccountNonLocked())
throw new LockedException(ACCOUNT_IS_LOCKED_MESSAGE) {
};
if (!userDetails.isAccountNonExpired())
throw new AccountExpiredException(ACCOUNT_IS_EXPIRED_MESSAGE) {
};
throw new DisabledException(ACCOUNT_IS_DISABLED_MESSAGE) {
};
}
/**
* Clear all Memory Constructs
*/
rawPass = null;
storedHashedPass = null;
userDetails.shredCredentials();
/**
* Return Authentication Token...
*/
return new YourMSAuthenticationToken(userDetails);
} catch (UsernameNotFoundException ex) {
logger.warn("Entity:[" + userName + "] Not Found, Ignoring!");
throw new BadCredentialsException(USERNAME_OR_PASSWORD_IS_INVALID_MESSAGE);
} finally {
/**
* Report our Authentication Timings
*/
overall_duration.stop();
logger.info("Authentication Timings: DB:" + db_duration.getElapsedtoString() +
", PW: " + pw_duration + ", Overall: " +
overall_duration.getElapsedtoString());
}
}