Não estou conseguindo fazer o login mesmo seguindo todo código da aula. Quando tento autenticar utilizando admin@casadocodigo.com.br/123456, recebo o erro:
Your login attempt was not successful, try again.
Reason: Bad credentials
Os registros estão nas tabelas:
mysql> select * from role;
+------------+
| name |
+------------+
| ROLE_ADMIN |
+------------+
1 row in set (0.00 sec)
mysql> select * from user;
+---------------------------+---------------+----------------------------------
---------------------------+
| email | name | passwd
|
+---------------------------+---------------+----------------------------------
---------------------------+
| admin@casadocodigo.com.br | Administrador | $2a$04$qP517gz1KNVEJUTCkUQCY.JzEo
zHFjLAhPQjrg5iP6Z/UmWjvUhq |
+---------------------------+---------------+----------------------------------
---------------------------+
1 row in set (0.02 sec)
mysql> select * from user_role;
+---------------------------+------------+
| User_email | roles_name |
+---------------------------+------------+
| admin@casadocodigo.com.br | ROLE_ADMIN |
+---------------------------+------------+
1 row in set (0.00 sec)
Por favor, podem me ajudar?
Segue novas classes:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import br.com.spedroza.bookstore.dao.UserDAO;
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private UserDAO userDAO;
@Override
protected void configure(HttpSecurity http) throws Exception {
System.out.println("Inside SecurityConfiguration.configure HttpSecurity");
http.authorizeRequests()
.antMatchers(HttpMethod.POST, "/product").hasRole("ADMIN") // only admin can post new products
.antMatchers(HttpMethod.GET, "/product").permitAll() // all users can see produt list
.antMatchers("/product/form").hasRole("ADMIN") // only admin can access produt form
.antMatchers("/resources/**").permitAll()
.antMatchers("/cart/**").permitAll()
.antMatchers("/payment/**").permitAll()
.antMatchers("/product/**").permitAll()
.antMatchers("/").permitAll()
.anyRequest().authenticated()
.and().formLogin();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("Inside SecurityConfiguration.configure AuthenticationManagerBuilder");
auth.userDetailsService(userDAO).passwordEncoder(new BCryptPasswordEncoder());
}
}
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;
import br.com.spedroza.bookstore.model.User;
@Repository
public class UserDAO implements UserDetailsService {
@PersistenceContext
private EntityManager manager;
// get a user by email
@Override
public UserDetails loadUserByUsername(String email) {
System.out.println("Inside UserDAO.getUser...");
System.out.println("Querying for user : " + email);
User user = manager.createQuery("select u from User u where u.email = :email", User.class)
.setParameter("email", email).getSingleResult();
// check user exists
if (user != null) {
throw new UsernameNotFoundException("User not found for email " + email);
}
return user;
}
}