Hejka.
Bardzo proszę o pomoc ze Springem-security.
Cały czas pryz logowaniu na "localhost:8080/login" wywala mi błąd "BAD CREDENTIALS". Na konsoli zadnego błędu nie pokazuje.
Jakby ktoś wiedział jak to można naprawić to będę wdzięczny:D
@Component
@Scope("singleton")
public class Starter implements CommandLineRunner
{
@Autowired
KnightRepository knightRepository;
@Autowired
QuestRepository questRepository;
@Autowired
QuestService questService;
@Autowired
PlayerInformationRepository playerInformationRepository;
@Autowired
RoleRepository roleRepository;
@Override
@Transactional
public void run(String... args) throws Exception
{
questRepository.createRandomQuest();
questRepository.createRandomQuest();
knightRepository.createKnight("Percival", 33);
PlayerInformation pi1 = new PlayerInformation("user1", "password");
playerInformationRepository.createPlayerInformation(pi1);
PlayerInformation pi2 = new PlayerInformation("user2", "user2");
playerInformationRepository.createPlayerInformation(pi2);
Role user1RoleUser = new Role ("user1", "USER");
Role user2RoleUser = new Role ("user2", "USER");
Role user2RoleAdmin = new Role ("user2", "ADMIN");
roleRepository.persistRole(user1RoleUser);
roleRepository.persistRole(user2RoleUser);
roleRepository.persistRole(user2RoleAdmin);
System.out.println(roleRepository.getAll());
}
}
@Entity
public class PlayerInformation
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String username;
private String password;
private boolean enabled;
public PlayerInformation ()
{
}
public PlayerInformation (String username, String password)
{
this.username = username;
this.password = password;
this.enabled = true;
}
private int gold = 0;
public int getGold()
{
return gold;
}
public void setGold(int gold)
{
this.gold = gold;
}
}
@Entity
public class Role
{
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private Integer id;
private String username;
private String role;
public Role ()
{
}
public Role(String userName, String userRole)
{
this.username = userName;
this.role = userRole;
}
@Override
public String toString()
{
return "Role{" + "id=" + id + ", username='" + username + '\'' + ", role='" + role + '\'' + '}';
}
}
@Repository
public class RoleRepository
{
@PersistenceContext
EntityManager em;
@Transactional
public void persistRole (Role role)
{
em.persist(role);
}
public List<Role> getAll()
{
return em.createQuery("from Role", Role.class).getResultList();
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Autowired
DataSource dataSource;
@Override
public void configure (HttpSecurity http) throws Exception
{
http.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.antMatchers("/knights").hasAnyAuthority("USER", "ADMIN")
.antMatchers("/knight").hasAnyAuthority("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().defaultSuccessUrl("/knights");
}
@Autowired
public void securityUsers (AuthenticationManagerBuilder auth) throws Exception
{
auth.jdbcAuthentication()
.dataSource(dataSource)
.usersByUsernameQuery("SELECT username, password, enabled FROM PLAYER_INFORMATION WHERE username = ?")
.authoritiesByUsernameQuery("SELECT username, role FROM ROLE WHERE username = ?");
}
}