0
votes

La page de connexion personnalisée de Spring Security ne fonctionne pas

Je crée une application Web d'entreprise, j'ai créé ma page de connexion personnalisée, mais d'une manière ou d'une autre, seule la page de connexion de sécurité de printemps arrive à la place de ma page de connexion personnalisée. Voici ma classe de configuration de sécurité.

Veuillez aider.

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobalSecurity(AuthenticationManagerBuilder auth)
        throws Exception {
    auth.inMemoryAuthentication().withUser("test").password("pwd123")
            .roles("USER", "ADMIN");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/login").permitAll()
            .antMatchers("/", "/*todo*/**").access("hasRole('USER')").and()
            .formLogin();
        http.csrf().disable();
}


0 commentaires

3 Réponses :


0
votes

Vous devez spécifier l'URL de votre page de connexion personnalisée.

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/login").permitAll()
        .antMatchers("/", "/*todo*/**").access("hasRole('USER')").and()
        .formLogin()

         // put the relative URL to your custom login here
             .loginPage("/login")
             .permitAll();
    http.csrf().disable();
}

J'espère que cela vous aidera.


0 commentaires

0
votes

Regardez tout bien, c'est du travail pour moi:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
         .authorizeRequests()
             .antMatchers("/todo/**").hasRole("USER")
             .antMatchers("/", "/home").permitAll()
             .antMatchers("/login").permitAll()
             .anyRequest().authenticated()
             .and()
        .formLogin()
             .loginPage("/login")
             .defaultSuccessUrl("/home")
             .permitAll()
             .and()
         .logout() 
             .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
             .logoutSuccessUrl("/home")
             .permitAll()
             .and()
         .exceptionHandling().accessDeniedPage("/403");
  }

Voici un Projet Spring Boot .


0 commentaires

0
votes

@Override protected void configure (HttpSecurity http) lève l'exception { http.csrf (). disable (); http.authorizeRequests (). antMatchers ("/ login"). permitAll () .anyRequest (). authenticated (). et () .formLogin () .loginPage ("/ login") .defaultSuccessUrl ("/ home") .failureUrl ("/ login? error = true") .permitAll () .et() .Se déconnecter() .logoutSuccessUrl ("/ login? logout = true") .invalidateHttpSession (vrai) .deleteCookies ("JSESSIONID") .permitAll (); }

Référence: https://devkonline.com / tutoriels / content / SPRING-SECURITY-5-CUSTOM-FORM-LOGIN-THYMELEAF


0 commentaires