Skip to content

Commit

Permalink
Task 60 : Add missing Java Doc for API Gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Jul 21, 2024
1 parent cc66927 commit 19c2f69
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Feign client interface named {@link UserServiceClient} for interacting with the User Service.
* This client is used to perform various operations related to user management,
* such as validating tokens.
*/
@FeignClient(name = "userservice", path = "/api/v1/users")
public interface UserServiceClient {

/**
* Validates the given token by making a POST request to the User Service.
*
* @param token the token to be validated
*/
@PostMapping("/validate-token")
void validateToken(@RequestParam String token);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,50 @@

import java.util.List;

/**
* A custom Gateway filter named {@link JwtAuthenticationFilter} that handles JWT authentication for requests.
* This filter validates JWT tokens for all requests except those to public endpoints.
*/
@Component
@Slf4j
public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAuthenticationFilter.Config> {


/**
* Configuration class for JwtAuthenticationFilter.
* It holds a list of public endpoints that should not be filtered.
*/
public static class Config {
// List of public endpoints that should not be filtered
private List<String> publicEndpoints;

/**
* Gets the list of public endpoints.
*
* @return the list of public endpoints
*/
public List<String> getPublicEndpoints() {
return publicEndpoints;
}

/**
* Sets the list of public endpoints.
*
* @param publicEndpoints the list of public endpoints to set
* @return the updated Config object
*/
public Config setPublicEndpoints(List<String> publicEndpoints) {
this.publicEndpoints = publicEndpoints;
return this;
}

}

/**
* Applies the JWT authentication filter to the gateway.
*
* @param config the configuration for the filter
* @return the gateway filter
*/
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
* Represents a Token that includes an access token, its expiration time, and a refresh token.
* Provides utility methods to check if a token is a Bearer token and to extract the JWT from an authorization header.
*/
@Getter
@Builder
public class Token {
Expand All @@ -14,11 +18,23 @@ public class Token {

private static final String TOKEN_PREFIX = "Bearer ";

/**
* Checks if the given authorization header is a Bearer token.
*
* @param authorizationHeader the authorization header to check
* @return true if the authorization header is a Bearer token, false otherwise
*/
public static boolean isBearerToken(final String authorizationHeader) {
return StringUtils.hasText(authorizationHeader) &&
authorizationHeader.startsWith(TOKEN_PREFIX);
}

/**
* Extracts the JWT from the given Bearer token authorization header.
*
* @param authorizationHeader the authorization header containing the Bearer token
* @return the JWT extracted from the authorization header
*/
public static String getJwt(final String authorizationHeader) {
return authorizationHeader.replace(TOKEN_PREFIX, "");
}
Expand Down

0 comments on commit 19c2f69

Please sign in to comment.