├── .gitignore ├── .idea ├── .gitignore ├── encodings.xml ├── misc.xml ├── sonarlint │ └── issuestore │ │ ├── 0 │ │ └── b │ │ │ └── 0be7e288c9bdcd53666e01bfe770ceb076d022a7 │ │ ├── 1 │ │ └── 2 │ │ │ └── 12e2c70c44c3e10b4249c3dcdd4e72ddb0849771 │ │ ├── 3 │ │ └── 7 │ │ │ └── 377cec15f41d1e6244fa2ec4687ec13b72f9db23 │ │ ├── 5 │ │ └── 9 │ │ │ └── 593f1bc2dfb5887e0ac5ebede7fbebd619fc507c │ │ ├── 6 │ │ └── 2 │ │ │ └── 627bed90df51db9ce1f2c987c3ddc0380c236bae │ │ ├── 7 │ │ └── e │ │ │ └── 7e2a93dc15928a8ab4dff670d6d413b0a1b7e7b7 │ │ ├── 9 │ │ └── 3 │ │ │ ├── 93690a16fa86474e5c4b44ff74aeca09a1296124 │ │ │ └── 93d3996e8089bcf62c0f63dc8ae952567bec29ae │ │ ├── a │ │ └── b │ │ │ └── aba51be8a326539a5f53c34a93f1cf8c7942ab93 │ │ ├── d │ │ ├── 1 │ │ │ └── d19d0a03bc7d216f97d118eda320032254055dc5 │ │ └── 2 │ │ │ └── d2f5d1978e5f519100a679c4109f89296278badc │ │ ├── e │ │ └── f │ │ │ └── ef752e58032524232e169a04e51519ffbcaabb75 │ │ └── index.pb └── uiDesigner.xml ├── authentication-service ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── oril │ │ ├── AuthApplication.java │ │ ├── controllers │ │ └── AuthController.java │ │ ├── entities │ │ ├── AuthRequest.java │ │ ├── AuthResponse.java │ │ └── UserVO.java │ │ └── services │ │ ├── AuthService.java │ │ └── JwtUtil.java │ └── resources │ ├── application.yml │ └── bootstrap.yml ├── config-service ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── oril │ │ └── ConfigService.java │ └── resources │ ├── application.yml │ └── bootstrap.yml ├── discovery-service ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── oril │ │ └── DiscoveryService.java │ └── resources │ └── application.yml ├── gateway-service ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── oril │ │ ├── GatewayApplication.java │ │ ├── configs │ │ ├── AuthenticationFilter.java │ │ ├── GatewayConfig.java │ │ └── RouterValidator.java │ │ └── services │ │ └── JwtUtils.java │ └── resources │ ├── application.yml │ └── bootstrap.yml ├── pom.xml ├── src └── main │ └── java │ └── org │ └── oril │ └── Main.java └── user-service ├── .gitignore ├── pom.xml └── src └── main ├── java └── org │ └── oril │ ├── UserApplication.java │ ├── controllers │ └── UserController.java │ ├── entities │ └── UserVO.java │ └── services │ └── UserService.java └── resources ├── application.yml └── bootstrap.yml /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea 8 | .idea/modules.xml 9 | .idea/jarRepositories.xml 10 | .idea/compiler.xml 11 | .idea/libraries/ 12 | *.iws 13 | *.iml 14 | *.ipr 15 | 16 | ### Eclipse ### 17 | .apt_generated 18 | .classpath 19 | .factorypath 20 | .project 21 | .settings 22 | .springBeans 23 | .sts4-cache 24 | 25 | ### NetBeans ### 26 | /nbproject/private/ 27 | /nbbuild/ 28 | /dist/ 29 | /nbdist/ 30 | /.nb-gradle/ 31 | build/ 32 | !**/src/main/**/build/ 33 | !**/src/test/**/build/ 34 | 35 | ### VS Code ### 36 | .vscode/ 37 | 38 | ### Mac OS ### 39 | .DS_Store 40 | .mvn -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 10 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/0/b/0be7e288c9bdcd53666e01bfe770ceb076d022a7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/0/b/0be7e288c9bdcd53666e01bfe770ceb076d022a7 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/1/2/12e2c70c44c3e10b4249c3dcdd4e72ddb0849771: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/1/2/12e2c70c44c3e10b4249c3dcdd4e72ddb0849771 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/3/7/377cec15f41d1e6244fa2ec4687ec13b72f9db23: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/3/7/377cec15f41d1e6244fa2ec4687ec13b72f9db23 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/5/9/593f1bc2dfb5887e0ac5ebede7fbebd619fc507c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/5/9/593f1bc2dfb5887e0ac5ebede7fbebd619fc507c -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/6/2/627bed90df51db9ce1f2c987c3ddc0380c236bae: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/6/2/627bed90df51db9ce1f2c987c3ddc0380c236bae -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/7/e/7e2a93dc15928a8ab4dff670d6d413b0a1b7e7b7: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/7/e/7e2a93dc15928a8ab4dff670d6d413b0a1b7e7b7 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/9/3/93690a16fa86474e5c4b44ff74aeca09a1296124: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/9/3/93690a16fa86474e5c4b44ff74aeca09a1296124 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/9/3/93d3996e8089bcf62c0f63dc8ae952567bec29ae: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/9/3/93d3996e8089bcf62c0f63dc8ae952567bec29ae -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/a/b/aba51be8a326539a5f53c34a93f1cf8c7942ab93: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/a/b/aba51be8a326539a5f53c34a93f1cf8c7942ab93 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/d/1/d19d0a03bc7d216f97d118eda320032254055dc5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/d/1/d19d0a03bc7d216f97d118eda320032254055dc5 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/d/2/d2f5d1978e5f519100a679c4109f89296278badc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/d/2/d2f5d1978e5f519100a679c4109f89296278badc -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/e/f/ef752e58032524232e169a04e51519ffbcaabb75: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/oril-software/spring-cloud-api-gateway-jwt/3cf5eba5954f22f2e4633e46377d56e7ee46d655/.idea/sonarlint/issuestore/e/f/ef752e58032524232e169a04e51519ffbcaabb75 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/index.pb: -------------------------------------------------------------------------------- 1 | 2 | P 3 | src/main/java/org/oril/Main.java,3/7/377cec15f41d1e6244fa2ec4687ec13b72f9db23 4 | L 5 | discovery-service/.gitignore,0/b/0be7e288c9bdcd53666e01bfe770ceb076d022a7 6 | F 7 | config-service/pom.xml,9/3/93d3996e8089bcf62c0f63dc8ae952567bec29ae 8 | N 9 | authentication-service/pom.xml,5/9/593f1bc2dfb5887e0ac5ebede7fbebd619fc507c 10 | D 11 | user-service/pom.xml,d/2/d2f5d1978e5f519100a679c4109f89296278badc 12 | n 13 | >discovery-service/src/main/java/org/oril/DiscoveryService.java,1/2/12e2c70c44c3e10b4249c3dcdd4e72ddb0849771 14 | G 15 | gateway-service/pom.xml,6/2/627bed90df51db9ce1f2c987c3ddc0380c236bae 16 | I 17 | discovery-service/pom.xml,7/e/7e2a93dc15928a8ab4dff670d6d413b0a1b7e7b7 18 | r 19 | Bauthentication-service/src/main/java/org/oril/AuthApplication.java,a/b/aba51be8a326539a5f53c34a93f1cf8c7942ab93 20 | q 21 | Agateway-service/src/main/java/org/oril/configs/GatewayConfig.java,9/3/93690a16fa86474e5c4b44ff74aeca09a1296124 22 | w 23 | Gauthentication-service/src/main/java/org/oril/entities/AuthRequest.java,d/1/d19d0a03bc7d216f97d118eda320032254055dc5 24 | r 25 | Bauthentication-service/src/main/java/org/oril/entities/UserVO.java,e/f/ef752e58032524232e169a04e51519ffbcaabb75 -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /authentication-service/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store -------------------------------------------------------------------------------- /authentication-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oril 8 | jwt-api-gateway 9 | 1.0-SNAPSHOT 10 | 11 | 12 | authentication-service 13 | 14 | 15 | 11 16 | 11 17 | UTF-8 18 | Hoxton.SR9 19 | 0.4 20 | 21 | 22 | 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-web 28 | 29 | 30 | org.projectlombok 31 | lombok 32 | true 33 | 34 | 35 | 36 | org.springframework.cloud 37 | spring-cloud-starter-netflix-eureka-client 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-config-server 43 | 44 | 45 | 46 | 47 | 48 | org.projectlombok 49 | lombok 50 | 51 | 52 | io.jsonwebtoken 53 | jjwt-api 54 | 0.11.1 55 | 56 | 57 | io.jsonwebtoken 58 | jjwt-impl 59 | 0.11.1 60 | runtime 61 | 62 | 63 | io.jsonwebtoken 64 | jjwt-jackson 65 | 0.11.1 66 | runtime 67 | 68 | 69 | 70 | org.mindrot 71 | jbcrypt 72 | ${jbcrypt.version} 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | org.springframework.cloud 81 | spring-cloud-dependencies 82 | ${spring-cloud.version} 83 | pom 84 | import 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | org.springframework.boot 93 | spring-boot-maven-plugin 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package org.oril; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.client.loadbalancer.LoadBalanced; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.web.client.RestTemplate; 8 | 9 | @SpringBootApplication 10 | public class AuthApplication { 11 | public static void main(String[] args) { 12 | SpringApplication.run(AuthApplication.class); 13 | } 14 | 15 | @Bean 16 | @LoadBalanced 17 | public RestTemplate restTemplate() { 18 | return new RestTemplate(); 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/controllers/AuthController.java: -------------------------------------------------------------------------------- 1 | package org.oril.controllers; 2 | 3 | import lombok.AllArgsConstructor; 4 | import org.oril.entities.AuthRequest; 5 | import org.oril.entities.AuthResponse; 6 | import org.oril.services.AuthService; 7 | import org.springframework.http.ResponseEntity; 8 | import org.springframework.web.bind.annotation.PostMapping; 9 | import org.springframework.web.bind.annotation.RequestBody; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | @RestController 14 | @RequestMapping("/auth") 15 | @AllArgsConstructor 16 | public class AuthController { 17 | 18 | private final AuthService authService; 19 | 20 | @PostMapping(value = "/register") 21 | public ResponseEntity register(@RequestBody AuthRequest request) { 22 | return ResponseEntity.ok(authService.register(request)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/entities/AuthRequest.java: -------------------------------------------------------------------------------- 1 | package org.oril.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class AuthRequest { 9 | 10 | private String email; 11 | private String password; 12 | private String name; 13 | } 14 | -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/entities/AuthResponse.java: -------------------------------------------------------------------------------- 1 | package org.oril.entities; 2 | 3 | import lombok.Data; 4 | 5 | @Data 6 | public class AuthResponse { 7 | 8 | private String accessToken; 9 | private String refreshToken; 10 | public AuthResponse(String accessToken, String refreshToken) { 11 | this.accessToken = accessToken; 12 | this.refreshToken = refreshToken; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/entities/UserVO.java: -------------------------------------------------------------------------------- 1 | package org.oril.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class UserVO { 11 | 12 | private String id; 13 | private String email; 14 | private String password; 15 | private String role; 16 | } 17 | -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/services/AuthService.java: -------------------------------------------------------------------------------- 1 | package org.oril.services; 2 | 3 | import lombok.AllArgsConstructor; 4 | import org.mindrot.jbcrypt.BCrypt; 5 | import org.oril.entities.AuthRequest; 6 | import org.oril.entities.AuthResponse; 7 | import org.oril.entities.UserVO; 8 | import org.springframework.stereotype.Service; 9 | import org.springframework.web.client.RestTemplate; 10 | 11 | @Service 12 | @AllArgsConstructor 13 | public class AuthService { 14 | 15 | private final RestTemplate restTemplate; 16 | private final JwtUtil jwtUtil; 17 | 18 | public AuthResponse register(AuthRequest request) { 19 | //do validation if user exists in DB 20 | request.setPassword(BCrypt.hashpw(request.getPassword(), BCrypt.gensalt())); 21 | UserVO registeredUser = restTemplate.postForObject("http://user-service/users", request, UserVO.class); 22 | 23 | String accessToken = jwtUtil.generate(registeredUser.getId(), registeredUser.getRole(), "ACCESS"); 24 | String refreshToken = jwtUtil.generate(registeredUser.getId(), registeredUser.getRole(), "REFRESH"); 25 | 26 | return new AuthResponse(accessToken, refreshToken); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /authentication-service/src/main/java/org/oril/services/JwtUtil.java: -------------------------------------------------------------------------------- 1 | package org.oril.services; 2 | 3 | import io.jsonwebtoken.Claims; 4 | import io.jsonwebtoken.Jwts; 5 | import io.jsonwebtoken.security.Keys; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.stereotype.Service; 8 | 9 | import javax.annotation.PostConstruct; 10 | import java.security.Key; 11 | import java.util.Date; 12 | import java.util.Map; 13 | 14 | @Service 15 | public class JwtUtil { 16 | 17 | @Value("${jwt.secret}") 18 | private String secret; 19 | @Value("${jwt.expiration}") 20 | private String expiration; 21 | 22 | private Key key; 23 | 24 | @PostConstruct 25 | public void initKey() { 26 | this.key = Keys.hmacShaKeyFor(secret.getBytes()); 27 | } 28 | 29 | public Claims getClaims(String token) { 30 | return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJwt(token).getBody(); 31 | } 32 | 33 | public Date getExpirationDate(String token) { 34 | return getClaims(token).getExpiration(); 35 | } 36 | 37 | public String generate(String userId, String role, String tokenType) { 38 | Map claims = Map.of("id", userId, "role", role); 39 | long expMillis = "ACCESS".equalsIgnoreCase(tokenType) 40 | ? Long.parseLong(expiration) * 1000 41 | : Long.parseLong(expiration) * 1000 * 5; 42 | 43 | final Date now = new Date(); 44 | final Date exp = new Date(now.getTime() + expMillis); 45 | 46 | return Jwts.builder() 47 | .setClaims(claims) 48 | .setSubject(claims.get("id")) 49 | .setIssuedAt(now) 50 | .setExpiration(exp) 51 | .signWith(key) 52 | .compact(); 53 | } 54 | 55 | private boolean isExpired(String token) { 56 | return getExpirationDate(token).before(new Date()); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /authentication-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9001 3 | 4 | spring: 5 | application: 6 | name: auth-service 7 | 8 | jwt: 9 | secret: MiAVzqUXy5Tfr1kVIGpPMiAVzqUXy5Tfr1kVIGpP 10 | expiration: 86400 -------------------------------------------------------------------------------- /authentication-service/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | cloud: 3 | config: 4 | enabled: true 5 | uri: http://localhost:9296 -------------------------------------------------------------------------------- /config-service/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store -------------------------------------------------------------------------------- /config-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oril 8 | jwt-api-gateway 9 | 1.0-SNAPSHOT 10 | 11 | 12 | config-service 13 | 14 | 15 | 11 16 | 11 17 | UTF-8 18 | Hoxton.SR9 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-config-server 26 | 27 | 28 | 29 | org.springframework.cloud 30 | spring-cloud-starter-netflix-eureka-client 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.springframework.cloud 39 | spring-cloud-dependencies 40 | ${spring-cloud.version} 41 | pom 42 | import 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /config-service/src/main/java/org/oril/ConfigService.java: -------------------------------------------------------------------------------- 1 | package org.oril; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.config.server.EnableConfigServer; 6 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 | 8 | @SpringBootApplication 9 | @EnableEurekaClient 10 | @EnableConfigServer 11 | public class ConfigService { 12 | public static void main(String[] args) { 13 | SpringApplication.run(ConfigService.class); 14 | } 15 | } -------------------------------------------------------------------------------- /config-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9297 3 | 4 | spring: 5 | application: 6 | name: config-service 7 | cloud: 8 | config: 9 | server: 10 | git: 11 | uri: https://github.com/igorkosandyak/demo-config-server.git 12 | clone-on-start: true -------------------------------------------------------------------------------- /config-service/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | cloud: 3 | config: 4 | enabled: true 5 | uri: http://localhost:9296 -------------------------------------------------------------------------------- /discovery-service/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store -------------------------------------------------------------------------------- /discovery-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oril 8 | jwt-api-gateway 9 | 1.0-SNAPSHOT 10 | 11 | 12 | discovery-service 13 | 14 | 15 | 11 16 | 11 17 | UTF-8 18 | Hoxton.SR9 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-netflix-eureka-server 27 | 28 | 29 | 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-dependencies 35 | ${spring-cloud.version} 36 | pom 37 | import 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /discovery-service/src/main/java/org/oril/DiscoveryService.java: -------------------------------------------------------------------------------- 1 | package org.oril; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaServer 9 | public class DiscoveryService { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(DiscoveryService.class); 13 | } 14 | } -------------------------------------------------------------------------------- /discovery-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8761 3 | 4 | eureka: 5 | client: 6 | register-with-eureka: false 7 | fetch-repository: false -------------------------------------------------------------------------------- /gateway-service/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store -------------------------------------------------------------------------------- /gateway-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oril 8 | jwt-api-gateway 9 | 1.0-SNAPSHOT 10 | 11 | 12 | gateway-service 13 | 14 | 15 | 11 16 | 11 17 | UTF-8 18 | Hoxton.SR9 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-gateway 26 | 27 | 28 | org.springframework.cloud 29 | spring-cloud-starter-netflix-hystrix 30 | 31 | 32 | 33 | org.springframework.cloud 34 | spring-cloud-starter-netflix-eureka-client 35 | 36 | 37 | 38 | 39 | 40 | io.jsonwebtoken 41 | jjwt-api 42 | 0.11.1 43 | 44 | 45 | io.jsonwebtoken 46 | jjwt-impl 47 | 0.11.1 48 | runtime 49 | 50 | 51 | io.jsonwebtoken 52 | jjwt-jackson 53 | 0.11.1 54 | runtime 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.springframework.cloud 63 | spring-cloud-dependencies 64 | ${spring-cloud.version} 65 | pom 66 | import 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | org.springframework.boot 75 | spring-boot-maven-plugin 76 | 77 | 78 | 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /gateway-service/src/main/java/org/oril/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | package org.oril; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 | 7 | @SpringBootApplication 8 | @EnableEurekaClient 9 | public class GatewayApplication { 10 | public static void main(String[] args) { 11 | SpringApplication.run(GatewayApplication.class); 12 | } 13 | } -------------------------------------------------------------------------------- /gateway-service/src/main/java/org/oril/configs/AuthenticationFilter.java: -------------------------------------------------------------------------------- 1 | package org.oril.configs; 2 | 3 | import org.oril.services.JwtUtils; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.cloud.context.config.annotation.RefreshScope; 6 | import org.springframework.cloud.gateway.filter.GatewayFilter; 7 | import org.springframework.cloud.gateway.filter.GatewayFilterChain; 8 | import org.springframework.http.HttpStatus; 9 | import org.springframework.http.server.reactive.ServerHttpRequest; 10 | import org.springframework.http.server.reactive.ServerHttpResponse; 11 | import org.springframework.stereotype.Component; 12 | import org.springframework.web.server.ServerWebExchange; 13 | import reactor.core.publisher.Mono; 14 | 15 | @RefreshScope 16 | @Component 17 | public class AuthenticationFilter implements GatewayFilter { 18 | 19 | @Autowired 20 | private RouterValidator validator; 21 | @Autowired 22 | private JwtUtils jwtUtils; 23 | 24 | @Override 25 | public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { 26 | ServerHttpRequest request = exchange.getRequest(); 27 | 28 | if (validator.isSecured.test(request)) { 29 | if (authMissing(request)) { 30 | return onError(exchange, HttpStatus.UNAUTHORIZED); 31 | } 32 | 33 | final String token = request.getHeaders().getOrEmpty("Authorization").get(0); 34 | 35 | if (jwtUtils.isExpired(token)) { 36 | return onError(exchange, HttpStatus.UNAUTHORIZED); 37 | } 38 | } 39 | return chain.filter(exchange); 40 | } 41 | 42 | private Mono onError(ServerWebExchange exchange, HttpStatus httpStatus) { 43 | ServerHttpResponse response = exchange.getResponse(); 44 | response.setStatusCode(httpStatus); 45 | return response.setComplete(); 46 | } 47 | 48 | private boolean authMissing(ServerHttpRequest request) { 49 | return !request.getHeaders().containsKey("Authorization"); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /gateway-service/src/main/java/org/oril/configs/GatewayConfig.java: -------------------------------------------------------------------------------- 1 | package org.oril.configs; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.cloud.gateway.route.RouteLocator; 5 | import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; 6 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | 10 | @Configuration 11 | @EnableHystrix 12 | public class GatewayConfig { 13 | @Autowired 14 | private AuthenticationFilter filter; 15 | 16 | @Bean 17 | public RouteLocator routes(RouteLocatorBuilder builder) { 18 | return builder.routes() 19 | .route("user-service", r -> r.path("/users/**") 20 | .filters(f -> f.filter(filter)) 21 | .uri("lb://user-service")) 22 | .route("auth-service", r -> r.path("/auth/**") 23 | .filters(f -> f.filter(filter)) 24 | .uri("lb://auth-service")) 25 | .build(); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /gateway-service/src/main/java/org/oril/configs/RouterValidator.java: -------------------------------------------------------------------------------- 1 | package org.oril.configs; 2 | 3 | import org.springframework.http.server.reactive.ServerHttpRequest; 4 | import org.springframework.stereotype.Service; 5 | 6 | import java.util.List; 7 | import java.util.function.Predicate; 8 | 9 | @Service 10 | public class RouterValidator { 11 | 12 | public static final List openEndpoints = List.of( 13 | "/auth/register" 14 | ); 15 | 16 | public Predicate isSecured = 17 | request -> openEndpoints.stream() 18 | .noneMatch(uri -> request.getURI().getPath().contains(uri)); 19 | } 20 | -------------------------------------------------------------------------------- /gateway-service/src/main/java/org/oril/services/JwtUtils.java: -------------------------------------------------------------------------------- 1 | package org.oril.services; 2 | 3 | import io.jsonwebtoken.Claims; 4 | import io.jsonwebtoken.Jwts; 5 | import io.jsonwebtoken.security.Keys; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.stereotype.Service; 9 | 10 | import javax.annotation.PostConstruct; 11 | import java.security.Key; 12 | import java.util.Date; 13 | 14 | @Service 15 | public class JwtUtils { 16 | @Value("${jwt.secret}") 17 | private String secret; 18 | 19 | private Key key; 20 | 21 | @PostConstruct 22 | public void initKey() { 23 | this.key = Keys.hmacShaKeyFor(secret.getBytes()); 24 | } 25 | 26 | public Claims getClaims(String token) { 27 | return Jwts.parserBuilder().setSigningKey(key).build().parseClaimsJws(token).getBody(); 28 | } 29 | 30 | public boolean isExpired(String token) { 31 | try { 32 | return getClaims(token).getExpiration().before(new Date()); 33 | } catch (Exception e) { 34 | return false; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /gateway-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 8080 3 | 4 | spring: 5 | application: 6 | name: api-gateway 7 | cloud: 8 | gateway: 9 | discovery: 10 | locator: 11 | enabled: true 12 | 13 | jwt: 14 | secret: MiAVzqUXy5Tfr1kVIGpPMiAVzqUXy5Tfr1kVIGpP -------------------------------------------------------------------------------- /gateway-service/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | cloud: 3 | config: 4 | enabled: true 5 | uri: http://localhost:9296 -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oril 8 | jwt-api-gateway 9 | 1.0-SNAPSHOT 10 | pom 11 | 12 | discovery-service 13 | config-service 14 | authentication-service 15 | user-service 16 | gateway-service 17 | 18 | 19 | 20 | 11 21 | 11 22 | UTF-8 23 | 24 | 25 | 26 | org.springframework.boot 27 | spring-boot-starter-parent 28 | 2.3.8.RELEASE 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/org/oril/Main.java: -------------------------------------------------------------------------------- 1 | package org.oril; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | System.out.println("Hello world!"); 6 | } 7 | } -------------------------------------------------------------------------------- /user-service/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | !**/src/main/**/target/ 4 | !**/src/test/**/target/ 5 | 6 | ### IntelliJ IDEA ### 7 | .idea/modules.xml 8 | .idea/jarRepositories.xml 9 | .idea/compiler.xml 10 | .idea/libraries/ 11 | *.iws 12 | *.iml 13 | *.ipr 14 | 15 | ### Eclipse ### 16 | .apt_generated 17 | .classpath 18 | .factorypath 19 | .project 20 | .settings 21 | .springBeans 22 | .sts4-cache 23 | 24 | ### NetBeans ### 25 | /nbproject/private/ 26 | /nbbuild/ 27 | /dist/ 28 | /nbdist/ 29 | /.nb-gradle/ 30 | build/ 31 | !**/src/main/**/build/ 32 | !**/src/test/**/build/ 33 | 34 | ### VS Code ### 35 | .vscode/ 36 | 37 | ### Mac OS ### 38 | .DS_Store -------------------------------------------------------------------------------- /user-service/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oril 8 | jwt-api-gateway 9 | 1.0-SNAPSHOT 10 | 11 | 12 | user-service 13 | 14 | 15 | 11 16 | 11 17 | UTF-8 18 | Hoxton.SR9 19 | 0.4 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | org.projectlombok 31 | lombok 32 | true 33 | 34 | 35 | 36 | org.springframework.cloud 37 | spring-cloud-starter-netflix-eureka-client 38 | 39 | 40 | 41 | org.springframework.cloud 42 | spring-cloud-config-server 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.springframework.cloud 51 | spring-cloud-dependencies 52 | ${spring-cloud.version} 53 | pom 54 | import 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.springframework.boot 63 | spring-boot-maven-plugin 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /user-service/src/main/java/org/oril/UserApplication.java: -------------------------------------------------------------------------------- 1 | package org.oril; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class UserApplication { 8 | public static void main(String[] args) { 9 | SpringApplication.run(UserApplication.class); 10 | } 11 | } -------------------------------------------------------------------------------- /user-service/src/main/java/org/oril/controllers/UserController.java: -------------------------------------------------------------------------------- 1 | package org.oril.controllers; 2 | 3 | import lombok.AllArgsConstructor; 4 | import org.oril.entities.UserVO; 5 | import org.oril.services.UserService; 6 | import org.springframework.http.ResponseEntity; 7 | import org.springframework.web.bind.annotation.*; 8 | 9 | @RestController 10 | @RequestMapping(value = "/users") 11 | @AllArgsConstructor 12 | public class UserController { 13 | 14 | private final UserService userService; 15 | 16 | @PostMapping 17 | public ResponseEntity save(@RequestBody UserVO userVO) { 18 | return ResponseEntity.ok(userService.save(userVO)); 19 | } 20 | 21 | @GetMapping("/secured") 22 | public ResponseEntity securedEndpoint() { 23 | return ResponseEntity.ok("Hello, from secured endpoint!"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /user-service/src/main/java/org/oril/entities/UserVO.java: -------------------------------------------------------------------------------- 1 | package org.oril.entities; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | 6 | @Data 7 | @AllArgsConstructor 8 | public class UserVO { 9 | 10 | private String id; 11 | private String email; 12 | private String password; 13 | private String role; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /user-service/src/main/java/org/oril/services/UserService.java: -------------------------------------------------------------------------------- 1 | package org.oril.services; 2 | 3 | import lombok.AllArgsConstructor; 4 | import org.oril.entities.UserVO; 5 | import org.springframework.stereotype.Service; 6 | 7 | import java.util.Date; 8 | 9 | @Service 10 | @AllArgsConstructor 11 | public class UserService { 12 | 13 | public UserVO save(UserVO userVO) { 14 | //simulate save operation; 15 | String userId = String.valueOf(new Date().getTime()); 16 | userVO.setId(userId); 17 | userVO.setRole("USER"); 18 | //save user 19 | return userVO; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /user-service/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9002 3 | 4 | spring: 5 | application: 6 | name: user-service 7 | 8 | -------------------------------------------------------------------------------- /user-service/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | cloud: 3 | config: 4 | enabled: true 5 | uri: http://localhost:9296 --------------------------------------------------------------------------------