Skip to content

Commit

Permalink
Add basic auth in DEW.
Browse files Browse the repository at this point in the history
  • Loading branch information
gudaoxuri committed Jun 8, 2017
1 parent 111d8d0 commit 543ea92
Show file tree
Hide file tree
Showing 19 changed files with 144 additions and 41 deletions.
4 changes: 2 additions & 2 deletions components/auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>auth</artifactId>
<name>Dew Auth</name>
<description>Dew 多租户的RBAC权限服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions components/config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>config</artifactId>
<name>Dew Config</name>
<description>Dew 配置中心服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions components/gateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>gateway</artifactId>
<name>Dew Gateway</name>
<description>Dew 网关服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions components/logger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>logger</artifactId>
<name>Dew Logger</name>
<description>Dew 日志服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions components/monitor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>monitor</artifactId>
<name>Dew Monitor</name>
<description>Dew 集群监控服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions components/registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>registry</artifactId>
<name>Dew Registry</name>
<description>Dew 注册中心服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions components/wsgateway/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>wsgateway</artifactId>
<name>Dew WSGateway</name>
<description>Dew Websocket网关服务</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
<vertx.version>3.4.0</vertx.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Component
public class WSClient {

@Autowired
private RestTemplate restTemplate;
@Autowired
private WSConfig wsConfig;

@HystrixCommand(fallbackMethod = "serviceFallback")
public String ws(String token, String message) {
return restTemplate.postForEntity(Dew.EB.buildUrl(wsConfig.getWsServiceName(), "ws", token), message, String.class).getBody();
if (Dew.dewConfig.getSecurity().isTokenInHeader()) {
return Dew.EB.post("http://" + wsConfig.getWsServiceName() + "/ws", message, new HashMap<String, String>() {{
put(Dew.dewConfig.getSecurity().getTokenFlag(), token);
}}, String.class).getBody();
} else {
return Dew.EB.post("http://" + wsConfig.getWsServiceName() + "/ws?" + Dew.dewConfig.getSecurity().getTokenFlag() + "=" + token, message, String.class).getBody();
}
}

public String serviceFallback(String token, String message) {
Expand Down
4 changes: 2 additions & 2 deletions modules/boot-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>boot-core</artifactId>
<name>Dew Boot Core</name>
<description>Dew Spring Boot 封装</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
95 changes: 88 additions & 7 deletions modules/boot-core/src/main/java/com/ecfront/dew/core/Dew.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.persistence.Entity;
Expand Down Expand Up @@ -125,16 +130,91 @@ public static DewContext context() {
*/
public static class EB {

public static String buildUrl(String serviceName, String path) {
return buildUrl(serviceName, path, Dew.context().getToken());
private static RestTemplate restTemplate;

public static void setRestTemplate(RestTemplate _restTemplate) {
restTemplate = _restTemplate;
}

public static <T> ResponseEntity<T> get(String url, Class<T> respClazz) {
return get(url, null, respClazz);
}

public static <T> ResponseEntity<T> get(String url, Map<String, String> header, Class<T> respClazz) {
return exchange(HttpMethod.GET, url, null, header, respClazz);
}

public static <T> ResponseEntity<T> delete(String url, Class<T> respClazz) {
return delete(url, null, respClazz);
}

public static <T> ResponseEntity<T> delete(String url, Map<String, String> header, Class<T> respClazz) {
return exchange(HttpMethod.DELETE, url, null, header, respClazz);
}

public static <T> ResponseEntity<T> head(String url, Class<T> respClazz) {
return head(url, null, respClazz);
}

public static <T> ResponseEntity<T> head(String url, Map<String, String> header, Class<T> respClazz) {
return exchange(HttpMethod.HEAD, url, null, header, respClazz);
}

public static <T> ResponseEntity<T> options(String url, Class<T> respClazz) {
return options(url, null, respClazz);
}

public static <T> ResponseEntity<T> options(String url, Map<String, String> header, Class<T> respClazz) {
return exchange(HttpMethod.OPTIONS, url, null, header, respClazz);
}

public static String buildUrl(String serviceName, String path, String token) {
String url = "http://" + serviceName + "/" + path;
if (url.contains("&")) {
return url + "&" + Dew.dewConfig.getSecurity().getTokenFlag() + "=" + token;
public static <T> ResponseEntity<T> post(String url, Object body, Class<T> respClazz) {
return post(url, body, null, respClazz);
}

public static <T> ResponseEntity<T> post(String url, Object body, Map<String, String> header, Class<T> respClazz) {
return exchange(HttpMethod.POST, url, body, header, respClazz);
}

public static <T> ResponseEntity<T> put(String url, Object body, Class<T> respClazz) {
return put(url, body, null, respClazz);
}

public static <T> ResponseEntity<T> put(String url, Object body, Map<String, String> header, Class<T> respClazz) {
return exchange(HttpMethod.PUT, url, body, header, respClazz);
}

public static <T> ResponseEntity<T> exchange(HttpMethod httpMethod, String url, Object body, Map<String, String> header, Class<T> respClazz) {
HttpHeaders headers = new HttpHeaders();
if (header != null) {
header.forEach(headers::add);
}
tryAttachTokenToHeader(headers);
HttpEntity entity;
if (body != null) {
entity = new HttpEntity(body, headers);
} else {
return url + "?" + Dew.dewConfig.getSecurity().getTokenFlag() + "=" + token;
entity = new HttpEntity(headers);
}
return restTemplate.exchange(tryAttachTokenToUrl(url), httpMethod, entity, respClazz);
}

private static String tryAttachTokenToUrl(String url) {
if (!Dew.dewConfig.getSecurity().isTokenInHeader()) {
String token = Dew.context().getToken();
if (url.contains("&")) {
return url + "&" + Dew.dewConfig.getSecurity().getTokenFlag() + "=" + token;
} else {
return url + "?" + Dew.dewConfig.getSecurity().getTokenFlag() + "=" + token;
}
}
return url;
}

private static void tryAttachTokenToHeader(HttpHeaders headers) {
if (Dew.dewConfig.getSecurity().isTokenInHeader()) {
String token = Dew.context().getToken();
headers.add(Dew.dewConfig.getSecurity().getTokenFlag(), token);
}
}

Expand Down Expand Up @@ -271,6 +351,7 @@ public static Optional<OptInfo> getOptInfoByAccCode(String accountCode) {
}

public static void setOptInfo(OptInfo optInfo) {
Dew.cluster.cache.del(Dew.Constant.TOKEN_INFO_FLAG + Dew.cluster.cache.get(Dew.Constant.TOKEN_ID_REL_FLAG + optInfo.getAccountCode()));
Dew.cluster.cache.set(Dew.Constant.TOKEN_ID_REL_FLAG + optInfo.getAccountCode(), optInfo.getToken());
Dew.cluster.cache.set(Dew.Constant.TOKEN_INFO_FLAG + optInfo.getToken(), $.json.toJsonString(optInfo));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public static class DewSecurity {

private boolean tokenInHeader = false;

private boolean tokenHash = false;

public DewSecurityCORS getCors() {
return cors;
}
Expand All @@ -164,6 +166,14 @@ public boolean isTokenInHeader() {
public void setTokenInHeader(boolean tokenInHeader) {
this.tokenInHeader = tokenInHeader;
}

public boolean isTokenHash() {
return tokenHash;
}

public void setTokenHash(boolean tokenHash) {
this.tokenHash = tokenHash;
}
}

public static class DewSecurityCORS {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;

public class DewHandlerInterceptor extends HandlerInterceptorAdapter {

Expand Down Expand Up @@ -36,6 +37,12 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
} else {
token = request.getParameter(Dew.dewConfig.getSecurity().getTokenFlag());
}
if (token != null) {
token = URLDecoder.decode(token, "UTF-8");
if (Dew.dewConfig.getSecurity().isTokenHash()) {
token = $.encrypt.symmetric.encrypt(token, "MD5");
}
}
DewContext context = new DewContext();
context.setId($.field.createUUID());
context.setSourceIP(Dew.Util.getRealIP(request));
Expand Down
4 changes: 2 additions & 2 deletions modules/cloud-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>cloud-core</artifactId>
<name>Dew Cloud Core</name>
<description>Dew Spring Cloud 封装</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions modules/cluster-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>cluster-common</artifactId>
<name>Dew Cluster Common</name>
<description>Dew 集群服务接口</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
</properties>
Expand Down
4 changes: 2 additions & 2 deletions modules/cluster-hazelcast/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<parent>
<groupId>com.ecfront.dew</groupId>
<artifactId>parent</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>
<relativePath>../../modules/parent/pom.xml</relativePath>
</parent>

<artifactId>cluster-spi-hazelcast</artifactId>
<name>Dew Cluster Hazelcast</name>
<description>Dew 集群 Hazelcast 实现</description>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>

<properties>
<hazelcast.verion>3.8.1</hazelcast.verion>
Expand Down
Loading

0 comments on commit 543ea92

Please sign in to comment.