当前位置: 首页>>代码示例>>Java>>正文


Java BCryptPasswordEncoder.matches方法代码示例

本文整理汇总了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);
}
 
开发者ID:myliang,项目名称:fish-admin,代码行数:5,代码来源:User.java

示例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());
    }
}
 
开发者ID:jaschenk,项目名称:Your-Microservice,代码行数:74,代码来源:YourMSAuthenticationManager.java


注:本文中的org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.matches方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。