Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BCrypt: Empty Encoded Password with Spring Security

A CODE SAMPLE TO HELP FIX THE PROBLEM: https://github.com/Suwappertjes/SpringSample

Problem

When trying to implement jwt-security in a Spring Boot application, I run into the following problem:

When I try to login with x-www-form-urlencoded through Postman I get a "Bad client credentials" error, whilst I know the credentials to be correct.

When I look in my log, I see that BCrypt gave a "Empty Encoded Password" warning. This is odd, considering I see correctly encrypted passwords in the database when I look at it through the MySQL interpreter.

Info

I am using Hibernate to build a MySQL database.

compile 'org.springframework.security.oauth:spring-security-oauth2:2.3.6.RELEASE'

compile 'org.springframework.security:spring-security-jwt:1.0.10.RELEASE'

compile 'org.springframework.boot:spring-boot-starter-data-jpa'

Java 1.8.0_212

What I tried

Before starting to implement security, the controllers, repositories, and MySQL database were all functioning correctly.

When I search for this problem online, some people suggest it has to do with the "loadUserByUsername" function. But when I debug that function I notice it is not being called at all.

I also tried allowing every single path in my program AND disabling crsf in case it had something to do with access rights, but both didn't change anything. (http.requestMatchers.andMatcher("/**").permitAll().and().csrf().disable();)

Update: When putting NO users in the database, I still get the same error.

Some code:

The loadUserByUsername method:

@Override
public UserDetails loadUserByUsername(String userName) {
    return userRepository.findByUsername(userName)
            .map(user -> new User(
                        user.getUsername(),
                        user.getPassword(),
                        UserRoleAuthority.getAuthorities(user.getRoles())
                ))
            .orElseThrow(() -> new UsernameNotFoundException("Unknown user: " + userName));

}

The authenticationprovider and passwordencoder:

@Bean
public DaoAuthenticationProvider getAuthenticationProvider() {
    DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
    authenticationProvider.setUserDetailsService(userDetailsService);
    authenticationProvider.setPasswordEncoder(passwordEncoder());
    return authenticationProvider;
}

@Bean
PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

The signing key:

@Bean
public JwtAccessTokenConverter jwtTokenEnhancer() {
    JwtAccessTokenConverter result = new JwtAccessTokenConverter();
    result.setSigningKey(signingKey);
    return result;
}

Adding a new user:

            userRepository.save(new User()
                            .withUsername("test")
                            .withPassword(passwordEncoder.encode("password"))
                            .withFirst("Admin")
                            .withLast("Nator")
                            .withEmail("[email protected]")
                            .withRole(new HashSet<>(Arrays.asList(Role.Admin, Role.NotAdmin)))
            );

And the Http configuration:

@Override
public void configure(HttpSecurity http) throws Exception {

    http.requestMatchers()
            .antMatchers("/iungo/*/**")
            .and()
            .authorizeRequests()
            .antMatchers("/iungo/system/**")
            .permitAll()
            .antMatchers("/iungo/**");
}

@Override
public void configure(HttpSecurity http) throws Exception {
    // We don't use this endpoint but we have to define it anyway in order to not enable web security on the whole application.
    http
            .userDetailsService(userDetailsService)
            .requestMatchers()
            .antMatchers("/oauth/authorize");
}

And finally, my console:

2019-06-12 10:29:58.460  INFO 25207 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.9.Final}
2019-06-12 10:29:58.461  INFO 25207 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-06-12 10:29:58.532  INFO 25207 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-06-12 10:29:58.599  INFO 25207 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-06-12 10:29:59.092  INFO 25207 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@c30f26d'
2019-06-12 10:29:59.098  INFO 25207 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-06-12 10:29:59.382  WARN 25207 --- [           main] o.s.s.o.p.t.s.JwtAccessTokenConverter    : Unable to create an RSA verifier from verifierKey (ignoreable if using MAC)
2019-06-12 10:29:59.595  INFO 25207 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/oauth/token'], Ant [pattern='/oauth/token_key'], Ant [pattern='/oauth/check_token']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@56a72887, org.springframework.security.web.context.SecurityContextPersistenceFilter@1ddba7a0, org.springframework.security.web.header.HeaderWriterFilter@7adbec34, org.springframework.security.web.authentication.logout.LogoutFilter@296bfddb, org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter@22ab1b8a, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@54033a65, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@7dfec0bc, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@42734b71, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3c8dea0b, org.springframework.security.web.session.SessionManagementFilter@6fe9c048, org.springframework.security.web.access.ExceptionTranslationFilter@526fc044, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@690e4b00]
2019-06-12 10:29:59.600  INFO 25207 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/iungo/*/**']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1978b0d5, org.springframework.security.web.context.SecurityContextPersistenceFilter@69a3bf40, org.springframework.security.web.header.HeaderWriterFilter@3186f8f5, org.springframework.security.web.authentication.logout.LogoutFilter@a4dcede, org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationProcessingFilter@760c777d, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@2c731a16, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@2a341e3d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@6556471b, org.springframework.security.web.session.SessionManagementFilter@467cd4b9, org.springframework.security.web.access.ExceptionTranslationFilter@3f3f554f, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@d0e4972]
2019-06-12 10:29:59.603  INFO 25207 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/oauth/authorize']]], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6cf3b3d7, org.springframework.security.web.context.SecurityContextPersistenceFilter@462f8fe9, org.springframework.security.web.header.HeaderWriterFilter@24f2608b, org.springframework.security.web.csrf.CsrfFilter@713497cd, org.springframework.security.web.authentication.logout.LogoutFilter@56193e3a, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@3c6fc4cd, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@b2e1df3, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2e785b28, org.springframework.security.web.session.SessionManagementFilter@12a9e864, org.springframework.security.web.access.ExceptionTranslationFilter@4b762988]
2019-06-12 10:29:59.731  INFO 25207 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-12 10:29:59.902  INFO 25207 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-12 10:29:59.903  INFO 25207 --- [           main] nl.highway.iungomain.Application         : Started Application in 2.937 seconds (JVM running for 3.345)
2019-06-12 10:29:59.923  INFO 25207 --- [           main] o.h.h.i.QueryTranslatorFactoryInitiator  : HHH000397: Using ASTQueryTranslatorFactory
2019-06-12 10:30:12.550  INFO 25207 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-06-12 10:30:12.550  INFO 25207 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-06-12 10:30:12.554  INFO 25207 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
2019-06-12 10:30:12.632  WARN 25207 --- [nio-8080-exec-1] o.s.s.c.bcrypt.BCryptPasswordEncoder     : Empty encoded password
like image 822
Suwappertjes Avatar asked Aug 24 '26 15:08

Suwappertjes


1 Answers

The bug was in my code under UserPrincipal in Security, where my getters returned null.

public String getEmail() {
    return null;
}

public String getPassword() {
    return null;
}

Here is what it should be:

    public String getEmail() {
    return email;
}

public String getPassword() {
    return password;
}
like image 111
l00pinfinity Avatar answered Aug 27 '26 05:08

l00pinfinity