init commit
This commit is contained in:
33
.gitignore
vendored
Normal file
33
.gitignore
vendored
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
HELP.md
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
96
auth-service/pom.xml
Normal file
96
auth-service/pom.xml
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>auth-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
|
||||||
|
<jbcrypt.version>0.4</jbcrypt.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-config-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-api</artifactId>
|
||||||
|
<version>0.11.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-impl</artifactId>
|
||||||
|
<version>0.11.1</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-jackson</artifactId>
|
||||||
|
<version>0.11.1</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mindrot</groupId>
|
||||||
|
<artifactId>jbcrypt</artifactId>
|
||||||
|
<version>${jbcrypt.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
22
auth-service/src/main/java/com/AuthApplication.java
Normal file
22
auth-service/src/main/java/com/AuthApplication.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com;
|
||||||
|
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class AuthApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(AuthApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@LoadBalanced
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.controllers;
|
||||||
|
|
||||||
|
import com.entities.AuthRequest;
|
||||||
|
import com.entities.AuthResponse;
|
||||||
|
import com.services.AuthService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/auth")
|
||||||
|
public class AuthController {
|
||||||
|
|
||||||
|
private final AuthService authService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public AuthController(final AuthService authService) {
|
||||||
|
this.authService = authService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = "/register")
|
||||||
|
public ResponseEntity<AuthResponse> register(@RequestBody AuthRequest authRequest) {
|
||||||
|
return ResponseEntity.ok(authService.register(authRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
18
auth-service/src/main/java/com/entities/AuthRequest.java
Normal file
18
auth-service/src/main/java/com/entities/AuthRequest.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.entities;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class AuthRequest {
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
private String password;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
}
|
||||||
17
auth-service/src/main/java/com/entities/AuthResponse.java
Normal file
17
auth-service/src/main/java/com/entities/AuthResponse.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.entities;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class AuthResponse {
|
||||||
|
|
||||||
|
private String accessToken;
|
||||||
|
private String refreshToken;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.entities.value_objects;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@Data
|
||||||
|
public class UserVO {
|
||||||
|
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String email;
|
||||||
|
private String password;
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
}
|
||||||
38
auth-service/src/main/java/com/services/AuthService.java
Normal file
38
auth-service/src/main/java/com/services/AuthService.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.services;
|
||||||
|
|
||||||
|
import com.entities.AuthRequest;
|
||||||
|
import com.entities.AuthResponse;
|
||||||
|
import com.entities.value_objects.UserVO;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AuthService {
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
private final JwtUtil jwt;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public AuthService(RestTemplate restTemplate,
|
||||||
|
final JwtUtil jwt) {
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
this.jwt = jwt;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AuthResponse register(AuthRequest authRequest) {
|
||||||
|
//do validation if user already exists
|
||||||
|
authRequest.setPassword(BCrypt.hashpw(authRequest.getPassword(), BCrypt.gensalt()));
|
||||||
|
|
||||||
|
UserVO userVO = restTemplate.postForObject("http://user-service/users", authRequest, UserVO.class);
|
||||||
|
Assert.notNull(userVO, "Failed to register user. Please try again later");
|
||||||
|
|
||||||
|
String accessToken = jwt.generate(userVO, "ACCESS");
|
||||||
|
String refreshToken = jwt.generate(userVO, "REFRESH");
|
||||||
|
|
||||||
|
return new AuthResponse(accessToken, refreshToken);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
76
auth-service/src/main/java/com/services/JwtUtil.java
Normal file
76
auth-service/src/main/java/com/services/JwtUtil.java
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package com.services;
|
||||||
|
|
||||||
|
import com.entities.value_objects.UserVO;
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.security.Key;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtUtil {
|
||||||
|
|
||||||
|
@Value("${jwt.secret}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
|
@Value("${jwt.expiration}")
|
||||||
|
private String expirationTime;
|
||||||
|
|
||||||
|
private Key key;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
this.key = Keys.hmacShaKeyFor(secret.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Claims getAllClaimsFromToken(String token) {
|
||||||
|
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Date getExpirationDateFromToken(String token) {
|
||||||
|
return getAllClaimsFromToken(token).getExpiration();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Boolean isTokenExpired(String token) {
|
||||||
|
final Date expiration = getExpirationDateFromToken(token);
|
||||||
|
return expiration.before(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String generate(UserVO userVO, String type) {
|
||||||
|
Map<String, Object> claims = new HashMap<>();
|
||||||
|
claims.put("id", userVO.getId());
|
||||||
|
claims.put("role", userVO.getRole());
|
||||||
|
return doGenerateToken(claims, userVO.getEmail(), type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String doGenerateToken(Map<String, Object> claims, String username, String type) {
|
||||||
|
long expirationTimeLong;
|
||||||
|
if ("ACCESS".equals(type)) {
|
||||||
|
expirationTimeLong = Long.parseLong(expirationTime) * 1000;
|
||||||
|
} else {
|
||||||
|
expirationTimeLong = Long.parseLong(expirationTime) * 1000 * 5;
|
||||||
|
}
|
||||||
|
final Date createdDate = new Date();
|
||||||
|
final Date expirationDate = new Date(createdDate.getTime() + expirationTimeLong);
|
||||||
|
|
||||||
|
return Jwts.builder()
|
||||||
|
.setClaims(claims)
|
||||||
|
.setSubject(username)
|
||||||
|
.setIssuedAt(createdDate)
|
||||||
|
.setExpiration(expirationDate)
|
||||||
|
.signWith(key)
|
||||||
|
.compact();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean validateToken(String token) {
|
||||||
|
return !isTokenExpired(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
10
auth-service/src/main/resources/application.yml
Normal file
10
auth-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
server:
|
||||||
|
port: 9004
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: auth-service
|
||||||
|
|
||||||
|
jwt:
|
||||||
|
secret: BvPHGM8C0ia4uOuxxqPD5DTbWC9F9TWvPStp3pb7ARo0oK2mJ3pd3YG4lxA9i8bj6OTbadwezxgeEByY
|
||||||
|
expiration: 86400
|
||||||
5
auth-service/src/main/resources/bootstrap.yml
Normal file
5
auth-service/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
enabled: true
|
||||||
|
uri: http://localhost:9296
|
||||||
62
config-service/pom.xml
Normal file
62
config-service/pom.xml
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>config-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-config-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
17
config-service/src/main/java/com/ConfigApplication.java
Normal file
17
config-service/src/main/java/com/ConfigApplication.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.config.server.EnableConfigServer;
|
||||||
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableEurekaClient
|
||||||
|
@EnableConfigServer
|
||||||
|
public class ConfigApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(ConfigApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
13
config-service/src/main/resources/application.yml
Normal file
13
config-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
server:
|
||||||
|
port: 9297
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: config-server
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
server:
|
||||||
|
git:
|
||||||
|
uri: https://github.com/igorkosandyak/demo-config-server.git
|
||||||
|
clone-on-start: true
|
||||||
|
|
||||||
5
config-service/src/main/resources/bootstrap.yml
Normal file
5
config-service/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
enabled: true
|
||||||
|
uri: http://localhost:9296
|
||||||
78
departments-service/pom.xml
Normal file
78
departments-service/pom.xml
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>departments-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
|
||||||
|
<jbcrypt.version>0.4</jbcrypt.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-config-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class DepartmentsApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(DepartmentsApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.controllers;
|
||||||
|
|
||||||
|
import com.entities.Department;
|
||||||
|
import com.services.DepartmentService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/departments")
|
||||||
|
@Slf4j
|
||||||
|
public class DepartmentController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DepartmentService departmentService;
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public Department save(@RequestBody Department department) {
|
||||||
|
return departmentService.save(department);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public Department getById(@PathVariable long id) {
|
||||||
|
return departmentService.getById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.entities;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@Data
|
||||||
|
@Table(name = "departments")
|
||||||
|
public class Department {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private long id;
|
||||||
|
private String name;
|
||||||
|
private String address;
|
||||||
|
private String code;
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.repos;
|
||||||
|
|
||||||
|
import com.entities.Department;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface DepartmentRepository extends JpaRepository<Department, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.services;
|
||||||
|
|
||||||
|
import com.entities.Department;
|
||||||
|
import com.repos.DepartmentRepository;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class DepartmentService {
|
||||||
|
|
||||||
|
private final DepartmentRepository repository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public DepartmentService(DepartmentRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Department save(Department department) {
|
||||||
|
return this.repository.save(department);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Department getById(long id) {
|
||||||
|
return this.repository.findById(id)
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
departments-service/src/main/resources/application.yml
Normal file
16
departments-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
server:
|
||||||
|
port: 9001
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: department-service
|
||||||
|
datasource:
|
||||||
|
username: root
|
||||||
|
password: igorkosandyak
|
||||||
|
url: jdbc:mysql://localhost:3306/microservices
|
||||||
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
jpa:
|
||||||
|
show-sql: true
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: update
|
||||||
|
properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
|
||||||
5
departments-service/src/main/resources/bootstrap.yml
Normal file
5
departments-service/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
enabled: true
|
||||||
|
uri: http://localhost:9296
|
||||||
52
discovery-service/pom.xml
Normal file
52
discovery-service/pom.xml
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>discovery-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableEurekaServer
|
||||||
|
public class DiscoveryApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(DiscoveryApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
7
discovery-service/src/main/resources/application.yml
Normal file
7
discovery-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
server:
|
||||||
|
port: 8761
|
||||||
|
|
||||||
|
eureka:
|
||||||
|
client:
|
||||||
|
register-with-eureka: false
|
||||||
|
fetch-registry: false
|
||||||
89
gateway-service/pom.xml
Normal file
89
gateway-service/pom.xml
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>gateway-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-api</artifactId>
|
||||||
|
<version>0.11.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-impl</artifactId>
|
||||||
|
<version>0.11.1</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.jsonwebtoken</groupId>
|
||||||
|
<artifactId>jjwt-jackson</artifactId>
|
||||||
|
<version>0.11.1</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.junit.vintage</groupId>
|
||||||
|
<artifactId>junit-vintage-engine</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
14
gateway-service/src/main/java/com/GatewayApplication.java
Normal file
14
gateway-service/src/main/java/com/GatewayApplication.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||||
|
|
||||||
|
@EnableEurekaClient
|
||||||
|
@SpringBootApplication
|
||||||
|
public class GatewayApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(GatewayApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.config;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||||
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
@RefreshScope
|
||||||
|
@Component
|
||||||
|
public class AuthenticationFilter implements GatewayFilter {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RouterValidator routerValidator;
|
||||||
|
@Autowired
|
||||||
|
private JwtUtil jwtUtil;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||||
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
|
|
||||||
|
if (routerValidator.isSecured.test(request)) {
|
||||||
|
if (this.isAuthMissing(request))
|
||||||
|
return this.onError(exchange, "Authorization header is missing in request", HttpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
|
final String token = this.getAuthHeader(request);
|
||||||
|
|
||||||
|
if (jwtUtil.isInvalid(token))
|
||||||
|
return this.onError(exchange, "Authorization header is invalid", HttpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
|
this.populateRequestWithHeaders(exchange, token);
|
||||||
|
}
|
||||||
|
return chain.filter(exchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*PRIVATE*/
|
||||||
|
|
||||||
|
private Mono<Void> onError(ServerWebExchange exchange, String err, HttpStatus httpStatus) {
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
|
response.setStatusCode(httpStatus);
|
||||||
|
return response.setComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getAuthHeader(ServerHttpRequest request) {
|
||||||
|
return request.getHeaders().getOrEmpty("Authorization").get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAuthMissing(ServerHttpRequest request) {
|
||||||
|
return !request.getHeaders().containsKey("Authorization");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void populateRequestWithHeaders(ServerWebExchange exchange, String token) {
|
||||||
|
Claims claims = jwtUtil.getAllClaimsFromToken(token);
|
||||||
|
exchange.getRequest().mutate()
|
||||||
|
.header("id", String.valueOf(claims.get("id")))
|
||||||
|
.header("role", String.valueOf(claims.get("role")))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
30
gateway-service/src/main/java/com/config/GatewayConfig.java
Normal file
30
gateway-service/src/main/java/com/config/GatewayConfig.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||||
|
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||||
|
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableHystrix
|
||||||
|
public class GatewayConfig {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AuthenticationFilter filter;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RouteLocator routes(RouteLocatorBuilder builder) {
|
||||||
|
return builder.routes()
|
||||||
|
.route("user-service", r -> r.path("/users/**")
|
||||||
|
.filters(f -> f.filter(filter))
|
||||||
|
.uri("lb://user-service"))
|
||||||
|
|
||||||
|
.route("auth-service", r -> r.path("/auth/**")
|
||||||
|
.filters(f -> f.filter(filter))
|
||||||
|
.uri("lb://auth-service"))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
38
gateway-service/src/main/java/com/config/JwtUtil.java
Normal file
38
gateway-service/src/main/java/com/config/JwtUtil.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package com.config;
|
||||||
|
|
||||||
|
import io.jsonwebtoken.Claims;
|
||||||
|
import io.jsonwebtoken.Jwts;
|
||||||
|
import io.jsonwebtoken.security.Keys;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.security.Key;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class JwtUtil {
|
||||||
|
|
||||||
|
@Value("${jwt.secret}")
|
||||||
|
private String secret;
|
||||||
|
|
||||||
|
private Key key;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init(){
|
||||||
|
this.key = Keys.hmacShaKeyFor(secret.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public Claims getAllClaimsFromToken(String token) {
|
||||||
|
return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isTokenExpired(String token) {
|
||||||
|
return this.getAllClaimsFromToken(token).getExpiration().before(new Date());
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isInvalid(String token) {
|
||||||
|
return this.isTokenExpired(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.config;
|
||||||
|
|
||||||
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class RouterValidator {
|
||||||
|
|
||||||
|
public static final List<String> openApiEndpoints = List.of(
|
||||||
|
"/auth/register",
|
||||||
|
"/auth/login"
|
||||||
|
);
|
||||||
|
|
||||||
|
public Predicate<ServerHttpRequest> isSecured =
|
||||||
|
request -> openApiEndpoints
|
||||||
|
.stream()
|
||||||
|
.noneMatch(uri -> request.getURI().getPath().contains(uri));
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.controller;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class FallbackController {
|
||||||
|
|
||||||
|
@GetMapping("/fallback1")
|
||||||
|
public String userFallback() {
|
||||||
|
return "User service is not available";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/auth-fallback")
|
||||||
|
public String authFallback() {
|
||||||
|
return "Auth service is not available";
|
||||||
|
}
|
||||||
|
}
|
||||||
14
gateway-service/src/main/resources/application.yml
Normal file
14
gateway-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
server:
|
||||||
|
port: 8080
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: api-gateway
|
||||||
|
cloud:
|
||||||
|
gateway:
|
||||||
|
discovery:
|
||||||
|
locator:
|
||||||
|
enabled: true
|
||||||
|
|
||||||
|
jwt:
|
||||||
|
secret: BvPHGM8C0ia4uOuxxqPD5DTbWC9F9TWvPStp3pb7ARo0oK2mJ3pd3YG4lxA9i8bj6OTbadwezxgeEByY
|
||||||
5
gateway-service/src/main/resources/bootstrap.yml
Normal file
5
gateway-service/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
enabled: true
|
||||||
|
uri: http://localhost:9296
|
||||||
32
pom.xml
Normal file
32
pom.xml
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<version>1.0</version>
|
||||||
|
<modules>
|
||||||
|
<module>config-service</module>
|
||||||
|
<module>discovery-service</module>
|
||||||
|
<module>gateway-service</module>
|
||||||
|
<module>userVO-service</module>
|
||||||
|
<module>departments-service</module>
|
||||||
|
<module>auth-service</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>2.3.8.RELEASE</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>14</maven.compiler.source>
|
||||||
|
<maven.compiler.target>14</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
78
user-service/pom.xml
Normal file
78
user-service/pom.xml
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<parent>
|
||||||
|
<artifactId>spring-cloud-demo</artifactId>
|
||||||
|
<groupId>org.igor</groupId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</parent>
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<artifactId>userVO-service</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<java.version>11</java.version>
|
||||||
|
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
|
||||||
|
<jbcrypt.version>0.4</jbcrypt.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-mongodb</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-config-server</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>${spring-cloud.version}</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
22
user-service/src/main/java/com/UserServiceApplication.java
Normal file
22
user-service/src/main/java/com/UserServiceApplication.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class UserServiceApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(UserServiceApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@LoadBalanced
|
||||||
|
public RestTemplate restTemplate() {
|
||||||
|
return new RestTemplate();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.controllers;
|
||||||
|
|
||||||
|
import com.entities.User;
|
||||||
|
import com.entities.value_objects.ResponseTemplateVO;
|
||||||
|
import com.services.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(value = "/users")
|
||||||
|
public class UserController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public User save(@RequestBody User user) {
|
||||||
|
return userService.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public ResponseTemplateVO getUser(
|
||||||
|
@RequestHeader(value = "id") String userId,
|
||||||
|
@RequestHeader(value = "role") String role) {
|
||||||
|
return userService.getUserWithDepartment(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(value = "/secure")
|
||||||
|
public String getSecure() {
|
||||||
|
return "Secure endpoint available";
|
||||||
|
}
|
||||||
|
}
|
||||||
30
user-service/src/main/java/com/entities/User.java
Normal file
30
user-service/src/main/java/com/entities/User.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package com.entities;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
import org.springframework.data.mongodb.core.mapping.Document;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
@Data
|
||||||
|
@Document(collection = "users")
|
||||||
|
public class User {
|
||||||
|
|
||||||
|
|
||||||
|
@JsonSerialize(using = ToStringSerializer.class)
|
||||||
|
private ObjectId id;
|
||||||
|
private String firstName;
|
||||||
|
private String lastName;
|
||||||
|
private String email;
|
||||||
|
private String departmentId;
|
||||||
|
private String password;
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.entities.value_objects;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class Department {
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
private String name;
|
||||||
|
private String address;
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.entities.value_objects;
|
||||||
|
|
||||||
|
import com.entities.User;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Builder
|
||||||
|
public class ResponseTemplateVO {
|
||||||
|
|
||||||
|
private User user;
|
||||||
|
private Department department;
|
||||||
|
}
|
||||||
11
user-service/src/main/java/com/repos/UserRepository.java
Normal file
11
user-service/src/main/java/com/repos/UserRepository.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package com.repos;
|
||||||
|
|
||||||
|
import com.entities.User;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface UserRepository extends MongoRepository<User, ObjectId> {
|
||||||
|
}
|
||||||
43
user-service/src/main/java/com/services/UserService.java
Normal file
43
user-service/src/main/java/com/services/UserService.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package com.services;
|
||||||
|
|
||||||
|
import com.entities.User;
|
||||||
|
import com.entities.value_objects.Department;
|
||||||
|
import com.entities.value_objects.ResponseTemplateVO;
|
||||||
|
import com.repos.UserRepository;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.bson.types.ObjectId;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@Slf4j
|
||||||
|
public class UserService {
|
||||||
|
|
||||||
|
private final UserRepository repository;
|
||||||
|
private final RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public UserService(UserRepository repository,
|
||||||
|
RestTemplate restTemplate) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public User save(User user) {
|
||||||
|
return this.repository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
public User getById(ObjectId id) {
|
||||||
|
return this.repository.findById(id).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResponseTemplateVO getUserWithDepartment(String id) {
|
||||||
|
User user = this.getById(new ObjectId(id));
|
||||||
|
|
||||||
|
Department department = restTemplate.getForObject("http://department-service/departments/" + user.getDepartmentId(), Department.class);
|
||||||
|
|
||||||
|
return new ResponseTemplateVO(user, department);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
user-service/src/main/resources/application.yml
Normal file
14
user-service/src/main/resources/application.yml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
server:
|
||||||
|
port: 9002
|
||||||
|
|
||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: user-service
|
||||||
|
data:
|
||||||
|
mongodb:
|
||||||
|
uri: mongodb://localhost:27017/microservices-1-db
|
||||||
|
|
||||||
|
|
||||||
|
jwt:
|
||||||
|
secret: BvPHGM8C0ia4uOuxxqPD5DTbWC9F9TWvPStp3pb7ARo0oK2mJ3pd3YG4lxA9i8bj6OTbadwezxgeEByY
|
||||||
|
expiration: 86400
|
||||||
5
user-service/src/main/resources/bootstrap.yml
Normal file
5
user-service/src/main/resources/bootstrap.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
spring:
|
||||||
|
cloud:
|
||||||
|
config:
|
||||||
|
enabled: true
|
||||||
|
uri: http://localhost:9296
|
||||||
Reference in New Issue
Block a user