为什么我能够使用Java Spring Security中的密码哈希进行身份验证

时间:2017-10-09 06:14:58

标签: java spring authentication spring-security spring-boot-starter

我正在使用listItems = ['foo', 'bar', 'bak'] listLetter = [] myDictionary = {} for item in listItems: for letter in item: listLetter.append(letter) myDictionary['f'] = 5 print myDictionary 。我将spring-boot-starter-security配置为使用WebSecurityConfigation提供程序和DaoAuthenticationProvider进行身份验证。此外,BCryptPasswordEncoder实现返回UserDetailsService对象,并将User字段设置为实际哈希值。

似乎工作正常。但是我注意到我可以使用密码哈希来成功进行身份验证。

例如,密码本身就是生成的UUID password,其编码为51a80a6a-8618-4583-98d2-d77d103a62c6

完整的网络安全配置:

$2a$10$u4OSZf7B9yJvQ5UYNNpy7O4f3g0gfUMl2Xmm3h282W.3emSN3WqxO

为什么我能用两个字符串进行身份验证?我做错了什么,或者这是预期的还是一些配置?我无法在文档中找到任何内容。

2 个答案:

答案 0 :(得分:1)

我想这件事发生是因为根据您的配置,您实际上得到了两个DaoAuthenticationProvider。一个是明确的,由您配置,一个是隐式的,当您调用auth.userDetailsService(userDetailsService);时,它是在引擎盖下配置的,而对于这个隐式提供者,您没有设置密码编码器。

试试这个:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService).passwordEncoder(new BCryptPasswordEncoder()); 
    auth.inMemoryAuthentication().withUser("user").password("password").roles("SUPER", "BASIC");
}

并删除您手动配置的提供商 - 似乎您实际上并不需要它。

希望它有所帮助。

答案 1 :(得分:-2)

最好的方法是连接调试器。使用PasswordEncoder匹配密码的实际逻辑是additionalAuthenticationChecks方法[{1}}

DaoAuthenticationProvider

此代表 protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { Object salt = null; if (this.saltSource != null) { salt = this.saltSource.getSalt(userDetails); } if (authentication.getCredentials() == null) { logger.debug("Authentication failed: no credentials provided"); throw new BadCredentialsException(messages.getMessage( "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails); } String presentedPassword = authentication.getCredentials().toString(); if (!passwordEncoder.isPasswordValid(userDetails.getPassword(), presentedPassword, salt)) { logger.debug("Authentication failed: password does not match stored value"); throw new BadCredentialsException(messages.getMessage( "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"), userDetails); } }

BCryptPasswordEncoder