Différences
Ci-dessous, les différences entre deux révisions de la page.
| 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:07] – [Mise en place CSRF] jcheron | web:framework:spring:security [2025/12/15 09:18] (Version actuelle) – [Déclaration du service] jcheron | ||
|---|---|---|---|
| Ligne 7: | Ligne 7: | ||
| ===== Intégration ===== | ===== Intégration ===== | ||
| - | Ajouter | + | Ajouter |
| <sxh xml; | <sxh xml; | ||
| - | < | + | < |
| - | | + | < |
| - | | + | < |
| - | </ | + | </ |
| - | + | ||
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | + | ||
| - | < | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| </ | </ | ||
| 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:: | http.csrf(AbstractHttpConfigurer:: | ||
| (req) -> req.requestMatchers( | (req) -> req.requestMatchers( | ||
| - | AntPathRequestMatcher.antMatcher("/" | + | PathPatternRequestMatcher.withDefaults().matcher("/" |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| ) | ) | ||
| .permitAll() | .permitAll() | ||
| .anyRequest() | .anyRequest() | ||
| .authenticated() | .authenticated() | ||
| - | | + | ); |
| 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: | ||
| </ | </ | ||
| + | === Récupération Utilisateur connecté === | ||
| + | Récupération de l' | ||
| + | |||
| + | <sxh java> | ||
| + | @ControllerAdvice | ||
| + | public class MainAdvice { | ||
| + | @ModelAttribute(" | ||
| + | public User activeUser(Authentication auth) { | ||
| + | return (auth == null) ? null : (User) auth.getPrincipal(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| ==== Login form personnalisée ==== | ==== Login form personnalisée ==== | ||
| Ligne 295: | Ligne 295: | ||
| http.csrf(AbstractHttpConfigurer:: | http.csrf(AbstractHttpConfigurer:: | ||
| (req) -> req.requestMatchers( | (req) -> req.requestMatchers( | ||
| - | | + | |
| ... | ... | ||
| ) | ) | ||
| Ligne 323: | Ligne 323: | ||
| static RoleHierarchy roleHierarchy() { | static RoleHierarchy roleHierarchy() { | ||
| RoleHierarchyImpl hierarchy = new RoleHierarchyImpl(); | RoleHierarchyImpl hierarchy = new RoleHierarchyImpl(); | ||
| - | hierarchy.setHierarchy(" | + | hierarchy.setHierarchy(" |
| return hierarchy; | return hierarchy; | ||
| } | } | ||
| Ligne 363: | Ligne 363: | ||
| ) | ) | ||
| .permitAll(); | .permitAll(); | ||
| - | req.requestMatchers(AntPathRequestMatcher.antMatcher("/ | + | req.requestMatchers(AntPathRequestMatcher.antMatcher("/ |
| req.requestMatchers(AntPathRequestMatcher.antMatcher("/ | req.requestMatchers(AntPathRequestMatcher.antMatcher("/ | ||
| req.requestMatchers(AntPathRequestMatcher.antMatcher("/ | req.requestMatchers(AntPathRequestMatcher.antMatcher("/ | ||
| Ligne 376: | Ligne 376: | ||
| ^Méthode ^ Description ^ | ^Méthode ^ Description ^ | ||
| | 1 - **permitAll** | Pas de sécurisation sur les urls commençant par **/css**, **/ | | 1 - **permitAll** | Pas de sécurisation sur les urls commençant par **/css**, **/ | ||
| - | | 2 - **hasRole** | Les utilisateurs ayant le rôle **ROLE_ADMIN** peuvent accéder aux urls commençant par **/admin**. | | + | | 2 - **hasRole** | Les utilisateurs ayant le rôle **ADMIN** peuvent accéder aux urls commençant par **/admin**. | |
| | 3 - **hasAuthority** | Les utilisateurs ayant l' | | 3 - **hasAuthority** | Les utilisateurs ayant l' | ||
| | 4 - **hasAnyAuthority** | Les utilisateurs ayant l'une des authorities **USER**, **ADMIN** ou **MANAGER** peuvent accéder aux urls commençant par **/staff**. | | | 4 - **hasAnyAuthority** | Les utilisateurs ayant l'une des authorities **USER**, **ADMIN** ou **MANAGER** peuvent accéder aux urls commençant par **/staff**. | | ||
| Ligne 474: | Ligne 474: | ||
| </ | </ | ||
| - | Mettez | + | Mettre |
| <sxh java; | <sxh java; | ||
| Ligne 500: | Ligne 500: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | |||
| + | <wrap important> | ||