From 19c8ed8a1d173ee16c5717d44b145ca365d2f6f5 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 6 Feb 2018 14:36:37 +0300 Subject: api server should authenticate in database for now --- .../api/configuration/ApiAppConfiguration.java | 22 +++++++ .../juick/api/configuration/ApiInitializer.java | 5 +- .../NotificationsAppConfiguration.java | 12 +--- .../JuickServerComponentConfiguration.java | 73 ---------------------- .../JuickServerWebsocketConfiguration.java | 72 +++++++++++++++++++++ 5 files changed, 98 insertions(+), 86 deletions(-) delete mode 100644 juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java create mode 100644 juick-server-web/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java index 388fda67..a475ca6f 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -21,10 +21,15 @@ import com.juick.api.ApiServer; import com.juick.server.component.JuickServerComponent; import com.juick.server.component.JuickServerReconnectManager; import com.juick.server.configuration.BaseWebConfiguration; +import com.juick.service.UserService; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.*; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.socket.client.WebSocketConnectionManager; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.util.UriComponentsBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; @@ -32,6 +37,7 @@ import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; +import javax.inject.Inject; import java.util.Collections; /** @@ -44,6 +50,12 @@ import java.util.Collections; @PropertySource("classpath:juick.conf") @ComponentScan(basePackages = "com.juick") public class ApiAppConfiguration extends BaseWebConfiguration { + @Inject + UserService userService; + @Value("${api_user:juick}") + private String serviceUser; + @Value("${websocket_url:ws://localhost:8080/}") + private String baseUri; @Lazy @Bean public JuickServerComponent juickServerComponent() { @@ -55,6 +67,16 @@ public class ApiAppConfiguration extends BaseWebConfiguration { return new JuickServerReconnectManager(); } @Bean + public WebSocketConnectionManager connectionManager() { + String websocketURI = UriComponentsBuilder.fromUriString(baseUri) + .queryParam("hash", userService.getHashByUID(userService.getUIDbyName(serviceUser))).build().toUriString(); + return new WebSocketConnectionManager(client(), juickServerComponent(), websocketURI); + } + @Bean + public StandardWebSocketClient client() { + return new StandardWebSocketClient(); + } + @Bean public ApiServer apiServer() { return new ApiServer(); } diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java index edda931b..8f35e3f7 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -18,7 +18,7 @@ package com.juick.api.configuration; import com.juick.configuration.DataConfiguration; -import com.juick.server.configuration.JuickServerComponentConfiguration; +import com.juick.server.configuration.JuickServerWebsocketConfiguration; import com.juick.server.configuration.StorageConfiguration; import org.apache.commons.codec.CharEncoding; import org.springframework.web.filter.CharacterEncodingFilter; @@ -37,8 +37,7 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni return new Class[]{ ApiSecurityConfig.class, DataConfiguration.class, - StorageConfiguration.class, - JuickServerComponentConfiguration.class + StorageConfiguration.class }; } diff --git a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java index 67293373..b557cfc7 100644 --- a/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java +++ b/juick-notifications/src/main/java/com/juick/components/configuration/NotificationsAppConfiguration.java @@ -5,19 +5,11 @@ import com.juick.components.Notifications; import com.juick.server.component.JuickServerComponent; import com.juick.server.component.JuickServerReconnectManager; import com.juick.server.configuration.BaseWebConfiguration; -import com.juick.server.configuration.JuickServerComponentConfiguration; -import org.springframework.beans.factory.annotation.Value; +import com.juick.server.configuration.JuickServerWebsocketConfiguration; import org.springframework.context.annotation.*; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.InterceptingClientHttpRequestFactory; -import org.springframework.http.client.support.BasicAuthorizationInterceptor; import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import java.util.Collections; -import java.util.List; - /** * Created by aalexeev on 11/12/16. */ @@ -27,7 +19,7 @@ import java.util.List; @PropertySource("classpath:juick.conf") @ComponentScan(basePackages = "com.juick.components.service") @Import({ APNSConfiguration.class, MPNSConfiguration.class, - GCMConfiguration.class, JuickServerComponentConfiguration.class}) + GCMConfiguration.class, JuickServerWebsocketConfiguration.class}) public class NotificationsAppConfiguration extends BaseWebConfiguration { @Bean diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java deleted file mode 100644 index a2df26fc..00000000 --- a/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.juick.server.configuration; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.juick.server.component.JuickServerComponent; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.InterceptingClientHttpRequestFactory; -import org.springframework.http.client.support.BasicAuthorizationInterceptor; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.socket.client.WebSocketConnectionManager; -import org.springframework.web.socket.client.standard.StandardWebSocketClient; -import org.springframework.web.util.UriComponentsBuilder; - -import javax.inject.Inject; -import java.io.IOException; -import java.util.Collections; -import java.util.List; - -@Lazy -@Configuration -@EnableScheduling -public class JuickServerComponentConfiguration { - private static final Logger logger = LoggerFactory.getLogger(JuickServerComponentConfiguration.class); - @Value("${websocket_url:ws://localhost:8080/}") - private String baseUri; - @Value("${api_user:juick}") - private String serviceUser; - @Value("${api_password:secret}") - private String servicePassword; - @Inject - ObjectMapper jsonMapper; - @Inject - private JuickServerComponent juickServerComponent; - @Bean - public RestTemplate rest() { - RestTemplate rest = new RestTemplate(); - List interceptors = Collections.singletonList( - new BasicAuthorizationInterceptor(serviceUser, servicePassword)); - - rest.setRequestFactory(new InterceptingClientHttpRequestFactory(rest.getRequestFactory(), interceptors)); - return rest; - } - @Bean - public WebSocketConnectionManager connectionManager() { - String hash = StringUtils.EMPTY; - try { - ResponseEntity response = rest().exchange("https://api.juick.com/auth", - HttpMethod.GET, null, String.class); - hash = jsonMapper.readValue(response.getBody(), String.class); - } catch (HttpClientErrorException | IOException e) { - logger.warn("service component is not authenticated", e); - } - String websocketURI = UriComponentsBuilder.fromUriString(baseUri) - .queryParam("hash", hash).build().toUriString(); - WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), juickServerComponent, websocketURI); - manager.setAutoStartup(true); - return manager; - } - @Bean - public StandardWebSocketClient client() { - return new StandardWebSocketClient(); - } -} diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java new file mode 100644 index 00000000..6790a7c9 --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/server/configuration/JuickServerWebsocketConfiguration.java @@ -0,0 +1,72 @@ +package com.juick.server.configuration; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.juick.server.component.JuickServerComponent; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.InterceptingClientHttpRequestFactory; +import org.springframework.http.client.support.BasicAuthorizationInterceptor; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.socket.client.WebSocketConnectionManager; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.Collections; +import java.util.List; + +@Lazy +@Configuration +@EnableScheduling +public class JuickServerWebsocketConfiguration { + private static final Logger logger = LoggerFactory.getLogger(JuickServerWebsocketConfiguration.class); + @Value("${websocket_url:ws://localhost:8080/}") + private String baseUri; + @Value("${api_user:juick}") + private String serviceUser; + @Value("${api_password:secret}") + private String servicePassword; + @Inject + ObjectMapper jsonMapper; + @Inject + private JuickServerComponent juickServerComponent; + @Bean + public RestTemplate rest() { + RestTemplate rest = new RestTemplate(); + List interceptors = Collections.singletonList( + new BasicAuthorizationInterceptor(serviceUser, servicePassword)); + + rest.setRequestFactory(new InterceptingClientHttpRequestFactory(rest.getRequestFactory(), interceptors)); + return rest; + } + @Bean + public WebSocketConnectionManager connectionManager() { + String hash = StringUtils.EMPTY; + try { + ResponseEntity response = rest().exchange("https://api.juick.com/auth", + HttpMethod.GET, null, String.class); + hash = jsonMapper.readValue(response.getBody(), String.class); + } catch (HttpClientErrorException | IOException e) { + logger.warn("service component is not authenticated", e); + } + String websocketURI = UriComponentsBuilder.fromUriString(baseUri) + .queryParam("hash", hash).build().toUriString(); + WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), juickServerComponent, websocketURI); + return manager; + } + @Bean + public StandardWebSocketClient client() { + return new StandardWebSocketClient(); + } +} -- cgit v1.2.3