Bonjour,
je travaille sur une application Spring Boot/Spring Security/angular. J'ai implémenté du coté client une page de login correspondant au composant LoginComponent

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html'
})
export class LoginComponent implements OnInit {
Le routing est défini de la sorte

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
 
const routes: Routes = [
  { path: '', component: HomeComponent, canActivate: [AuthGuard] },
  { path: 'login', component: LoginComponent },
 
  // otherwise redirect to home
  { path: '**', redirectTo: '', pathMatch: 'full' }
];
Quand je lance en dev l'url http://localhost:4200/login, tout se passe bien et la page de login est affichée

j'interface avec un back Spring Boot/Spring Security que je lance avec >java -jar backend-0.1-SNAPSHOT.jar où backend-0.1-SNAPSHOT.jar est le package de l'ensemble coté client/coté serveur. Avant d'implémenter Spring Security, tout se passait bien et j'avais la page de login affichée lorsque je lancais l'url http://localhost:8080/login

J'ai rajouté Spring Security. La configuration est la suivante:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
package .............;
 
import .......
 
@Configuration
@EnableWebSecurity
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter {
 
 
    @Autowired
    private CustomUserDetailsService userDetailsService;
 
    @Autowired
    private UserRepository userRepository;
 
    @Autowired
    private ModelMapper modelMapper;
 
 
    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }
 
    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authProvider
                = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        authProvider.setPasswordEncoder(bCryptPasswordEncoder());
        return authProvider;
    }
 
    @Bean(name="passwordEncoder")
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
 
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .httpBasic()
                .and()
                .authorizeRequests()
                .antMatchers("/index.html", "/", "/home", "/login").permitAll()
                .anyRequest().authenticated().and()
                .cors().configurationSource(corsConfigurationSource()).and()
                .formLogin(form -> form
                        .loginPage("/login")
                        .permitAll().defaultSuccessUrl("/")
                        .failureUrl("/login")
                );
 
    }
 
    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200"));
        configuration.setAllowedMethods(Arrays.asList("*"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}
J'ai rajouté le controller suivant mais je ne sais pas si c'est nécessaire

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
@CrossOrigin(origins = "http://localhost:4200")
public class HelloController {
 
 
    @RequestMapping("/login")
    public boolean login() {
        return
                isAuthenticated();
    }
 
    private boolean isAuthenticated() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null || AnonymousAuthenticationToken.class.
                isAssignableFrom(authentication.getClass())) {
            return false;
        }
        return authentication.isAuthenticated();
    }
et quand je lance le jar et met l'adresse http://localhost:8080/login, j'ai une requête GET
http://localhost:8080/login qui est lancée avec succès et qui renvoie false dans la page, mais ma page de login n'est pas affichée. Les logs sont les suivant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
2021-05-01 09:26:18.869  INFO 964 --- [           main] com.example.commission.Application       : Started Application in 12.654 seconds (JVM running for 13.801)
2021-05-01 09:26:24.932  INFO 964 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-05-01 09:26:24.934  INFO 964 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-05-01 09:26:24.936  INFO 964 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
2021-05-01 09:26:24.961 DEBUG 964 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Securing GET /login
2021-05-01 09:26:24.969 DEBUG 964 --- [nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to empty SecurityContext
2021-05-01 09:26:24.982 DEBUG 964 --- [nio-8080-exec-1] o.s.s.w.a.AnonymousAuthenticationFilter  : Set SecurityContextHolder to anonymous SecurityContext
2021-05-01 09:26:24.983 DEBUG 964 --- [nio-8080-exec-1] o.s.s.w.session.SessionManagementFilter  : Request requested invalid session id 151FD0D8B3F35F2A7928F29359EF4C03
2021-05-01 09:26:24.995 DEBUG 964 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor    : Authorized filter invocation [GET /login] with attributes [permitAll]
2021-05-01 09:26:24.996 DEBUG 964 --- [nio-8080-exec-1] o.s.security.web.FilterChainProxy        : Secured GET /login
2021-05-01 09:26:25.072 DEBUG 964 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : Did not store anonymous SecurityContext
2021-05-01 09:26:25.082 DEBUG 964 --- [nio-8080-exec-1] w.c.HttpSessionSecurityContextRepository : Did not store anonymous SecurityContext
2021-05-01 09:26:25.082 DEBUG 964 --- [nio-8080-exec-1] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
et quand je lance l'url http://localhost:8080, j'obtiens sur mon navigateur l'écran suivant

Nom : Capture d’écran 2021-05-01 à 10.08.29.png
Affichages : 1095
Taille : 76,7 Ko