Skip to content

Commit d54873c

Browse files
committedMay 18, 2021
12: Added swagger in user, order and notification server.
1 parent 154ddf0 commit d54873c

File tree

32 files changed

+561
-49
lines changed

32 files changed

+561
-49
lines changed
 

‎document/Auth2-0-Generate-Token.PNG

84 KB
Loading
59.7 KB
Loading
67.1 KB
Loading
67.8 KB
Loading
File renamed without changes.
File renamed without changes.

‎readme.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Spring boot microservice example with Eureka Server + Eureka Client + Spring Clo
55
- **Eureka Server** : Eureka service registry
66
- **Spring Cloud API Gateway**: API Gateway which is responsible to route the request to specific microservice
77
- **Spring OAuth2.0**: Authentication service and responsible to secure the end points.
8+
- Eureka Client
89
- Generate OAuth token
910
- Validate the OAuth token
1011
- **User Service**: User microservice with a basic feature
@@ -13,15 +14,18 @@ Spring boot microservice example with Eureka Server + Eureka Client + Spring Clo
1314
- Supports RestTemplate Client
1415
- Supports Resilience4J circuit breaker
1516
- Use of Resilience4J circuit breaker with RestTemplate
16-
- **Order Service**: Order microservice with a basic feature (Eureka client)
17+
- Swagger Document
18+
- **Order Service**: Order microservice with a basic feature
1719
- Eureka Client
1820
- OAuth2.0 Client
1921
- Supports FeignClient Client
2022
- Supports Resilience4J circuit breaker
2123
- Use of Resilience4J circuit breaker with FeignClient
24+
- Swagger Document
2225
- **Notification Service**: Notification microservice with basic feature
2326
- Eureka Client
2427
- OAuth2.0 Client
28+
- Swagger Document
2529

2630
## Checkout repository
2731

@@ -67,7 +71,7 @@ Eureka server is running 8761 port, Now let's open it. Where we can check follow
6771

6872
### Eureka server : [http://localhost:8761/](http://localhost:8761/)
6973

70-
![eureka server](eureka-server.PNG)
74+
![eureka server](document/eureka-server.PNG)
7175

7276
### Step 3: Configure Zipkin Server (Optional)
7377

@@ -78,10 +82,12 @@ Eureka server is running 8761 port, Now let's open it. Where we can check follow
7882
```
7983
3. Open zipkin server : [http://localhost:9411/](http://localhost:9411/)
8084

81-
![Zipkin-Server](Zipkin-Server.PNG)
85+
![Zipkin-Server](document/Zipkin-Server.PNG)
8286

8387
## Step 4: Generate OAuth2.0 token
8488

89+
### 4.1 Using curl
90+
8591
```sh
8692
curl -X POST \
8793
http://localhost:8080/oauth/token \
@@ -93,7 +99,19 @@ curl -X POST \
9399
-F username=zone1 \
94100
-F password=mypassword
95101
```
102+
### 4.2 Using swagger
103+
In the swagger UI, There is a "Autorize" button on the top, click on it it will open the following popup:
104+
105+
![eureka server](./document/Auth2-0-Generate-Token.PNG)
106+
107+
Use the following information
96108

109+
```
110+
Username: zone1
111+
Password: mypassword
112+
ClientId: javadeveloperzone
113+
Secret: secret
114+
```
97115
Output
98116

99117
```sh
@@ -108,6 +126,7 @@ Output
108126

109127
## Step 5: Create user using user microservice
110128

129+
### 5.1 Using curl
111130
```
112131
curl -X POST \
113132
http://localhost:8080/user/ \
@@ -118,9 +137,14 @@ curl -X POST \
118137
-d '{"firstName":"subhash", "lastName": "Lamba"}'
119138
120139
```
140+
### 5.2 Using Swagger UI
141+
142+
![eureka server](./document/User-Microservice-Swagger.PNG)
121143

122144
## Step 6: Create order using order microservice
123145

146+
147+
### 6.1 Using curl
124148
```
125149
curl -X POST \
126150
http://localhost:8080/order/ \
@@ -131,8 +155,14 @@ curl -X POST \
131155
-d '{"userId":1, "orderDate": "2021-01-01"}'
132156
```
133157

158+
### 6.2 Using Swagger UI
159+
160+
![eureka server](./document/Order-Microservice-Swagger.PNG)
161+
162+
134163
## Step 7: Get user and order using user microservice
135164

165+
### 7.1 Using curl
136166
```
137167
curl -X GET \
138168
http://localhost:8080/user/1 \
@@ -160,5 +190,11 @@ curl -X GET \
160190
}
161191
```
162192

193+
### 7.1 Using Swagger UI
194+
195+
![eureka server](./document/User-Microservice-Swagger.PNG)
196+
197+
198+
163199

164200

‎spring-boot-cloud-api-gateway-routing/src/main/java/com/javadeveloperzone/SpringBootConfig.java

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.cors.reactive.CorsWebFilter;
9+
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
10+
11+
import java.util.Arrays;
12+
import java.util.Collections;
613

714
@SpringBootApplication
815
@EnableEurekaClient
@@ -11,3 +18,21 @@ public static void main(String[] args) {
1118
SpringApplication.run(SpringBootConfig.class, args); // it wil start application
1219
}
1320
}
21+
22+
@Configuration
23+
class CorsConfiguration{
24+
@Bean
25+
public CorsWebFilter corsWebFilter() {
26+
27+
org.springframework.web.cors.CorsConfiguration corsConfig = new org.springframework.web.cors.CorsConfiguration();
28+
corsConfig.setAllowedOrigins(Collections.singletonList("*"));
29+
corsConfig.setMaxAge(3600L);
30+
corsConfig.setAllowedMethods(Arrays.asList("GET", "POST"));
31+
corsConfig.addAllowedHeader("*");
32+
33+
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
34+
source.registerCorsConfiguration("/**", corsConfig);
35+
36+
return new CorsWebFilter(source);
37+
}
38+
}

‎spring-boot-cloud-authentication-service/src/main/java/com/javadeveloperzone/config/SecurityConfig.java

+16
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
99
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
1010
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
11+
import org.springframework.web.cors.CorsConfiguration;
12+
import org.springframework.web.cors.CorsConfigurationSource;
13+
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
14+
15+
import java.util.Arrays;
1116

1217
@Configuration
1318
public class SecurityConfig extends WebSecurityConfigurerAdapter {
1419

1520
@Override
1621
protected void configure(HttpSecurity http) throws Exception {
1722
http
23+
.cors().and()
1824
.antMatcher("/**")
1925
.authorizeRequests()
2026
.antMatchers("/", "/login**")
@@ -46,4 +52,14 @@ public static NoOpPasswordEncoder passwordEncoder() {
4652
public AuthenticationManager authenticationManagerBean() throws Exception {
4753
return super.authenticationManagerBean();
4854
}
55+
56+
@Bean
57+
CorsConfigurationSource corsConfigurationSource() {
58+
CorsConfiguration configuration = new CorsConfiguration();
59+
configuration.setAllowedOrigins(Arrays.asList("*"));
60+
configuration.setAllowedMethods(Arrays.asList("GET","POST"));
61+
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
62+
source.registerCorsConfiguration("/**", configuration);
63+
return source;
64+
}
4965
}

‎spring-boot-cloud-authentication-service/src/main/resources/application.properties

Whitespace-only changes.

‎spring-boot-cloud-authentication-service/src/main/resources/application.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ spring:
66
name: subhash
77
password: subhash
88
server:
9-
port: 8686
9+
port: 8484
1010
eureka:
1111
instance:
1212
hostname: localhost

‎spring-boot-cloud-eureka-notification-service/pom.xml

+17-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<groupId>com.javadeveloperzone</groupId>
1212
<artifactId>microservce-example</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
14-
<name>spring-microservice-user-service</name>
15-
<description>Sprig boot microservice user service</description>
14+
<name>spring-microservice-notification-service</name>
15+
<description>Sprig boot microservice notification service</description>
1616
<properties>
1717
<java.version>11</java.version>
1818
<spring-cloud.version>2020.0.2</spring-cloud.version>
@@ -85,7 +85,21 @@
8585
<artifactId>h2</artifactId>
8686
<scope>runtime</scope>
8787
</dependency>
88-
88+
<dependency>
89+
<groupId>io.springfox</groupId>
90+
<artifactId>springfox-swagger2</artifactId>
91+
<version>2.7.0</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>io.springfox</groupId>
95+
<artifactId>springfox-swagger-ui</artifactId>
96+
<version>2.7.0</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>io.springfox</groupId>
100+
<artifactId>springfox-bean-validators</artifactId>
101+
<version>2.7.0</version>
102+
</dependency>
89103
</dependencies>
90104
<dependencyManagement>
91105
<dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Notification Service - Eureka Client
2+
3+
Following API available in notification Service
4+
5+
![eureka server](../document/Notification-Microservice-Swagger.PNG)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.javadeveloperzone.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.HttpHeaders;
6+
import springfox.documentation.builders.ApiInfoBuilder;
7+
import springfox.documentation.builders.PathSelectors;
8+
import springfox.documentation.builders.RequestHandlerSelectors;
9+
import springfox.documentation.service.*;
10+
import springfox.documentation.spi.DocumentationType;
11+
import springfox.documentation.spi.service.contexts.SecurityContext;
12+
import springfox.documentation.spring.web.plugins.Docket;
13+
import springfox.documentation.swagger.web.ApiKeyVehicle;
14+
import springfox.documentation.swagger.web.SecurityConfiguration;
15+
import springfox.documentation.swagger2.annotations.EnableSwagger2;
16+
17+
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
@Configuration
23+
@EnableSwagger2
24+
public class SwaggerConfig {
25+
26+
@Bean
27+
public Docket productApi() {
28+
return new Docket(DocumentationType.SWAGGER_2)
29+
30+
.select()
31+
.apis(RequestHandlerSelectors.any())
32+
.paths(PathSelectors.any())
33+
.build()
34+
.securityContexts(Collections.singletonList(securityContext()))
35+
.securitySchemes(Arrays.asList(securitySchema()))
36+
.apiInfo(apiEndPointsInfo());
37+
}
38+
39+
40+
private ApiInfo apiEndPointsInfo() {
41+
return new ApiInfoBuilder().title("Job Platform REST API")
42+
.description("REST APIs for Job Platform Application")
43+
.contact(new Contact("Subhash Lamba", "https://prominentpixel.com/", "info@prominentpixel.com"))
44+
.license("Apache 2.0")
45+
.licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
46+
.version("1.0-SNAPSHOT")
47+
.build();
48+
}
49+
50+
private OAuth securitySchema() {
51+
52+
List<AuthorizationScope> authorizationScopeList = new ArrayList<>();
53+
authorizationScopeList.add(new AuthorizationScope("read", "read all"));
54+
authorizationScopeList.add(new AuthorizationScope("write", "access all"));
55+
56+
List<GrantType> grantTypes = new ArrayList();
57+
GrantType passwordCredentialsGrant = new ResourceOwnerPasswordCredentialsGrant("http://localhost:8080/oauth/token");
58+
grantTypes.add(passwordCredentialsGrant);
59+
60+
return new OAuth("oauth2", authorizationScopeList, grantTypes);
61+
}
62+
63+
private SecurityContext securityContext() {
64+
return SecurityContext.builder().securityReferences(defaultAuth())
65+
.build();
66+
}
67+
68+
private List<SecurityReference> defaultAuth() {
69+
70+
final AuthorizationScope[] authorizationScopes = new AuthorizationScope[3];
71+
authorizationScopes[0] = new AuthorizationScope("read", "read all");
72+
authorizationScopes[1] = new AuthorizationScope("trust", "trust all");
73+
authorizationScopes[2] = new AuthorizationScope("write", "write all");
74+
75+
return Collections.singletonList(new SecurityReference("oauth2", authorizationScopes));
76+
}
77+
78+
@Bean
79+
public SecurityConfiguration security() {
80+
return new SecurityConfiguration
81+
("client", "secret", "", "", "", ApiKeyVehicle.HEADER, HttpHeaders.AUTHORIZATION,"");
82+
}
83+
84+
/**
85+
*
86+
* @return ApiInf
87+
*/
88+
private ApiInfo apiInfo() {
89+
return new ApiInfoBuilder().title("Authentication API").description("")
90+
.termsOfServiceUrl("https://www.example.com/api")
91+
.contact(new Contact("Developers", "https://projects.spring.io/spring-boot/", ""))
92+
.license("Open Source")
93+
.licenseUrl("\"https://www.apache.org/licenses/LICENSE-2.0")
94+
.version("1.0.0")
95+
.build();
96+
97+
}
98+
}

‎spring-boot-cloud-eureka-notification-service/src/main/resources/application.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ spring:
55
base-url: http://localhost:9411/
66

77
server:
8-
port: 0
8+
port: 8383
99
eureka:
1010
instance:
1111
hostname: localhost

‎spring-boot-cloud-eureka-order-service/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,21 @@
9090
<artifactId>h2</artifactId>
9191
<scope>runtime</scope>
9292
</dependency>
93+
<dependency>
94+
<groupId>io.springfox</groupId>
95+
<artifactId>springfox-swagger2</artifactId>
96+
<version>2.7.0</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>io.springfox</groupId>
100+
<artifactId>springfox-swagger-ui</artifactId>
101+
<version>2.7.0</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>io.springfox</groupId>
105+
<artifactId>springfox-bean-validators</artifactId>
106+
<version>2.7.0</version>
107+
</dependency>
93108
</dependencies>
94109
<dependencyManagement>
95110
<dependencies>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Order Service - Eureka Client
2+
3+
Following API available in Order Service
4+
5+
![eureka server](../document/Order-Microservice-Swagger.PNG)

0 commit comments

Comments
 (0)
Please sign in to comment.