Skip to content

Commit 77c5299

Browse files
Implement securitySchemeName
1 parent fb9c29c commit 77c5299

File tree

8 files changed

+25
-13
lines changed

8 files changed

+25
-13
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ SECURITY_JWT_SECRET_KEY=<JWT_SECRET_KEY> # Secret key for signing JWT tokens
5050
SECURITY_JWT_EXPIRATION=<JWT_EXPIRATION> # JWT expiration time (in ms)
5151
SECURITY_PUBLIC_ROUTES=<PUBLIC_ROUTES> # Public routes that do not require authentication (e.g., /auth/login)
5252
SECURITY_AES_KEY=<AES_KEY> # AES encryption key for sensitive data
53+
SECURITY_SCHEME_NAME=<NAME_SECURITY_SCHEMA> # Name of the security scheme used in API documentation
5354

5455
# Rate Limiting Config
5556
# Settings for API rate limiting

src/main/java/smartpot/com/api/Documentation/SwaggerConfig.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package smartpot.com.api.Documentation;
22

3+
import io.swagger.v3.oas.models.Components;
34
import io.swagger.v3.oas.models.OpenAPI;
45
import io.swagger.v3.oas.models.info.Contact;
56
import io.swagger.v3.oas.models.info.Info;
67
import io.swagger.v3.oas.models.info.License;
7-
import io.swagger.v3.oas.models.Components;
88
import io.swagger.v3.oas.models.security.SecurityRequirement;
99
import io.swagger.v3.oas.models.security.SecurityScheme;
1010
import org.springframework.beans.factory.annotation.Value;
@@ -26,10 +26,11 @@ public class SwaggerConfig {
2626
@Value("${DESCRIPTION}")
2727
private String description;
2828

29+
@Value("${SECURITY_SCHEME_NAME}")
30+
private String securitySchemeName;
2931

3032
@Bean
3133
public OpenAPI customOpenAPI() {
32-
final String securitySchemeName = "BearerAuth";
3334

3435
return new OpenAPI()
3536
.info(new Info()
@@ -44,10 +45,11 @@ public OpenAPI customOpenAPI() {
4445
.components(new Components()
4546
.addSecuritySchemes(securitySchemeName,
4647
new SecurityScheme()
47-
.name(securitySchemeName)
48-
.type(SecurityScheme.Type.HTTP)
49-
.scheme("bearer")
50-
.bearerFormat("JWT"))
48+
.name("Authorization")
49+
.type(SecurityScheme.Type.APIKEY)
50+
.in(SecurityScheme.In.HEADER)
51+
.description("Usar el formato: " + securitySchemeName + " <token>")
52+
)
5153
);
5254

5355

src/main/java/smartpot/com/api/Security/Config/Filters/JwtAuthFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected void doFilterInternal(
3838
user, user.getPassword(), null /* user.getAuthorities() */);
3939
authToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
4040
SecurityContextHolder.getContext().setAuthentication(authToken);
41-
} catch (Exception ignored) {}
41+
} catch (Exception ignored) {
42+
}
4243
filterChain.doFilter(request, response);
4344
}
4445
}

src/main/java/smartpot/com/api/Security/Config/SecurityConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
1111
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1212
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
13-
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
1413
import org.springframework.security.config.http.SessionCreationPolicy;
1514
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
1615
import org.springframework.security.crypto.password.PasswordEncoder;

src/main/java/smartpot/com/api/Security/Controller/AuthController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public ResponseEntity<?> register(@RequestBody UserDTO reqUser) {
8181
);
8282
}
8383
}
84+
8485
@PostMapping("/password/forgot")
8586
@Operation(
8687
summary = "Solicitud de recuperación de contraseña",

src/main/java/smartpot/com/api/Security/Service/AESEncryptionService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public class AESEncryptionService implements EncryptionServiceI {
2020
@Value("${application.security.aes.key}")
2121
private String aesKey;
2222

23-
public AESEncryptionService() {}
23+
public AESEncryptionService() {
24+
}
2425

2526
private SecretKey getSecretKey() {
2627
byte[] decoded = Base64.getDecoder().decode(aesKey);
@@ -50,7 +51,8 @@ public String encrypt(String data) throws EncryptionException {
5051
System.arraycopy(iv, 0, output, 0, iv.length);
5152
System.arraycopy(encrypted, 0, output, iv.length, encrypted.length);
5253
return Base64.getUrlEncoder().encodeToString(output);
53-
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | // if you need more specific errors, catch each one separately
54+
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
55+
// if you need more specific errors, catch each one separately
5456
InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
5557
throw new EncryptionException("Error while encrypting data");
5658
}
@@ -74,7 +76,8 @@ public String decrypt(String encryptedData) throws EncryptionException {
7476

7577
// remove salt
7678
return result.split(":", 2)[1];
77-
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | // if you need more specific errors, catch each one separately
79+
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException |
80+
// if you need more specific errors, catch each one separately
7881
InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
7982
throw new EncryptionException("Error while decrypting data");
8083
}

src/main/java/smartpot/com/api/Security/Service/EncryptionServiceI.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111

1212
public interface EncryptionServiceI {
1313
String encrypt(String plainText) throws EncryptionException, NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException;
14+
1415
String decrypt(String cipherText) throws EncryptionException;
1516
}

src/main/java/smartpot/com/api/Security/Service/JwtService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
import smartpot.com.api.Security.Model.DTO.ResetTokenDTO;
1919
import smartpot.com.api.Users.Model.DTO.UserDTO;
2020
import smartpot.com.api.Users.Service.SUserI;
21+
2122
import javax.crypto.SecretKey;
22-
import java.util.*;
23+
import java.util.Date;
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
import java.util.Optional;
2327

2428
@Service
2529
public class JwtService implements JwtServiceI {
@@ -142,7 +146,7 @@ public Boolean forgotPassword(String email) throws Exception {
142146
throw new ValidationException(e);
143147
}
144148
})
145-
.map(token -> new ResetTokenDTO(token, "reset", new Date(System.currentTimeMillis() + expiration) ))
149+
.map(token -> new ResetTokenDTO(token, "reset", new Date(System.currentTimeMillis() + expiration)))
146150
.map(token -> {
147151
try {
148152
return encryptionService.encrypt(ResetTokenDTO.convertToJson(token));

0 commit comments

Comments
 (0)