Spring Boot安全性:编码后的密码看起来不像BCrypt

时间:2018-10-21 12:45:54

标签: mysql spring-boot spring-security login bcrypt

我最近遵循了有关春季启动安全性的身份验证和授权教程,我认为在使用sql时我迷路了。尽管它没有显示任何错误,即使我输入了正确的用户名和密码,它仍然显示错误的凭据。这是我的代码:

UserDetailsS​​erviceImpl.java

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        WebUser appUser = this.appUserDAO.findUserAccount(userName);

        if (appUser == null) {
            System.out.println("User not found! " + userName);
            throw new UsernameNotFoundException("User " + userName + " was not found in the database");
        }

        System.out.println("Found User: " + appUser);

        // [ROLE_USER, ROLE_ADMIN,..]
        List<String> roleNames = this.appRoleDAO.getRoleNames(appUser.getId());

        List<GrantedAuthority> grantList = new ArrayList<GrantedAuthority>();
        if (roleNames != null) {
            for (String role : roleNames) {
                // ROLE_USER, ROLE_ADMIN,..
                GrantedAuthority authority = new SimpleGrantedAuthority(role);
                grantList.add(authority);
            }
        }

        UserDetails userDetails = (UserDetails) new User(appUser.getUsername(), //
                appUser.getPass(), grantList);

        return userDetails;
    }

WebUserMapper.java

public static final String BASE_SQL //
= "Select u.Id, u.Username, u.Pass From User u ";

@Override
public WebUser mapRow(ResultSet rs, int rowNum) throws SQLException {

    Long userId = rs.getLong("Id");
    String userName = rs.getString("Username");
    String encrytedPassword = rs.getString("Pass");

    return new WebUser(userId, userName, encrytedPassword);
}

WebSecurityConfig.java

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder;
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

        // Setting Service to find User in the database.
        // And Setting PassswordEncoder
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());     

    }

这是我的数据库:

enter image description here

它返回此错误:

Found User: test/$2y$12$MemyV61IuKV7SAntLOEVqeLPPvl2snXjQwlk3RSFDmn5eWrRcHwxm
2018-10-21 20:32:57.110  WARN 1300 --- [nio-8080-exec-3] o.s.s.c.bcrypt.BCryptPasswordEncoder     : Encoded password does not look like BCrypt

密码是123。即使我在密码中输入“ 123”,我也不知道为什么它不起作用。希望您能为我提供帮助。非常感谢

1 个答案:

答案 0 :(得分:1)

尝试使用bcrypt编码器在服务器端对client_secret进行编码。