React
Sécurité des applications distribuées
Architecture des composants A.Ettaoufik 373
SPRING SECURITY
❖ Spring Security est un framework de sécurité pour les applications Java.
❖ Il fournit des fonctionnalités de sécurité complètes pour les applications basées
sur Spring.
❖ Il permet d‘assurer l'authentification, l'autorisation et la protection contre les
vulnérabilités courantes liées à la sécurité dans les applications .
❖ S'intègre parfaitement avec d'autres composants Spring tels que Spring MVC,
Spring Boot et Spring Data.
Architecture des composants A.Ettaoufik 374
SPRING SECURITY
Home page
User authentificated
Spring Security User authentification
User fail
Login page
BDD
Architecture des composants A.Ettaoufik 375
SPRING SÉCURITÉ- ACTIVITÉ
1. Créer une nouvelle BDD nommée « gestionsecurite » et créer les trois tables
suivantes :
-- Users
create table if not exists users create table if not exists notes(id int PRIMARY
( key, username varchar(200) not null, matiere var
username varchar(200) not null primary key, char(50) not null, note int, constraint fk_note_
password varchar(500) not null, users foreign key (username) references users (u
enabled boolean not null sername));
);
-- Authorities
create table if not exists authorities
(
username varchar(200) not null,
authority varchar(50) not null,
constraint fk_authorities_users foreign key (username) references users (username),
constraint username_authority UNIQUE (username, authority)
);
Architecture des composants A.Ettaoufik 379
ACTIVITÉ
1. Insérer un jeu d’enregistrement
Le mot de passe est :password
INSERT users VALUES("user", "{bcrypt}$2a$14$tEnq90/CcR320dWQ.NdQLuj326PmgLzMGmFkUUOHQrbjPWplKK67i", true);
INSERT users VALUES("admin", "{bcrypt}$2a$14$tJANh4xMR7qNjwwftmoZjezhp6rP.RVUtIFXFBF6maQvqGXwvM4JS", true);
INSERT users VALUES("user1", "{bcrypt}$2a$14$tEnq90/CcR320dWQ.NdQLuj326PmgLzMGmFkUUOHQrbjPWplKK67i", true);
INSERT authorities VALUES("user", "ROLE_USER");
INSERT authorities VALUES("admin", "ROLE_USER");
INSERT authorities VALUES("admin", "ROLE_ADMIN");
INSERT authorities VALUES("user1", "ROLE_USER");
//insérer des données dans la tables notes
Architecture des composants A.Ettaoufik 380
@RestController
public class EtudiantController {
ACTIVITÉ @GetMapping("/liste")
public List<Etudiant>getEtudiants(){
return List.of(
Créer une nouvelle application spring boot new Etudiant(1,"Lamrani","Yousra"),
new Etudiant(2,"kessou","Abdo"),
Ajouter les deux classes Etudiant et EtudiantController new Etudiant(3,"chadli","Adam")
);
@Data }
@NoArgsConstructor @GetMapping("/msg")
@AllArgsConstructor public String getMessage(){
public class Etudiant { return "Utilistaurs";
int id; }
String nom; }
String prenom;
}
@Table(name="notes")
public class Note {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id;
String username;
String matiere;
int note;
}
Architecture des composants A.Ettaoufik 381
ACTIVITÉ
@Service
public interface NoteRepository extends JpaRepository<Note,Integer> { public class ServiceNote {
public List<Note> getNotesByUsername(String nom); @Autowired
} NoteRepository noteRepository;
public List<Note> listeNote(){
return noteRepository.findAll();
}
@RestController
public List<Note> listeNoteParUser(String nom){
public class NoteController {
return noteRepository.getNotesByUsername(nom);
@Autowired
}
ServiceNote serviceNote;
}
@GetMapping("/notes")
public List<Note> getAllNote(){
return serviceNote.listeNote();
}
@GetMapping("/notesUser")
public List<Note> getNoteParUser(){
String userName=SecurityContextHolder.getContext().getAuthentication().getName();
return serviceNote.listeNoteParUser(userName);
}
}
Architecture des composants A.Ettaoufik 382
Ajouter le fichier de configuration de la sécurité « SecurityConfig »
@Configuration
ACTIVITÉ
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
@Bean
SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/msg","/noteUser").hasAnyRole("USER")
.requestMatchers("/liste" ,"/notes").hasAnyRole("ADMIN")
.anyRequest().authenticated()
)
.formLogin(login -> login
.defaultSuccessUrl("/")
.permitAll())
.logout(logout -> logout
.logoutSuccessUrl("/"));
return http.build();
}
@Bean
PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
@Bean
UserDetailsManager jdbcUserDetailsManager(DataSource dataSource) {
return new JdbcUserDetailsManager(dataSource);
}
} Architecture des composants A.Ettaoufik 383
ACTIVITÉ
Le fichier pom.xml
<dependency>
<groupId>com.mysql</groupId>
<dependency> <artifactId>mysql-connector-j</artifactId>
<groupId>org.springframework.boot</groupId> <scope>runtime</scope>
<artifactId>spring-boot-starter-web</artifactId> </dependency>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<dependency> <artifactId>spring-boot-starter-test</artifactId>
<groupId>org.springframework.boot</groupId> <scope>test</scope>
<artifactId>spring-boot-starter-security</artifactId> </dependency>
</dependency> <dependency>
<dependency> <groupId>org.springframework.security</groupId>
<groupId>org.thymeleaf.extras</groupId> <artifactId>spring-security-test</artifactId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId> <scope>test</scope>
</dependency> </dependency>
<dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<groupId>org.springframework.boot</groupId> <dependency>
<artifactId>spring-boot-starter-jdbc</artifactId> <groupId>org.projectlombok</groupId>
</dependency> <artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
Architecture des composants A.Ettaoufik 384
ACTIVITÉ
Application.proporties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/gestionsecurite
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Architecture des composants A.Ettaoufik 385
ACTIVITÉ
Teste avec le compte « admin »
Architecture des composants A.Ettaoufik 386
ACTIVITÉ
Teste avec le compte « user »
Architecture des composants A.Ettaoufik 387