我最近学习了一个关于授权和身份验证(登录)的教程,一切都很好,但是每次输入用户名和密码都会显示错误的凭据。这是我的密码。
Web安全配置.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());
}
}
用户dao.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”,它返回了错误的凭据。非常感谢
暂无答案!
目前还没有任何答案,快来回答吧!