Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize auth #77

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions framework/modules/boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
<!--Fixed version conflicts-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Dew 核心配置.
*
* @author gudaoxuri
* @author gjason
*/
@ConfigurationProperties(prefix = "dew")
public class DewConfig {
Expand Down Expand Up @@ -710,6 +713,10 @@ public static class Security {

private boolean tokenHash = false;

private Router router = new Router();

private long optExpiration = 86400L;

/**
* Gets cors.
*
Expand Down Expand Up @@ -782,6 +789,44 @@ public void setTokenHash(boolean tokenHash) {
this.tokenHash = tokenHash;
}


/**
* Get route urls.
*
* @return the Router
*/
public Router getRouter() {
return router;
}

/**
* Sets route urls.
*
* @param router the router info
*/
public void setRouter(Router router) {
this.router = router;
}

/**
* Gets optExpiration.
*
* @return the expiration
*/
public long getOptExpiration() {
return optExpiration;
}

/**
* Sets expiration.
*
* @param optExpiration the expiration
*/
public void setOptExpiration(long optExpiration) {
this.optExpiration = optExpiration;
}


/**
* Security cors.
*/
Expand Down Expand Up @@ -846,6 +891,51 @@ public void setAllowHeaders(String allowHeaders) {
}
}

/**
* URL Router.
*/
public static class Router {

private boolean enabled = false;
private Map<String, List<String>> blackUri = new LinkedHashMap<>();

/**
* Is enabled boolean.
*
* @return the boolean
*/
public boolean isEnabled() {
return enabled;
}

/**
* Sets enabled.
*
* @param enabled the enabled
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

/**
* Get black uri list.
*
* @return Map The request url method with black urs
*/
public Map<String, List<String>> getBlackUri() {
return blackUri;
}

/**
* Sets black uris.
*
* @param blackUri black uri list
*/
public void setBlackUri(Map<String, List<String>> blackUri) {
this.blackUri = blackUri;
}
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* 基础登录鉴权适配器.
*
* @author gudaoxuri
* @author gjason
*/
public class BasicAuthAdapter implements AuthAdapter {

Expand Down Expand Up @@ -58,8 +59,8 @@ public void removeOptInfo(String token) {
@Override
public <E extends OptInfo> void setOptInfo(E optInfo) {
Dew.cluster.cache.del(TOKEN_INFO_FLAG + Dew.cluster.cache.get(TOKEN_ID_REL_FLAG + optInfo.getAccountCode()));
Dew.cluster.cache.set(TOKEN_ID_REL_FLAG + optInfo.getAccountCode(), optInfo.getToken());
Dew.cluster.cache.set(TOKEN_INFO_FLAG + optInfo.getToken(), $.json.toJsonString(optInfo));
Dew.cluster.cache.setex(TOKEN_ID_REL_FLAG + optInfo.getAccountCode(), optInfo.getToken(), Dew.dewConfig.getSecurity().getOptExpiration());
Dew.cluster.cache.setex(TOKEN_INFO_FLAG + optInfo.getToken(), $.json.toJsonString(optInfo), Dew.dewConfig.getSecurity().getOptExpiration());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@
import com.ecfront.dew.common.$;
import ms.dew.Dew;
import ms.dew.core.DewContext;
import ms.dew.core.web.error.ErrorController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
* Dew Servlet拦截器.
*
* @author gudaoxuri
* @author gjason
*/
public class BasicHandlerInterceptor extends HandlerInterceptorAdapter {

private static final Logger logger = LoggerFactory.getLogger(BasicHandlerInterceptor.class);
private AntPathMatcher pathMatcher = new AntPathMatcher();

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Expand Down Expand Up @@ -65,6 +73,14 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
token = $.security.digest.digest(token, "MD5");
}
}
// 请求黑名单拦截
if (Dew.dewConfig.getSecurity().getRouter().isEnabled()
&& blackRequest(request.getMethod(), request.getRequestURI())) {
ErrorController.error(request, response, 403,
String.format("The current[%S][%s] request is not allowed",
request.getRequestURI(), request.getMethod()), AuthException.class.getName());
return false;
}
DewContext context = new DewContext();
context.setId($.field.createUUID());
context.setSourceIP(Dew.Util.getRealIP(request));
Expand All @@ -79,4 +95,26 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return super.preHandle(request, response, handler);
}

/**
* 黑名单.
*
* @param method Request method
* @param requestUri URL
*/
public boolean blackRequest(String method, String requestUri) {

/*
兼容requestUri末尾包含/的情况
*/
final String reqUri = requestUri.replaceAll("/+$", "");
method = method.toLowerCase();
if (method.equalsIgnoreCase(HttpMethod.OPTIONS.name())) {
return false;
}
final List<String> blacks = Dew.dewConfig.getSecurity().getRouter().getBlackUri().getOrDefault(method, new ArrayList<>());
if (logger.isDebugEnabled()) {
logger.debug("the black apis are {}", $.json.toJsonString(blacks));
}
return blacks.stream().anyMatch(uri -> pathMatcher.match(uri, reqUri));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public class AllTest {
*/
@Test
public void testAll() throws Exception {
clusterTest.testAll();
webTest.testAll();
// clusterTest.testAll();
// webTest.testAll();
authTest.testAll();
notifyTest.testAll();
docTest.testAll();
// notifyTest.testAll();
// docTest.testAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public void testAll() throws Exception {
userDTO.setPhone("15957199704");
Resp registerResult = testRestTemplate.postForObject("/auth/register", userDTO, Resp.class);
Assert.assertEquals("200", registerResult.getCode());
//["/auth/register/*","/auth/re?","/user/**","/tes[t]"]
registerResult = testRestTemplate.postForObject("/auth/register/user", userDTO, Resp.class);
Assert.assertEquals("403", registerResult.getCode());
registerResult = testRestTemplate.postForObject("/auth/reg", userDTO, Resp.class);
Assert.assertEquals("403", registerResult.getCode());
registerResult = testRestTemplate.postForObject("/user/register/hello", userDTO, Resp.class);
Assert.assertEquals("403", registerResult.getCode());
registerResult = testRestTemplate.postForObject("/test", userDTO, Resp.class);
Assert.assertEquals("403", registerResult.getCode());

AuthController.LoginDTO loginDTO = new AuthController.LoginDTO();
loginDTO.setIdCard(userDTO.getIdCard());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ dew:
token-flag: _token_
token-in-header: true
token-hash: false
router:
enabled: true
blackUri:
post: ["/auth/register/*","/auth/re?","/user/**","/tes{t}"]

spring:
application:
Expand Down
6 changes: 0 additions & 6 deletions framework/modules/cloud-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
<!--Fixed version conflicts-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<!--Fixed ClassNotFoundException-->
<dependency>
<groupId>com.netflix.servo</groupId>
Expand Down