Unable to authenticate using after implementing Bcrypt PasswordEncoder












1















after implementing Bcrypt Password Encoder, I'm unable to authenticate (invalid credentials). Here is the user I'm adding:



userRepository.save(new User("First", "Last", "user", "user" , "email@email.com", "12345", superRoles));


When I view the JSON for the page, I see that the password is hashed. However, when I try to enter the password "user", I am unable to authenticate:



..."password": "$2a$10$ZwUxEGVDAgI4qgkas0bEO.BmU1WrMXk1zQA5Jc70m.e6reiL3M7BG"...


Can anyone spot if I'm doing anything wrong? Code is posted below. Thank you in advance!



User class:



@Entity
public class User {
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
private long userId;
private String userFirstName;
private String userLastName;
private String username;
private String password;
private String userPhone;
private String userEmail;
//others, such as List for roles, etc.

public User() { }

public User(String userFirstName, String userLastName, String username, String password, String userPhone, String userEmail, Map<String, Boolean> userRoles) {
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.username = username;
setPassword(password);
this.userPhone = userPhone;
this.userEmail = userEmail;
this.userRoles = userRoles;
}
public void setPassword(String password) {
this.password = PASSWORD_ENCODER.encode(password);
}
// other getters and setters
}


WebSecurityConfiguration:



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
DetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(User.PASSWORD_ENCODER);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login"
).permitAll()
...
// taken out for brevity
}
}


DetailsService:



@Component
public class DetailsService implements UserDetailsService {
@Autowired
UserRepository users;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = users.findByUsername(username);
// Roles are implemented in the entity in a Map<String, Boolean>, e.g. "ROLE_ADMIN" : true, to help with easily setting new permissions on the front end
List<String> roles = new ArrayList<>();

for (Map.Entry<String, Boolean> entry : user.getUserRoles().entrySet()) {
if (entry.getValue().equals(true)) {
roles.add(entry.getKey());
}
}

String permissions = roles.toArray(new String[roles.size()]);

if (user == null) {
throw new UsernameNotFoundException(username + " was not found");
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
AuthorityUtils.createAuthorityList(permissions)
);
}
}


Edit 2:



@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login",
"/home",
"/visitor-area",
"/site.css",
"/app.js",
"/create-account",
"/css/*",
"/saveUser",
"/users"
).permitAll()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/user-dashboard")
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/home")
.and()
.csrf().disable();
}









share|improve this question

























  • I tried your code (simplified it a bit by removing the roles and such) and your code works fine for me without the MD5PasswordEncoder. Make sure that the repository.save() method is actually called and that your security configuration (the part you've taken out) works correctly.

    – g00glen00b
    Nov 21 '18 at 14:23











  • Indeed, everything else should be perfect - see edit 2. Everything works fine without bcrypt, but as soon as I implement it it stops authenticating. Would it make any difference if I'm loading in my users via a class that implements ApplicationRunner? As I said, it works fine without bcrypt.. Been at it for days :)

    – Harry Brown
    Nov 21 '18 at 14:36











  • Well, your original code (with BCrypt) works for me as well. However, I don't understand why you have both a form login and basic authentication. Which of these two do you use? And why do you have authorizeRequests() multiple times?

    – g00glen00b
    Nov 21 '18 at 14:41













  • Thanks! I'm learning as I'm going and working with different snippets from different sources. With all of that removed and just using formLogin(), it still doesn't work so I'm at a bit of a loss as to why it works for you but not me. Have updated the question to reflect the new configure method.

    – Harry Brown
    Nov 21 '18 at 14:58













  • Also @g00glen00b, when you mentioned that I should make sure the repository.save() method is actually called - that's done in my DatabaseLoader class that implements ApplicationRunner. Does it need to be done anywhere else?

    – Harry Brown
    Nov 21 '18 at 15:01
















1















after implementing Bcrypt Password Encoder, I'm unable to authenticate (invalid credentials). Here is the user I'm adding:



userRepository.save(new User("First", "Last", "user", "user" , "email@email.com", "12345", superRoles));


When I view the JSON for the page, I see that the password is hashed. However, when I try to enter the password "user", I am unable to authenticate:



..."password": "$2a$10$ZwUxEGVDAgI4qgkas0bEO.BmU1WrMXk1zQA5Jc70m.e6reiL3M7BG"...


Can anyone spot if I'm doing anything wrong? Code is posted below. Thank you in advance!



User class:



@Entity
public class User {
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
private long userId;
private String userFirstName;
private String userLastName;
private String username;
private String password;
private String userPhone;
private String userEmail;
//others, such as List for roles, etc.

public User() { }

public User(String userFirstName, String userLastName, String username, String password, String userPhone, String userEmail, Map<String, Boolean> userRoles) {
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.username = username;
setPassword(password);
this.userPhone = userPhone;
this.userEmail = userEmail;
this.userRoles = userRoles;
}
public void setPassword(String password) {
this.password = PASSWORD_ENCODER.encode(password);
}
// other getters and setters
}


WebSecurityConfiguration:



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
DetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(User.PASSWORD_ENCODER);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login"
).permitAll()
...
// taken out for brevity
}
}


DetailsService:



@Component
public class DetailsService implements UserDetailsService {
@Autowired
UserRepository users;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = users.findByUsername(username);
// Roles are implemented in the entity in a Map<String, Boolean>, e.g. "ROLE_ADMIN" : true, to help with easily setting new permissions on the front end
List<String> roles = new ArrayList<>();

for (Map.Entry<String, Boolean> entry : user.getUserRoles().entrySet()) {
if (entry.getValue().equals(true)) {
roles.add(entry.getKey());
}
}

String permissions = roles.toArray(new String[roles.size()]);

if (user == null) {
throw new UsernameNotFoundException(username + " was not found");
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
AuthorityUtils.createAuthorityList(permissions)
);
}
}


Edit 2:



@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login",
"/home",
"/visitor-area",
"/site.css",
"/app.js",
"/create-account",
"/css/*",
"/saveUser",
"/users"
).permitAll()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/user-dashboard")
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/home")
.and()
.csrf().disable();
}









share|improve this question

























  • I tried your code (simplified it a bit by removing the roles and such) and your code works fine for me without the MD5PasswordEncoder. Make sure that the repository.save() method is actually called and that your security configuration (the part you've taken out) works correctly.

    – g00glen00b
    Nov 21 '18 at 14:23











  • Indeed, everything else should be perfect - see edit 2. Everything works fine without bcrypt, but as soon as I implement it it stops authenticating. Would it make any difference if I'm loading in my users via a class that implements ApplicationRunner? As I said, it works fine without bcrypt.. Been at it for days :)

    – Harry Brown
    Nov 21 '18 at 14:36











  • Well, your original code (with BCrypt) works for me as well. However, I don't understand why you have both a form login and basic authentication. Which of these two do you use? And why do you have authorizeRequests() multiple times?

    – g00glen00b
    Nov 21 '18 at 14:41













  • Thanks! I'm learning as I'm going and working with different snippets from different sources. With all of that removed and just using formLogin(), it still doesn't work so I'm at a bit of a loss as to why it works for you but not me. Have updated the question to reflect the new configure method.

    – Harry Brown
    Nov 21 '18 at 14:58













  • Also @g00glen00b, when you mentioned that I should make sure the repository.save() method is actually called - that's done in my DatabaseLoader class that implements ApplicationRunner. Does it need to be done anywhere else?

    – Harry Brown
    Nov 21 '18 at 15:01














1












1








1








after implementing Bcrypt Password Encoder, I'm unable to authenticate (invalid credentials). Here is the user I'm adding:



userRepository.save(new User("First", "Last", "user", "user" , "email@email.com", "12345", superRoles));


When I view the JSON for the page, I see that the password is hashed. However, when I try to enter the password "user", I am unable to authenticate:



..."password": "$2a$10$ZwUxEGVDAgI4qgkas0bEO.BmU1WrMXk1zQA5Jc70m.e6reiL3M7BG"...


Can anyone spot if I'm doing anything wrong? Code is posted below. Thank you in advance!



User class:



@Entity
public class User {
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
private long userId;
private String userFirstName;
private String userLastName;
private String username;
private String password;
private String userPhone;
private String userEmail;
//others, such as List for roles, etc.

public User() { }

public User(String userFirstName, String userLastName, String username, String password, String userPhone, String userEmail, Map<String, Boolean> userRoles) {
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.username = username;
setPassword(password);
this.userPhone = userPhone;
this.userEmail = userEmail;
this.userRoles = userRoles;
}
public void setPassword(String password) {
this.password = PASSWORD_ENCODER.encode(password);
}
// other getters and setters
}


WebSecurityConfiguration:



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
DetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(User.PASSWORD_ENCODER);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login"
).permitAll()
...
// taken out for brevity
}
}


DetailsService:



@Component
public class DetailsService implements UserDetailsService {
@Autowired
UserRepository users;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = users.findByUsername(username);
// Roles are implemented in the entity in a Map<String, Boolean>, e.g. "ROLE_ADMIN" : true, to help with easily setting new permissions on the front end
List<String> roles = new ArrayList<>();

for (Map.Entry<String, Boolean> entry : user.getUserRoles().entrySet()) {
if (entry.getValue().equals(true)) {
roles.add(entry.getKey());
}
}

String permissions = roles.toArray(new String[roles.size()]);

if (user == null) {
throw new UsernameNotFoundException(username + " was not found");
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
AuthorityUtils.createAuthorityList(permissions)
);
}
}


Edit 2:



@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login",
"/home",
"/visitor-area",
"/site.css",
"/app.js",
"/create-account",
"/css/*",
"/saveUser",
"/users"
).permitAll()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/user-dashboard")
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/home")
.and()
.csrf().disable();
}









share|improve this question
















after implementing Bcrypt Password Encoder, I'm unable to authenticate (invalid credentials). Here is the user I'm adding:



userRepository.save(new User("First", "Last", "user", "user" , "email@email.com", "12345", superRoles));


When I view the JSON for the page, I see that the password is hashed. However, when I try to enter the password "user", I am unable to authenticate:



..."password": "$2a$10$ZwUxEGVDAgI4qgkas0bEO.BmU1WrMXk1zQA5Jc70m.e6reiL3M7BG"...


Can anyone spot if I'm doing anything wrong? Code is posted below. Thank you in advance!



User class:



@Entity
public class User {
public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
private long userId;
private String userFirstName;
private String userLastName;
private String username;
private String password;
private String userPhone;
private String userEmail;
//others, such as List for roles, etc.

public User() { }

public User(String userFirstName, String userLastName, String username, String password, String userPhone, String userEmail, Map<String, Boolean> userRoles) {
this.userFirstName = userFirstName;
this.userLastName = userLastName;
this.username = username;
setPassword(password);
this.userPhone = userPhone;
this.userEmail = userEmail;
this.userRoles = userRoles;
}
public void setPassword(String password) {
this.password = PASSWORD_ENCODER.encode(password);
}
// other getters and setters
}


WebSecurityConfiguration:



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
DetailsService userDetailsService;

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService)
.passwordEncoder(User.PASSWORD_ENCODER);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login"
).permitAll()
...
// taken out for brevity
}
}


DetailsService:



@Component
public class DetailsService implements UserDetailsService {
@Autowired
UserRepository users;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = users.findByUsername(username);
// Roles are implemented in the entity in a Map<String, Boolean>, e.g. "ROLE_ADMIN" : true, to help with easily setting new permissions on the front end
List<String> roles = new ArrayList<>();

for (Map.Entry<String, Boolean> entry : user.getUserRoles().entrySet()) {
if (entry.getValue().equals(true)) {
roles.add(entry.getKey());
}
}

String permissions = roles.toArray(new String[roles.size()]);

if (user == null) {
throw new UsernameNotFoundException(username + " was not found");
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
AuthorityUtils.createAuthorityList(permissions)
);
}
}


Edit 2:



@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/login",
"/home",
"/visitor-area",
"/site.css",
"/app.js",
"/create-account",
"/css/*",
"/saveUser",
"/users"
).permitAll()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/user-dashboard")
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/home")
.and()
.csrf().disable();
}






java spring spring-boot bcrypt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:58







Harry Brown

















asked Nov 21 '18 at 13:44









Harry BrownHarry Brown

287




287













  • I tried your code (simplified it a bit by removing the roles and such) and your code works fine for me without the MD5PasswordEncoder. Make sure that the repository.save() method is actually called and that your security configuration (the part you've taken out) works correctly.

    – g00glen00b
    Nov 21 '18 at 14:23











  • Indeed, everything else should be perfect - see edit 2. Everything works fine without bcrypt, but as soon as I implement it it stops authenticating. Would it make any difference if I'm loading in my users via a class that implements ApplicationRunner? As I said, it works fine without bcrypt.. Been at it for days :)

    – Harry Brown
    Nov 21 '18 at 14:36











  • Well, your original code (with BCrypt) works for me as well. However, I don't understand why you have both a form login and basic authentication. Which of these two do you use? And why do you have authorizeRequests() multiple times?

    – g00glen00b
    Nov 21 '18 at 14:41













  • Thanks! I'm learning as I'm going and working with different snippets from different sources. With all of that removed and just using formLogin(), it still doesn't work so I'm at a bit of a loss as to why it works for you but not me. Have updated the question to reflect the new configure method.

    – Harry Brown
    Nov 21 '18 at 14:58













  • Also @g00glen00b, when you mentioned that I should make sure the repository.save() method is actually called - that's done in my DatabaseLoader class that implements ApplicationRunner. Does it need to be done anywhere else?

    – Harry Brown
    Nov 21 '18 at 15:01



















  • I tried your code (simplified it a bit by removing the roles and such) and your code works fine for me without the MD5PasswordEncoder. Make sure that the repository.save() method is actually called and that your security configuration (the part you've taken out) works correctly.

    – g00glen00b
    Nov 21 '18 at 14:23











  • Indeed, everything else should be perfect - see edit 2. Everything works fine without bcrypt, but as soon as I implement it it stops authenticating. Would it make any difference if I'm loading in my users via a class that implements ApplicationRunner? As I said, it works fine without bcrypt.. Been at it for days :)

    – Harry Brown
    Nov 21 '18 at 14:36











  • Well, your original code (with BCrypt) works for me as well. However, I don't understand why you have both a form login and basic authentication. Which of these two do you use? And why do you have authorizeRequests() multiple times?

    – g00glen00b
    Nov 21 '18 at 14:41













  • Thanks! I'm learning as I'm going and working with different snippets from different sources. With all of that removed and just using formLogin(), it still doesn't work so I'm at a bit of a loss as to why it works for you but not me. Have updated the question to reflect the new configure method.

    – Harry Brown
    Nov 21 '18 at 14:58













  • Also @g00glen00b, when you mentioned that I should make sure the repository.save() method is actually called - that's done in my DatabaseLoader class that implements ApplicationRunner. Does it need to be done anywhere else?

    – Harry Brown
    Nov 21 '18 at 15:01

















I tried your code (simplified it a bit by removing the roles and such) and your code works fine for me without the MD5PasswordEncoder. Make sure that the repository.save() method is actually called and that your security configuration (the part you've taken out) works correctly.

– g00glen00b
Nov 21 '18 at 14:23





I tried your code (simplified it a bit by removing the roles and such) and your code works fine for me without the MD5PasswordEncoder. Make sure that the repository.save() method is actually called and that your security configuration (the part you've taken out) works correctly.

– g00glen00b
Nov 21 '18 at 14:23













Indeed, everything else should be perfect - see edit 2. Everything works fine without bcrypt, but as soon as I implement it it stops authenticating. Would it make any difference if I'm loading in my users via a class that implements ApplicationRunner? As I said, it works fine without bcrypt.. Been at it for days :)

– Harry Brown
Nov 21 '18 at 14:36





Indeed, everything else should be perfect - see edit 2. Everything works fine without bcrypt, but as soon as I implement it it stops authenticating. Would it make any difference if I'm loading in my users via a class that implements ApplicationRunner? As I said, it works fine without bcrypt.. Been at it for days :)

– Harry Brown
Nov 21 '18 at 14:36













Well, your original code (with BCrypt) works for me as well. However, I don't understand why you have both a form login and basic authentication. Which of these two do you use? And why do you have authorizeRequests() multiple times?

– g00glen00b
Nov 21 '18 at 14:41







Well, your original code (with BCrypt) works for me as well. However, I don't understand why you have both a form login and basic authentication. Which of these two do you use? And why do you have authorizeRequests() multiple times?

– g00glen00b
Nov 21 '18 at 14:41















Thanks! I'm learning as I'm going and working with different snippets from different sources. With all of that removed and just using formLogin(), it still doesn't work so I'm at a bit of a loss as to why it works for you but not me. Have updated the question to reflect the new configure method.

– Harry Brown
Nov 21 '18 at 14:58







Thanks! I'm learning as I'm going and working with different snippets from different sources. With all of that removed and just using formLogin(), it still doesn't work so I'm at a bit of a loss as to why it works for you but not me. Have updated the question to reflect the new configure method.

– Harry Brown
Nov 21 '18 at 14:58















Also @g00glen00b, when you mentioned that I should make sure the repository.save() method is actually called - that's done in my DatabaseLoader class that implements ApplicationRunner. Does it need to be done anywhere else?

– Harry Brown
Nov 21 '18 at 15:01





Also @g00glen00b, when you mentioned that I should make sure the repository.save() method is actually called - that's done in my DatabaseLoader class that implements ApplicationRunner. Does it need to be done anywhere else?

– Harry Brown
Nov 21 '18 at 15:01












1 Answer
1






active

oldest

votes


















0














Solved:



When I encrypted the password on the User entity, it would not authenticate. To fix it, I reverted back to a regular String password, getter/setter and setPassword(password) in the constructor. Then, in the class that implements ApplicationRunner, I encoded the password there:



    String password = "user";
PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String hashedPassword = passwordEncoder.encode(password);

userRepository.save(new User("First", "Last", "user", hashedPassword , "email@email.com", "12345", superRoles));





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413456%2funable-to-authenticate-using-after-implementing-bcrypt-passwordencoder%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Solved:



    When I encrypted the password on the User entity, it would not authenticate. To fix it, I reverted back to a regular String password, getter/setter and setPassword(password) in the constructor. Then, in the class that implements ApplicationRunner, I encoded the password there:



        String password = "user";
    PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    String hashedPassword = passwordEncoder.encode(password);

    userRepository.save(new User("First", "Last", "user", hashedPassword , "email@email.com", "12345", superRoles));





    share|improve this answer




























      0














      Solved:



      When I encrypted the password on the User entity, it would not authenticate. To fix it, I reverted back to a regular String password, getter/setter and setPassword(password) in the constructor. Then, in the class that implements ApplicationRunner, I encoded the password there:



          String password = "user";
      PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
      String hashedPassword = passwordEncoder.encode(password);

      userRepository.save(new User("First", "Last", "user", hashedPassword , "email@email.com", "12345", superRoles));





      share|improve this answer


























        0












        0








        0







        Solved:



        When I encrypted the password on the User entity, it would not authenticate. To fix it, I reverted back to a regular String password, getter/setter and setPassword(password) in the constructor. Then, in the class that implements ApplicationRunner, I encoded the password there:



            String password = "user";
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(password);

        userRepository.save(new User("First", "Last", "user", hashedPassword , "email@email.com", "12345", superRoles));





        share|improve this answer













        Solved:



        When I encrypted the password on the User entity, it would not authenticate. To fix it, I reverted back to a regular String password, getter/setter and setPassword(password) in the constructor. Then, in the class that implements ApplicationRunner, I encoded the password there:



            String password = "user";
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String hashedPassword = passwordEncoder.encode(password);

        userRepository.save(new User("First", "Last", "user", hashedPassword , "email@email.com", "12345", superRoles));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 15:39









        Harry BrownHarry Brown

        287




        287
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413456%2funable-to-authenticate-using-after-implementing-bcrypt-passwordencoder%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith