Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea/
/Status
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '2.1'
services:
course:
build: ../microservices/course-service
image: javatab/course_service:v1
image: javatab/course_service:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker
Expand All @@ -15,7 +15,7 @@ services:

search:
build: ../microservices/search-service
image: javatab/search_service:v1
image: javatab/search_service:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker
Expand All @@ -27,7 +27,7 @@ services:

student:
build: ../microservices/student-service
image: javatab/student_service:v1
image: javatab/student_service:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker
Expand All @@ -39,7 +39,7 @@ services:

vote:
build: ../microservices/vote-service
image: javatab/vote_service:v1
image: javatab/vote_service:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker
Expand All @@ -51,7 +51,7 @@ services:

course-composite:
build: ../microservices/course-composite-service
image: javatab/course_composite_service:v1
image: javatab/course_composite_service:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker
Expand All @@ -71,7 +71,7 @@ services:
retries: 60

postgres:
image: postgres:alpine3.15
image: postgres:13.7-alpine3.16
mem_limit: 512m
ports:
- "5432:5432"
Expand Down Expand Up @@ -119,6 +119,7 @@ services:

eureka:
build: ../spring-cloud/eureka-server
image: javatab/eureka:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker
Expand All @@ -127,6 +128,7 @@ services:

gateway:
build: ../spring-cloud/gateway
image: javatab/gateway:v2
mem_limit: 512m
ports:
- "8443:8443"
Expand All @@ -144,6 +146,7 @@ services:
- CONFIG_SERVER_USR=${CONFIG_SERVER_USR}
- CONFIG_SERVER_PWD=${CONFIG_SERVER_PWD}
build: ../spring-cloud/authorization-server
image: javatab/auth-server:v2
mem_limit: 512m
#healthcheck:
# test: "curl --fail --silent http://localhost:9999/actuator/health | grep UP || exit 1"
Expand All @@ -153,6 +156,7 @@ services:

config-server:
build: ../spring-cloud/config-server
image: javatab/config-server:v2
mem_limit: 512m
environment:
- SPRING_PROFILES_ACTIVE=docker,native
Expand Down
2 changes: 1 addition & 1 deletion microservices/course-composite-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.javatab.microservices.composite.course</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
scopes = {
@OAuthScope(name = "course:read", description =
"read scope"),
@OAuthScope(name = "write:write", description =
@OAuthScope(name = "course:write", description =
"write scope")
}
)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.javatab.microservices.util.http.HttpErrorInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatusCode;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
Expand All @@ -20,6 +21,8 @@
import java.io.IOException;

import static java.util.logging.Level.FINE;
import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY;

@Component
public class CourseCompositeIntegration implements CourseService, StudentService, VoteService {
Expand Down Expand Up @@ -50,26 +53,20 @@ private String getErrorMessage(WebClientResponseException ex) {

private Throwable handleException(Throwable ex) {

if (!(ex instanceof WebClientResponseException)) {
if (!(ex instanceof WebClientResponseException webClientResponseException)) {
LOG.warn("Got a unexpected error: {}, will rethrow it", ex.toString());
return ex;
}

WebClientResponseException wcre = (WebClientResponseException)ex;

switch (wcre.getStatusCode()) {

case NOT_FOUND:
return new NotFoundException(getErrorMessage(wcre));

case UNPROCESSABLE_ENTITY :
return new InvalidInputException(getErrorMessage(wcre));

default:
LOG.warn("Got an unexpected HTTP error: {}, will rethrow it", wcre.getStatusCode());
LOG.warn("Error body: {}", wcre.getResponseBodyAsString());
return ex;
HttpStatusCode statusCode = webClientResponseException.getStatusCode();
if (NOT_FOUND.equals(statusCode)) {
return new NotFoundException(getErrorMessage(webClientResponseException));
} else if (UNPROCESSABLE_ENTITY.equals(statusCode)) {
return new InvalidInputException(getErrorMessage(webClientResponseException));
}
LOG.warn("Got an unexpected HTTP error: {}, will rethrow it", webClientResponseException.getStatusCode());
LOG.warn("Error body: {}", webClientResponseException.getResponseBodyAsString());
return ex;
}
@Override
public Mono<Course> getCourse(int courseId) {
Expand Down
2 changes: 1 addition & 1 deletion microservices/course-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.javatab.microservices.core.course</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;

import static org.junit.jupiter.api.Assertions.assertEquals;

@DataMongoTest(
excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class,
properties = {"spring.cloud.config.enabled=false"}
)
public class PersistenceTests extends MongoDbTestBase {
Expand Down
2 changes: 1 addition & 1 deletion microservices/search-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.javatab.microservices.core.search</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package io.javatab.microservices.core.search.persistence;


import co.elastic.clients.elasticsearch.core.DeleteResponse;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.javatab.microservices.api.core.course.Course;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchClient;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Mono;

Expand All @@ -30,7 +27,8 @@ public Mono<IndexResponse> createCourse(SearchEntity course) {
String id = UUID.randomUUID().toString();
Map documentMapper = objectMapper.convertValue(course,
Map.class);
return client.index(indexRequest -> indexRequest.index(COURSE_INDEX).id(id).source(documentMapper));
// return client.index(indexRequest -> indexRequest.index(COURSE_INDEX).id(id).source(documentMapper));
return client.index(indexRequest -> indexRequest.index(COURSE_INDEX).id(id).document(documentMapper));
}

public Mono<DeleteResponse> deleteCourse() {
Expand Down
2 changes: 1 addition & 1 deletion microservices/student-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.javatab.microservices.core.student</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.javatab.microservices.core.student.persistence;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import javax.persistence.*;

@Entity
@Table(name = "student")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.javatab.microservices.core.student.persistence;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

public interface StudentRepository extends PagingAndSortingRepository<StudentEntity, Integer> {
@Repository
public interface StudentRepository extends JpaRepository<StudentEntity, Integer> {

}
2 changes: 1 addition & 1 deletion microservices/vote-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.javatab.microservices.core.vote</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,50 @@
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.oauth2.server.resource.OAuth2ResourceServerConfigurer;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.core.oidc.OidcScopes;
import org.springframework.security.oauth2.server.authorization.client.InMemoryRegisteredClientRepository;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configuration.OAuth2AuthorizationServerConfiguration;
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
import org.springframework.security.oauth2.server.authorization.settings.ClientSettings;
import org.springframework.security.oauth2.server.authorization.settings.TokenSettings;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

import java.time.Duration;
import java.util.UUID;

@Configuration(proxyBeanMethods = false)
@Import(OAuth2AuthorizationServerConfiguration.class)
public class AuthorizationServerConfig {

private static final Logger LOG = LoggerFactory.getLogger(AuthorizationServerConfig.class);


@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http);
http.getConfigurer(OAuth2AuthorizationServerConfigurer.class)
.oidc(Customizer.withDefaults()); // Enable OpenID Connect 1.0

// @formatter:off
http
.exceptionHandling(exceptions ->
exceptions.authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login"))
)
.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
// @formatter:on
return http.build();
}

// @formatter:off
@Bean
public RegisteredClientRepository registeredClientRepository() {
Expand All @@ -36,7 +61,7 @@ public RegisteredClientRepository registeredClientRepository() {
RegisteredClient writerClient = RegisteredClient.withId(UUID.randomUUID().toString())
.clientId("writer")
.clientSecret("secret")
.clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
Expand All @@ -45,23 +70,23 @@ public RegisteredClientRepository registeredClientRepository() {
.scope(OidcScopes.OPENID)
.scope("product:read")
.scope("product:write")
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.tokenSettings(ts -> ts.accessTokenTimeToLive(Duration.ofHours(1)))
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
.tokenSettings(TokenSettings.builder().accessTokenTimeToLive(Duration.ofHours(1)).build())
.build();

RegisteredClient readerClient = RegisteredClient.withId(UUID.randomUUID().toString())
.clientId("reader")
.clientSecret("secret")
.clientAuthenticationMethod(ClientAuthenticationMethod.BASIC)
.clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
.authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
.redirectUri("https://my.redirect.uri")
.redirectUri("https://localhost:8443/webjars/swagger-ui/oauth2-redirect.html")
.scope(OidcScopes.OPENID)
.scope("product:read")
.clientSettings(clientSettings -> clientSettings.requireUserConsent(true))
.tokenSettings(ts -> ts.accessTokenTimeToLive(Duration.ofHours(1)))
.clientSettings(ClientSettings.builder().requireAuthorizationConsent(true).build())
.tokenSettings(TokenSettings.builder().accessTokenTimeToLive(Duration.ofHours(1)).build())
.build();
return new InMemoryRegisteredClientRepository(writerClient, readerClient);
}
Expand All @@ -73,8 +98,8 @@ public JWKSource<SecurityContext> jwkSource() {
return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
}

@Bean
/* @Bean
public ProviderSettings providerSettings() {
return new ProviderSettings().issuer("http://auth-server:9999");
}
}*/
}
Loading