我最近遵循了有关授权和认证(登录)的教程,并且一切正常,但是每次输入用户名和密码时,都会显示错误的凭据错误。这是我的代码。
WebSecurityConfig.java
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;
@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());
}
}
UserDAO.java
@Repository
@Transactional
public class UserDAO extends JdbcDaoSupport {
@Autowired
public UserDAO(DataSource dataSource) {
this.setDataSource(dataSource);
}
public WebUser findUserAccount(String userName) {
// Select .. from App_User u Where u.User_Name = ?
String sql = WebUserMapper.BASE_SQL + " where u.Username = ? ";
Object[] params = new Object[] { userName };
WebUserMapper mapper = new WebUserMapper();
try {
WebUser userInfo = this.getJdbcTemplate().queryForObject(sql, params, mapper);
return userInfo;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
}
WebUserMapper.java
public class WebUserMapper implements RowMapper<WebUser> {
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);
}
}
EncryptedPasswordUtils.java
public class EncrytedPasswordUtils {
public static String encrytePassword(String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder.encode(password);
}
}
这是我的数据库:
错误:
Found User: test/$2y$12$nUaUkOYaz0hAsgSippzpdeYPPqFsx3zKT9/H5vojD.YBBlZhLo4RG
2018-10-22 09:53:20.054 WARN 11316 --- [nio-8080-exec-6] o.s.s.c.bcrypt.BCryptPasswordEncoder : Encoded password does not look like BCrypt
希望您能帮助我。可以找到用户名,并且每次我尝试使用不在数据库中的用户名和密码时,都会显示错误。即使我输入正确的密码“ 123”,它也会返回错误的凭据。非常感谢