From bb2dba636b727f9623c631ba8aa491cb9b0b99cc Mon Sep 17 00:00:00 2001 From: DusanStevic <40674641+DusanStevic@users.noreply.github.com> Date: Wed, 15 Sep 2021 19:35:01 +0200 Subject: [PATCH] feign admin service (#24) --- pom.xml | 75 ++++++++++++------- .../AdministratorApplication.java | 2 - .../administrator/client/PostClient.java | 5 +- .../administrator/client/UserClient.java | 11 +-- .../config/WebSecurityConfig.java | 25 ++++++- src/main/resources/application-dev.properties | 17 +++++ .../resources/application-prod.properties | 16 ++++ .../resources/application-test.properties | 16 ++++ src/main/resources/application.properties | 1 + src/main/resources/application.yml | 42 ----------- 10 files changed, 132 insertions(+), 78 deletions(-) create mode 100644 src/main/resources/application-dev.properties create mode 100644 src/main/resources/application-prod.properties create mode 100644 src/main/resources/application-test.properties create mode 100644 src/main/resources/application.properties delete mode 100644 src/main/resources/application.yml diff --git a/pom.xml b/pom.xml index 6aba4c3..27fa520 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,6 @@ admin_service devops-nistagram-organization https://sonarcloud.io - 2020.0.3 @@ -43,24 +42,10 @@ org.springframework.boot spring-boot-starter-web - - org.springframework.cloud - spring-cloud-starter-netflix-eureka-client - org.springframework.boot spring-boot-starter-actuator - - mysql - mysql-connector-java - runtime - - - org.postgresql - postgresql - runtime - org.springframework.boot spring-boot-starter-test @@ -72,17 +57,55 @@ test - - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - - + + + + dev + + true + + + dev + dev + + + + mysql + mysql-connector-java + runtime + + + + + test + + test + test + + + + org.postgresql + postgresql + runtime + + + + + prod + + prod + prod + + + + org.postgresql + postgresql + runtime + + + + + diff --git a/src/main/java/com/nistagram/administrator/AdministratorApplication.java b/src/main/java/com/nistagram/administrator/AdministratorApplication.java index a19bc4a..3f2fe15 100644 --- a/src/main/java/com/nistagram/administrator/AdministratorApplication.java +++ b/src/main/java/com/nistagram/administrator/AdministratorApplication.java @@ -2,12 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients -@EnableEurekaClient public class AdministratorApplication { public static void main(String[] args) { diff --git a/src/main/java/com/nistagram/administrator/client/PostClient.java b/src/main/java/com/nistagram/administrator/client/PostClient.java index 7123cd6..cb6cbeb 100644 --- a/src/main/java/com/nistagram/administrator/client/PostClient.java +++ b/src/main/java/com/nistagram/administrator/client/PostClient.java @@ -5,8 +5,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -@FeignClient(url = "${api.postService}", name = "PostService") +// app.post.url from application.properties +@FeignClient(name = "post", url = "${app.post.url}") public interface PostClient { - @DeleteMapping(value = "delete/{id}") + @DeleteMapping(value = "post/delete/{id}") ResponseEntity deletePost(@PathVariable("id") Long id, @RequestHeader("Authorization") String bearerToken); } diff --git a/src/main/java/com/nistagram/administrator/client/UserClient.java b/src/main/java/com/nistagram/administrator/client/UserClient.java index ecf9fdf..742de96 100644 --- a/src/main/java/com/nistagram/administrator/client/UserClient.java +++ b/src/main/java/com/nistagram/administrator/client/UserClient.java @@ -9,17 +9,18 @@ import java.util.List; -@FeignClient(url = "${api.userService}", name = "UserService") +// app.user.url from application.properties +@FeignClient(name = "user", url = "${app.user.url}") public interface UserClient { - @GetMapping(value = "getUser/{username}") + @GetMapping(value = "user/getUser/{username}") UserInfoDTO getUser(@PathVariable("username") String username); - @GetMapping(value = "getNotApprovedAgents") + @GetMapping(value = "user/getNotApprovedAgents") ResponseEntity> getNotApprovedAgents(@RequestHeader("Authorization") String bearerToken); - @PostMapping(value = "approveAgent") + @PostMapping(value = "user/approveAgent") ResponseEntity approveAgent(@RequestBody() UsernameWrapper dto, @RequestHeader("Authorization") String bearerToken); - @PostMapping(value = "rejectAgent") + @PostMapping(value = "user/rejectAgent") ResponseEntity rejectAgent(@RequestBody() UsernameWrapper dto, @RequestHeader("Authorization") String bearerToken); } diff --git a/src/main/java/com/nistagram/administrator/config/WebSecurityConfig.java b/src/main/java/com/nistagram/administrator/config/WebSecurityConfig.java index b843890..9991a11 100644 --- a/src/main/java/com/nistagram/administrator/config/WebSecurityConfig.java +++ b/src/main/java/com/nistagram/administrator/config/WebSecurityConfig.java @@ -14,6 +14,11 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; +import org.springframework.web.filter.CorsFilter; +import java.util.Arrays; +import org.springframework.http.HttpMethod; @Configuration @@ -43,6 +48,23 @@ public PasswordEncoder passwordEncoder() { @Autowired private JwtUserDetailsService jwtUserDetailsService; + @Bean + public CorsFilter corsFilter() { + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + CorsConfiguration config = new CorsConfiguration(); + config.setAllowCredentials(false); + config.addAllowedOrigin("*"); + config.addAllowedHeader("*"); + config.setExposedHeaders(Arrays.asList("Access-Control-Allow-Origin", "Location", "X-Requested-With", "Authorization", "Cache-Control", "Content-Type", "X-Total-Count", "allowedOriginPatterns")); + config.addAllowedMethod(HttpMethod.OPTIONS.name()); + config.addAllowedMethod(HttpMethod.GET.name()); + config.addAllowedMethod(HttpMethod.PUT.name()); + config.addAllowedMethod(HttpMethod.POST.name()); + config.addAllowedMethod(HttpMethod.DELETE.name()); + source.registerCorsConfiguration("/**", config); + return new org.springframework.web.filter.CorsFilter(source); + } + // Defining access rights to specific URLs @Override @@ -65,7 +87,8 @@ protected void configure(HttpSecurity http) throws Exception { // intercept every request and add filter http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class); - http.csrf().disable(); + //http.csrf().disable(); + http.cors().and().csrf().disable(); /* http.headers().addHeaderWriter( new StaticHeadersWriter("Access-Control-Allow-Origin", "*"));*/ diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties new file mode 100644 index 0000000..228e647 --- /dev/null +++ b/src/main/resources/application-dev.properties @@ -0,0 +1,17 @@ +# DATABASE +spring.datasource.username=${DATABASE_USERNAME:root} +spring.datasource.password=${DATABASE_PASSWORD:password} +spring.datasource.url=jdbc:mysql://${DATABASE_DOMAIN:localhost}:${DATABASE_PORT:3306}/${DATABASE_SCHEMA:admin_db} +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.sql.init.continue-on-error=true + +server.port=8080 +spring.application.name=admin + +app.user.url = ${USER_SERVICE_PROTOCOL:http}://${USER_SERVICE_DOMAIN:localhost}:${USER_SERVICE_PORT:8080} +app.post.url = ${POST_SERVICE_PROTOCOL:http}://${POST_SERVICE_DOMAIN:localhost}:${POST_SERVICE_PORT:8080} + +jwt.secret=nistagram \ No newline at end of file diff --git a/src/main/resources/application-prod.properties b/src/main/resources/application-prod.properties new file mode 100644 index 0000000..273a487 --- /dev/null +++ b/src/main/resources/application-prod.properties @@ -0,0 +1,16 @@ +# DATABASE +spring.datasource.username=${DATABASE_USERNAME:root} +spring.datasource.password=${DATABASE_PASSWORD:password} +spring.datasource.url=jdbc:postgresql://${DATABASE_DOMAIN:localhost}:${DATABASE_PORT:5432}/${DATABASE_SCHEMA:admin_db} +spring.datasource.platform=postgres +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true + +server.port=8080 +spring.application.name=admin + +app.user.url = ${USER_SERVICE_PROTOCOL:http}://${USER_SERVICE_DOMAIN:localhost}:${USER_SERVICE_PORT:8080} +app.post.url = ${POST_SERVICE_PROTOCOL:http}://${POST_SERVICE_DOMAIN:localhost}:${POST_SERVICE_PORT:8080} + +jwt.secret=${JWT_SECRET:nistagram} \ No newline at end of file diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties new file mode 100644 index 0000000..273a487 --- /dev/null +++ b/src/main/resources/application-test.properties @@ -0,0 +1,16 @@ +# DATABASE +spring.datasource.username=${DATABASE_USERNAME:root} +spring.datasource.password=${DATABASE_PASSWORD:password} +spring.datasource.url=jdbc:postgresql://${DATABASE_DOMAIN:localhost}:${DATABASE_PORT:5432}/${DATABASE_SCHEMA:admin_db} +spring.datasource.platform=postgres +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true + +server.port=8080 +spring.application.name=admin + +app.user.url = ${USER_SERVICE_PROTOCOL:http}://${USER_SERVICE_DOMAIN:localhost}:${USER_SERVICE_PORT:8080} +app.post.url = ${POST_SERVICE_PROTOCOL:http}://${POST_SERVICE_DOMAIN:localhost}:${POST_SERVICE_PORT:8080} + +jwt.secret=${JWT_SECRET:nistagram} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..cbf6363 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=@profileActive@ \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml deleted file mode 100644 index 76812ae..0000000 --- a/src/main/resources/application.yml +++ /dev/null @@ -1,42 +0,0 @@ -server: - port: 0 - -spring: - application: - name: admin-service - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/nistagramAdmin?useSSL=false&createDatabaseIfNotExist=true&serverTimezone=Europe/Belgrade - username: root - password: root - dbcp2: - test-while-idle: true - validation-query: SELECT 1 - continue-on-error: true - jpa: - hibernate: - ddl-auto: update - show-sql: true -jwt: - secret: Agent - -eureka: - client: - service-url: - defaultZone: http://localhost:8762/eureka - register-with-eureka: true - fetch-registry: true - healthcheck: - enabled: true - instance: - hostname: localhost - instance-id: ${spring.application.name}:${random.int} - lease-renewal-interval-in-seconds: 1 - lease-expiration-duration-in-seconds: 2 - nonSecurePortEnabled: true - securePortEnabled: false - securePort: ${server.port} - -api: - userService: http://localhost:8081/user-service/user - postService: http://localhost:8081/post-service/post