web:framework:spring:security

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
web:framework:spring:security [2023/12/05 11:36] – [Configuration des rôles] jcheronweb:framework:spring:security [2025/12/15 09:18] (Version actuelle) – [Déclaration du service] jcheron
Ligne 7: Ligne 7:
 ===== Intégration ===== ===== Intégration =====
  
-Ajouter les dépendances suivantes à **pom.xml** :+Ajouter la dépendance suivante à **pom.xml** :
  
 <sxh xml;title:pom.xml> <sxh xml;title:pom.xml>
- <dependency> +<dependency> 
-     <groupId>org.springframework.security</groupId> +    <groupId>org.springframework.boot</groupId> 
-     <artifactId>spring-security-web</artifactId> +    <artifactId>spring-boot-starter-security</artifactId> 
- </dependency> +</dependency>
- +
- <dependency> +
-     <groupId>org.springframework.security</groupId> +
-     <artifactId>spring-security-test</artifactId> +
-     <scope>test</scope> +
- </dependency> +
-  +
- <dependency> +
-     <groupId>org.springframework.security</groupId> +
-     <artifactId>spring-security-config</artifactId> +
- </dependency>+
 </sxh> </sxh>
  
Ligne 50: Ligne 39:
 @Configuration @Configuration
 @EnableWebSecurity @EnableWebSecurity
-public class WebSecurityConfiguration +public class SecurityConfig 
 + 
     @Bean     @Bean
     public SecurityFilterChain configure(HttpSecurity http) throws Exception {     public SecurityFilterChain configure(HttpSecurity http) throws Exception {
         http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(         http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(
                         (req) -> req.requestMatchers(                         (req) -> req.requestMatchers(
-                                        AntPathRequestMatcher.antMatcher("/"), +                        PathPatternRequestMatcher.withDefaults().matcher("/"), 
-                                        AntPathRequestMatcher.antMatcher("/css/**"), +                         PathPatternRequestMatcher.withDefaults().matcher("/css/**"), 
-                                        AntPathRequestMatcher.antMatcher("/js/**"), +                         PathPatternRequestMatcher.withDefaults().matcher("/js/**"), 
-                                        AntPathRequestMatcher.antMatcher("/img/**"), +                         PathPatternRequestMatcher.withDefaults().matcher("/img/**"), 
-                                        AntPathRequestMatcher.antMatcher("/register/**")+                         PathPatternRequestMatcher.withDefaults().matcher("/register/**")
                                 )                                 )
                                 .permitAll()                                 .permitAll()
                                 .anyRequest()                                 .anyRequest()
                                 .authenticated()                                 .authenticated()
-                ).formLogin();+                );
         return http.build();         return http.build();
     }     }
Ligne 209: Ligne 198:
     @Bean     @Bean
     public DaoAuthenticationProvider authenticationProvider(UserDetailsService userService) {     public DaoAuthenticationProvider authenticationProvider(UserDetailsService userService) {
-        DaoAuthenticationProvider auth = new DaoAuthenticationProvider(); +        DaoAuthenticationProvider auth = new DaoAuthenticationProvider(userService);
-        auth.setUserDetailsService(userService);+
         auth.setPasswordEncoder(getPasswordEncoder());         auth.setPasswordEncoder(getPasswordEncoder());
         return auth;         return auth;
Ligne 238: Ligne 226:
 </sxh> </sxh>
  
 +=== Récupération Utilisateur connecté ===
 +Récupération de l'utilisateur connecté en tant que **ModelAttribute** pour utilisation dans les vues :
 +
 +<sxh java>
 +@ControllerAdvice
 +public class MainAdvice {
 +    @ModelAttribute("activeUser")
 +    public User activeUser(Authentication auth) {
 +        return (auth == null) ? null : (User) auth.getPrincipal();
 +    }
 +}
 +</sxh>
 ==== Login form personnalisée ==== ==== Login form personnalisée ====
  
Ligne 295: Ligne 295:
         http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(   // (1)         http.csrf(AbstractHttpConfigurer::disable).authorizeHttpRequests(   // (1)
                         (req) -> req.requestMatchers(                         (req) -> req.requestMatchers(
-                                        AntPathRequestMatcher.antMatcher("/h2-console/**"),   // (2)+                                        PathRequest.toH2Console(),   // (2)
                                         ...                                         ...
                                 )                                 )
  • web/framework/spring/security.1701772561.txt.gz
  • Dernière modification : il y a 2 ans
  • de jcheron