Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Gestion des stages Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fatima Ezzahra Majidi
Gestion des stages Backend
Merge requests
!7
Master
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Master
master
into
main
Overview
0
Commits
2
Pipelines
0
Changes
12
Merged
Fatima Ezzahra Majidi
requested to merge
master
into
main
3 months ago
Overview
0
Commits
2
Pipelines
0
Changes
12
Expand
0
0
Merge request reports
Compare
main
main (base)
and
latest version
latest version
ad35c6b3
2 commits,
3 months ago
12 files
+
324
−
52
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
12
Search (e.g. *.vue) (Ctrl+P)
src/main/java/com/example/gestionstagesbackend/
C
onfig/SecurityConfig.java
→
src/main/java/com/example/gestionstagesbackend/
c
onfig/SecurityConfig.java
+
80
−
0
Options
package
com.example.gestionstagesbackend.
C
onfig
;
package
com.example.gestionstagesbackend.
c
onfig
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity
;
import
org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer
;
import
org.springframework.security.config.http.SessionCreationPolicy
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.security.web.SecurityFilterChain
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.web.cors.CorsConfiguration
;
import
org.springframework.web.cors.CorsConfigurationSource
;
import
org.springframework.web.cors.UrlBasedCorsConfigurationSource
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.List
;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
(
prePostEnabled
=
true
)
// Enables @PreAuthorize
public
class
SecurityConfig
{
@Bean
@@ -31,17 +32,49 @@ public class SecurityConfig {
@Bean
public
SecurityFilterChain
securityFilterChain
(
HttpSecurity
http
)
throws
Exception
{
http
.
csrf
(
csrf
->
csrf
.
disable
())
.
cors
(
cors
->
cors
.
configurationSource
(
corsConfigurationSource
()))
// Enable CORS
.
csrf
(
csrf
->
csrf
.
disable
())
// Disable CSRF for APIs
.
authorizeHttpRequests
(
auth
->
auth
.
requestMatchers
(
"/api/auth/**"
).
permitAll
()
// Public routes
.
requestMatchers
(
"/api/students/**"
).
hasAnyRole
(
"SUPERVISEUR"
,
"ADMIN"
)
.
requestMatchers
(
"/api/stages/**"
).
hasAnyRole
(
"ENTREPRISE"
,
"ADMIN"
)
.
requestMatchers
(
"/api/enterprises/**"
).
hasRole
(
"ADMIN"
)
.
requestMatchers
(
"/api/candidacies/**"
).
hasAnyRole
(
"ETUDIANT"
,
"ADMIN"
)
// Allow everyone to access login, register, and logout
.
requestMatchers
(
"/api/auth/login"
,
"/api/auth/logout"
,
"/api/auth/register"
).
permitAll
()
// Allow OPTIONS requests for CORS preflight
.
requestMatchers
(
"/**"
).
permitAll
()
// Role-based access
.
requestMatchers
(
"/api/students"
).
hasAuthority
(
"ROLE_ETUDIANT"
)
.
requestMatchers
(
"/api/students/**"
).
hasAuthority
(
"ROLE_ETUDIANT"
)
.
requestMatchers
(
"/api/stages"
).
hasAuthority
(
"ROLE_ETUDIANT"
)
.
requestMatchers
(
"/api/stages/**"
).
hasAuthority
(
"ROLE_ENTREPRISE"
)
.
requestMatchers
(
"/api/students"
).
hasAuthority
(
"ROLE_SUPERVISEUR"
)
.
requestMatchers
(
"/api/students/**"
).
hasAuthority
(
"ROLE_SUPERVISEUR"
)
.
requestMatchers
(
"/api/stages"
).
hasAuthority
(
"ROLE_SUPERVISEUR"
)
.
requestMatchers
(
"/api/stages/**"
).
hasAuthority
(
"ROLE_SUPERVISEUR"
)
.
requestMatchers
(
"/api/enterprises/add"
).
hasAuthority
(
"ROLE_ENTREPRISE"
)
.
requestMatchers
(
"/api/enterprises/update"
).
hasAuthority
(
"ROLE_ENTREPRISE"
)
.
requestMatchers
(
"/api/enterprises/delete"
).
hasAuthority
(
"ROLE_ENTREPRISE"
)
.
requestMatchers
(
"/**"
).
hasAuthority
(
"ROLE_ADMIN"
)
// Any other request requires authentication
.
anyRequest
().
authenticated
()
)
.
sessionManagement
(
session
->
session
.
sessionCreationPolicy
(
SessionCreationPolicy
.
STATELESS
));
.
formLogin
()
.
and
()
.
httpBasic
();
return
http
.
build
();
}
@Bean
public
CorsConfigurationSource
corsConfigurationSource
()
{
CorsConfiguration
configuration
=
new
CorsConfiguration
();
configuration
.
setAllowedOrigins
(
Collections
.
singletonList
(
"http://localhost:3000"
));
// Frontend URL
configuration
.
setAllowedMethods
(
Arrays
.
asList
(
"GET"
,
"POST"
,
"PUT"
,
"DELETE"
,
"OPTIONS"
));
// Allow these HTTP methods
configuration
.
setAllowedHeaders
(
Arrays
.
asList
(
"Authorization"
,
"Content-Type"
));
// Allow these headers
configuration
.
setAllowCredentials
(
true
);
// Allow cookies/auth headers
UrlBasedCorsConfigurationSource
source
=
new
UrlBasedCorsConfigurationSource
();
source
.
registerCorsConfiguration
(
"/**"
,
configuration
);
return
source
;
}
}
Loading