-
Notifications
You must be signed in to change notification settings - Fork 3
Fix/security merge conflicts #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
07236b9
feat: add creation of users by token
Juan-Rpenuela 3d29270
feat: complete implemetation jwt
Juan-Rpenuela 6b17199
fix: merge conflicts
Juan-Rpenuela 9357c54
feat:implemented security and role management inside the endpoints
Juan-Rpenuela 78f1542
fix: merge conflicts and UserController unit test
Juan-Rpenuela 2749580
fix: permission with endpoint create
Juan-Rpenuela c5badd2
Merge branch 'develop' into fix/security-MergeConflicts
cris-eci 08ac975
fix: merge problems with develop
Juan-Rpenuela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/edu/eci/cvds/prometeo/config/JwtRequestFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package edu.eci.cvds.prometeo.config; | ||
|
|
||
| import edu.eci.cvds.prometeo.util.JwtUtil; | ||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
| import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
| import org.springframework.security.core.context.SecurityContextHolder; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
|
|
||
| @Component | ||
| public class JwtRequestFilter extends OncePerRequestFilter { | ||
|
|
||
| private final JwtUtil jwtUtil; | ||
|
|
||
| public JwtRequestFilter(JwtUtil jwtUtil) { | ||
| this.jwtUtil = jwtUtil; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doFilterInternal(HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain chain) | ||
| throws ServletException, IOException { | ||
| final String authHeader = request.getHeader("Authorization"); | ||
|
|
||
| System.out.println("🔍 Checking Authorization header..."); | ||
| if (authHeader != null && authHeader.startsWith("Bearer ")) { | ||
| System.out.println("✅ Authorization header found: " + authHeader); | ||
|
|
||
| try { | ||
| var claims = jwtUtil.extractClaims(authHeader); | ||
|
|
||
| String username = claims.get("userName", String.class); | ||
| String role = claims.get("role", String.class).toUpperCase(); | ||
| String name = claims.get("name", String.class); | ||
| String idCard = claims.get("id", String.class); | ||
|
|
||
| // Log extracted claims | ||
| System.out.println("✅ JWT Claims extracted:"); | ||
| System.out.println("username = " + username); | ||
| System.out.println("role = " + role); | ||
| System.out.println("name = " + name); | ||
| System.out.println("idCard = " + idCard); | ||
|
|
||
| // Save attributes in the request | ||
| request.setAttribute("username", username); | ||
| request.setAttribute("role", role); | ||
| request.setAttribute("name", name); | ||
| request.setAttribute("institutionalId", idCard); | ||
|
|
||
| // Set authentication in SecurityContext | ||
| var authorities = List.of(new SimpleGrantedAuthority("ROLE_" + role)); | ||
| var auth = new UsernamePasswordAuthenticationToken(username, null, authorities); | ||
| SecurityContextHolder.getContext().setAuthentication(auth); | ||
|
|
||
| } catch (Exception e) { | ||
| System.out.println("❌ Error extracting JWT claims: " + e.getMessage()); | ||
| response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid token"); | ||
| return; | ||
| } | ||
| } else { | ||
| System.out.println("⚠️ Authorization header is missing or does not start with 'Bearer '"); | ||
| } | ||
|
|
||
| chain.doFilter(request, response); | ||
| System.out.println("🔍 Post-filter role: " + request.getAttribute("role")); | ||
| } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/edu/eci/cvds/prometeo/config/LoggingFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package edu.eci.cvds.prometeo.config; | ||
| import jakarta.servlet.FilterChain; | ||
| import jakarta.servlet.ServletException; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.web.filter.OncePerRequestFilter; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class LoggingFilter extends OncePerRequestFilter { | ||
| @Override | ||
| protected void doFilterInternal(HttpServletRequest request, | ||
| HttpServletResponse response, | ||
| FilterChain filterChain) | ||
| throws ServletException, IOException { | ||
| System.out.println("🔍 Request URI: " + request.getRequestURI()); | ||
| System.out.println("🔍 Method: " + request.getMethod()); | ||
| System.out.println("🔍 All Attributes: "); | ||
| request.getAttributeNames().asIterator().forEachRemaining(attr -> | ||
| System.out.println(attr + " = " + request.getAttribute(attr)) | ||
| ); | ||
| filterChain.doFilter(request, response); | ||
| } | ||
| } | ||
32 changes: 23 additions & 9 deletions
32
src/main/java/edu/eci/cvds/prometeo/config/SecurityConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,40 @@ | ||
| package edu.eci.cvds.prometeo.config; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
| import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
| import org.springframework.security.web.SecurityFilterChain; | ||
| import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
|
||
| @Slf4j | ||
| @Configuration | ||
| @EnableWebSecurity | ||
| public class SecurityConfig { | ||
|
|
||
|
|
||
| private final JwtRequestFilter jwtRequestFilter; | ||
|
|
||
| public SecurityConfig(JwtRequestFilter jwtRequestFilter) { | ||
| this.jwtRequestFilter = jwtRequestFilter; | ||
| } | ||
|
|
||
| @Bean | ||
| public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
| // Configuración que desactiva toda la seguridad | ||
| http | ||
| .csrf(csrf -> csrf.disable()) | ||
| .authorizeHttpRequests(authorize -> authorize | ||
| .requestMatchers("/**").permitAll() | ||
| ) | ||
| .formLogin(form -> form.disable()) | ||
| .httpBasic(basic -> basic.disable()); | ||
|
|
||
| .csrf(csrf -> csrf.disable()) | ||
| .authorizeHttpRequests(auth -> auth | ||
|
|
||
| .requestMatchers("/api/users/create").authenticated() | ||
|
|
||
| .requestMatchers("/api/users/trainer/**").hasRole("TRAINER") | ||
|
|
||
| .anyRequest().hasAnyRole("TRAINER", "STUDENT", "ADMIN") | ||
| ) | ||
| .formLogin(form -> form.disable()) | ||
| .httpBasic(basic -> basic.disable()) | ||
| .addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class) | ||
| .addFilterBefore(new LoggingFilter(), JwtRequestFilter.class); | ||
| return http.build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||||||||||||||||
| package edu.eci.cvds.prometeo.util; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import io.jsonwebtoken.Claims; | ||||||||||||||||||||||||||
| import io.jsonwebtoken.Jwts; | ||||||||||||||||||||||||||
| import org.springframework.stereotype.Component; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @Component | ||||||||||||||||||||||||||
| public class JwtUtil { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| private final String SECRET_KEY = "supersecretpassword1234567891011121314"; // Debe ser la misma que usa el microservicio de usuarios | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| public Claims extractClaims(String token) { | ||||||||||||||||||||||||||
|
Comment on lines
+10
to
+12
|
||||||||||||||||||||||||||
| private final String SECRET_KEY = "supersecretpassword1234567891011121314"; // Debe ser la misma que usa el microservicio de usuarios | |
| public Claims extractClaims(String token) { | |
| private final String SECRET_KEY = System.getenv("JWT_SECRET_KEY"); | |
| public JwtUtil() { | |
| if (SECRET_KEY == null || SECRET_KEY.isEmpty()) { | |
| throw new IllegalStateException("Environment variable JWT_SECRET_KEY is not set or is empty."); | |
| } | |
| } | |
| public Claims extractClaims(String token) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider replacing System.out.println with a logging framework (e.g., SLF4J) to improve performance and manageability of logs in production.