diff options
author | 2018-01-30 17:51:13 +0300 | |
---|---|---|
committer | 2018-01-30 17:52:10 +0300 | |
commit | c41d997b1046fcc7bf04e8abb317907425bdb714 (patch) | |
tree | 7b294a83b1f8b773f95aeba4f86f701f04f6a5fc /juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java | |
parent | c6f081c30b378b9b0c3210bddb653c87059e75c8 (diff) |
web: authenticate services over api
Diffstat (limited to 'juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java')
-rw-r--r-- | juick-server-web/src/main/java/com/juick/server/configuration/JuickServerComponentConfiguration.java | 32 |
1 files changed, 27 insertions, 5 deletions
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 index 5b0cf178..3e8b3865 100644 --- 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 @@ -1,31 +1,53 @@ package com.juick.server.configuration; +import com.fasterxml.jackson.databind.ObjectMapper; import com.juick.server.component.JuickServerComponent; -import com.juick.service.UserService; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +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.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; @Configuration @EnableScheduling public class JuickServerComponentConfiguration { @Value("${websocket_url:ws://localhost:8080/}") private String baseUri; - @Value("${service_user:juick}") + @Value("${api_user:juick}") private String serviceUser; + @Value("${api_password:secret}") + private String servicePassword; @Inject - private UserService userService; + ObjectMapper jsonMapper; @Inject private JuickServerComponent juickServerComponent; @Bean - public WebSocketConnectionManager connectionManager() { - String hash = userService.getHashByUID(userService.getUIDbyName(serviceUser)); + public RestTemplate rest() { + RestTemplate rest = new RestTemplate(); + List<ClientHttpRequestInterceptor> interceptors = Collections.singletonList( + new BasicAuthorizationInterceptor(serviceUser, servicePassword)); + + rest.setRequestFactory(new InterceptingClientHttpRequestFactory(rest.getRequestFactory(), interceptors)); + return rest; + } + @Bean + public WebSocketConnectionManager connectionManager() throws IOException { + ResponseEntity<String> response = rest().exchange("https://api.juick.com/auth", + HttpMethod.GET, null, String.class); + String hash = jsonMapper.readValue(response.getBody(), String.class); String websocketURI = UriComponentsBuilder.fromUriString(baseUri) .queryParam("hash", hash).build().toUriString(); WebSocketConnectionManager manager = new WebSocketConnectionManager(client(), juickServerComponent, websocketURI); |