Logowanie do aplikacji za pomocą Spring Security

0

Witam wszystkich. ;)

Tworzę sobie swoją własną małą aplikację i nadszedł czas by stworzyć możliwość logowania się do niej.
Stworzyłem klasę SecurityConfig, która wygląda tak

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    DataSource dataSource;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("SELECT username, password FROM user_profile WHERE username = ?")
                .authoritiesByUsernameQuery("SELECT username, 'ROLE_USER' FROM user_profile WHERE username=?")
                .passwordEncoder(new StandardPasswordEncoder("53Kr3t"));

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .formLogin()
                .loginPage("/")
                .defaultSuccessUrl("/")
                .and()
                .logout().logoutSuccessUrl("/")
                .and()
                .authorizeRequests()
                .antMatchers("/webjars/**", "/", "/register/**", "/add/**",
                        "/list_item/**", "/signin/**", "/signup/**" ).permitAll()
                .anyRequest().authenticated();
    }
}

Przy odpaleniu tego kodu dostaje taki błąd https://github.com/Baron762/superengine.pl/blob/master/error

@Entity
public class UserProfile {

    @Id
    @GeneratedValue
    private Long id;

    private String firstName;

    private String lastName;

    private String postalCode;

    private String place;

    private String address;

    @OneToMany(mappedBy = "user")
    private List<UserProduct> userMyProductList;

    @NotNull
    private String username;

    @NotNull
    private String password;

    private LocalDate birthDate;

    @Email
    @NotNull
    private String email;

//Get & Set

Cały projekt https://github.com/Baron762/superengine.pl

0

Przecież odpowiedź jest w tym stack trace.


Caused by: java.sql.SQLException: Column Index out of range, 3 > 2. 
0

Potrzebujesz tabelę gdzie będą: id(Long), username(String), password(String), enabled(int) i 'ROLE_USER'(String). I to tyle.

Dodatkowo zmień zapytanie

auth.jdbcAuthentication()
                .dataSource(dataSource)
                .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username=?")
                .authoritiesByUsernameQuery("SELECT username, ‘ROLE_USER’ FROM users WHERE username=?");

Musisz wybierać trzy pola. Nie możesz wybierać tylko username i password. Spring Security nakazuje sprawdzenie poprzez 'enabled' czy konto jest aktywne.

1 użytkowników online, w tym zalogowanych: 0, gości: 1