密码没有在春季安全性中编码
密码没有编码。它保存与用户在登录时输入的保存一样。我尝试使用bcryptpasswordencoder,但它不起作用。好像我在某个地方犯了一个错误。请帮忙!
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
/**
* Password Encoder Bean
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* Authentication Manager Bean.
* It is required for login process
*/
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;
/**
* Method for configuring the authentication service based on user details
* provided
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(this.passwordEncoder());
}
/**
* Method for configuring HTTP requests for the application
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/generate-token").permitAll()
.antMatchers(HttpMethod.POST).permitAll()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated().and().exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// Check JWT authentication token before any request
http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
}
这是安全配置类。我怀疑错误仅在此课程中
The password is not getting encoded. It is saved as same as the user is entering it while signing in. I have tried using BCryptPasswordEncoder but it is not working. Seems like I am making a mistake somewhere. Please help!
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
/**
* Password Encoder Bean
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
/**
* Authentication Manager Bean.
* It is required for login process
*/
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Autowired
private JwtAuthenticationFilter jwtAuthenticationFilter;
/**
* Method for configuring the authentication service based on user details
* provided
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(this.passwordEncoder());
}
/**
* Method for configuring HTTP requests for the application
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/generate-token").permitAll()
.antMatchers(HttpMethod.POST).permitAll()
.antMatchers(HttpMethod.OPTIONS).permitAll()
.anyRequest().authenticated().and().exceptionHandling().authenticationEntryPoint(unauthorizedHandler)
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// Check JWT authentication token before any request
http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
}
This is the security configuration class. I suspect error is here only in this class maybe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
SecurityConfig
还可以。我认为您误解了
auth.userdetailsservice(userDetailsService)的用法.passwordencoder(this.passwordencoder());
>这里。
此代码将在授权和身份验证时在密码上应用
bcryptpasswordencoder
,而不是将用户存储到DB中时。将用户的密码持续在数据库上时,您应该手动编码密码。
这样的事情:
Your
SecurityConfig
is ok.I think you misunderstand the usage of
auth.userDetailsService(userDetailsService).passwordEncoder(this.passwordEncoder());
here.
This code will apply
BCryptPasswordEncoder
on the password when authorization and authentication, not when you store your user into DB.You should encode your users' passwords by hand when persisting them on the DB.
Something like this: