Skip to content

Commit 08b65b7

Browse files
refactor: improve readability into SecurityConfig URL authorizations
added new URL authorizations constraints and also comments to each endpoint
1 parent 09f40ed commit 08b65b7

1 file changed

Lines changed: 39 additions & 14 deletions

File tree

src/main/java/com/vianavitor/simplelibrarygame/config/SecurityConfig.java

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,46 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3131
session.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
3232
})
3333
.authorizeHttpRequests(authorization -> authorization
34-
.requestMatchers(HttpMethod.POST, "/auth/login").permitAll()
35-
.requestMatchers("/api/students/register").permitAll()
36-
// .requestMatchers(HttpMethod.POST, "/api/administrators/register").permitAll()
37-
.requestMatchers("/api/classrooms/**", "/api/administrators/**").hasRole("ADM")
38-
.requestMatchers("/api/students/**").hasAnyRole("ADM", "STUDENT")
39-
.requestMatchers("/api/groups/**", "/api/group-books/**").hasRole("STUDENT")
40-
.requestMatchers("/api/summaries/**").hasRole("STUDENT")
41-
.requestMatchers("/api/stats/**").hasRole("STUDENT")
42-
.requestMatchers(HttpMethod.GET, "/api/stats/**").hasRole("PROFESSOR")
43-
.requestMatchers(HttpMethod.GET, "/api/summaries/**").hasRole("PROFESSOR")
44-
.requestMatchers("/api/professors/**").hasAnyRole("ADM", "PROFESSOR")
45-
.requestMatchers("/api/librarians/**").hasAnyRole("ADM", "LIBRARIAN")
46-
.requestMatchers(HttpMethod.POST, "/api/books/**").hasRole("LIBRARIAN")
47-
.requestMatchers(HttpMethod.PUT, "/api/books/**").hasRole("LIBRARIAN")
4834
.requestMatchers("/api/test/**").permitAll()
35+
.requestMatchers(HttpMethod.POST, "/auth/login").permitAll()
36+
// ENDPOINTS PERMISSIONS:
37+
// * Students permissions
38+
.requestMatchers(HttpMethod.POST, "/api/students/register").permitAll()
39+
.requestMatchers(HttpMethod.GET, "/api/students/**").hasAnyRole("ADM", "STUDENT", "PROFESSOR")
40+
.requestMatchers("/api/students/**").hasAnyRole("ADM", "STUDENT")
41+
42+
// * Administrators permissions
43+
// .requestMatchers(HttpMethod.POST, "/api/administrators/register").permitAll()
44+
.requestMatchers("/api/administrators/**").hasRole("ADM")
45+
46+
// * Professors permissions
47+
.requestMatchers("/api/professors/**").hasAnyRole("ADM", "PROFESSOR")
48+
49+
// * Librarians permissions
50+
.requestMatchers("/api/librarians/**").hasAnyRole("ADM", "LIBRARIAN")
51+
52+
// * Classrooms permissions
53+
.requestMatchers(HttpMethod.GET, "/api/classrooms/**").hasAnyRole("ADM", "PROFESSOR", "STUDENT")
54+
.requestMatchers("/api/classrooms/**").hasRole("ADM")
55+
56+
// * Books permissions
57+
.requestMatchers(HttpMethod.POST, "/api/books/**").hasRole("LIBRARIAN")
58+
.requestMatchers(HttpMethod.PUT, "/api/books/**").hasRole("LIBRARIAN")
59+
60+
// * Stats permissions
61+
.requestMatchers(HttpMethod.GET, "/api/stats/**").hasAnyRole("ADM", "PROFESSOR", "STUDENT")
62+
.requestMatchers("/api/stats/**").hasRole("STUDENT")
63+
64+
// * Summaries permissions
65+
.requestMatchers(HttpMethod.GET, "/api/summaries/**").hasAnyRole("PROFESSOR", "STUDENT")
66+
.requestMatchers("/api/summaries/**").hasRole("STUDENT")
67+
68+
// * History permissions
69+
.requestMatchers(HttpMethod.GET, "/api/read-history").hasAnyRole("PROFESSOR", "STUDENT")
70+
.requestMatchers(HttpMethod.POST, "/api/read-history").hasRole("STUDENT")
71+
72+
// * Groups/Group-Books permissions
73+
.requestMatchers("/api/groups/**", "/api/group-books/**").hasRole("STUDENT")
4974
.anyRequest().authenticated()
5075
)
5176
.addFilterBefore(securityFilter, UsernamePasswordAuthenticationFilter.class)

0 commit comments

Comments
 (0)