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 10:29] – [Configuration des rôles] 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 167: | Ligne 156: | ||
| public void encodePassword(User user) { | public void encodePassword(User user) { | ||
| user.setPassword(pEncoder.encode(user.getPassword())); | user.setPassword(pEncoder.encode(user.getPassword())); | ||
| - | } | ||
| - | |||
| - | public User createUser(String login, String password) { | ||
| - | User user = new User(); | ||
| - | user.setLogin(login); | ||
| - | user.setPassword(password); | ||
| - | encodePassword(user); | ||
| - | user.setRole(getRole(" | ||
| - | return uRepo.save(user); | ||
| } | } | ||
| } | } | ||
| Ligne 218: | 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 230: | Ligne 209: | ||
| Ne pas oublier de hasher le password User avant création : | Ne pas oublier de hasher le password User avant création : | ||
| - | < | + | < |
| - | @Controller | + | @Service |
| - | class InitController | + | public |
| - | @Autowired | + | |
| - | lateinit var dbUserService: | + | |
| - | | + | |
| - | lateinit var userRepository: | + | |
| - | + | ||
| - | @Autowired | + | |
| - | lateinit var roleRepository: | + | |
| - | | + | |
| - | fun createUser(@PathVariable username:String): | + | |
| - | | + | user.setLogin(login); |
| - | user.username=username | + | user.setPassword(password); |
| - | user.email=username.lowercase()+" | + | |
| - | user.role=" | + | user.setRole(getRole("ROLE_USER")); |
| - | user.password="1234" | + | |
| - | (dbUserService as DbUserService).encodePassword(user) | + | |
| - | | + | |
| - | return " | + | |
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | === 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 269: | Ligne 251: | ||
| Ajouter une route pour le **login** | Ajouter une route pour le **login** | ||
| - | < | + | < |
| - | class AppConfig | + | @Configuration |
| - | | + | public |
| - | registry.addViewController("/ | + | |
| + | | ||
| + | public void addViewControllers(ViewControllerRegistry | ||
| + | registry.addViewController("/ | ||
| } | } | ||
| } | } | ||
| Ligne 279: | Ligne 264: | ||
| Modifier la configuration dans **WebSecurityConfig** : | Modifier la configuration dans **WebSecurityConfig** : | ||
| - | < | + | < |
| @Configuration | @Configuration | ||
| @EnableWebSecurity | @EnableWebSecurity | ||
| - | @EnableMethodSecurity( | ||
| - | prePostEnabled = true, proxyTargetClass = true | ||
| - | ) | ||
| public class WebSecurityConfiguration { | public class WebSecurityConfiguration { | ||
| Ligne 313: | Ligne 295: | ||
| http.csrf(AbstractHttpConfigurer:: | http.csrf(AbstractHttpConfigurer:: | ||
| (req) -> req.requestMatchers( | (req) -> req.requestMatchers( | ||
| - | | + | |
| ... | ... | ||
| ) | ) | ||
| Ligne 341: | 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 381: | 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 394: | 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 404: | Ligne 386: | ||
| === Activation === | === Activation === | ||
| - | < | + | < |
| @Configuration | @Configuration | ||
| @EnableWebSecurity | @EnableWebSecurity | ||
| Ligne 412: | Ligne 394: | ||
| jsr250Enabled = true | jsr250Enabled = true | ||
| ) | ) | ||
| - | class WebSecurityConfig | + | public |
| ... | ... | ||
| } | } | ||
| Ligne 426: | Ligne 408: | ||
| == @Secured == | == @Secured == | ||
| - | < | + | < |
| @Secured(" | @Secured(" | ||
| - | | + | |
| - | return " | + | return " |
| } | } | ||
| </ | </ | ||
| == @RolesAllowed == | == @RolesAllowed == | ||
| - | < | + | < |
| @RolesAllowed(value = [ " | @RolesAllowed(value = [ " | ||
| - | | + | |
| - | return " | + | return " |
| } | } | ||
| </ | </ | ||
| Ligne 447: | Ligne 429: | ||
| * En testant éventuellement les paramètres passés. | * En testant éventuellement les paramètres passés. | ||
| - | < | + | < |
| @PreAuthorize(" | @PreAuthorize(" | ||
| - | | + | |
| + | return | ||
| + | } | ||
| </ | </ | ||
| Ligne 456: | Ligne 440: | ||
| <sxh kotlin> | <sxh kotlin> | ||
| @PreAuthorize("# | @PreAuthorize("# | ||
| - | | + | |
| ... | ... | ||
| } | } | ||
| Ligne 469: | Ligne 453: | ||
| <sxh kotlin> | <sxh kotlin> | ||
| @PostAuthorize(" | @PostAuthorize(" | ||
| - | | + | |
| - | return userRoleRepository.loadUserByUserName(username) | + | return userRoleRepository.loadUserByUserName(username); |
| } | } | ||
| </ | </ | ||
| Ligne 489: | Ligne 473: | ||
| La protection csrf est activée par défaut dans **Spring security** | La protection csrf est activée par défaut dans **Spring security** | ||
| </ | </ | ||
| + | |||
| + | Mettre éventuellement une exception pour la console H2 (mais la console H2 n'a aucune raison d' | ||
| + | |||
| + | <sxh java; | ||
| + | req.csrf(csrf-> | ||
| + | </ | ||
| === Intégration dans les vues === | === Intégration dans les vues === | ||
| Ligne 510: | Ligne 500: | ||
| </ | </ | ||
| </ | </ | ||
| + | |||
| + | |||
| + | <wrap important> | ||