Skip to content

Commit

Permalink
Merge pull request #199 from 17629354490/master
Browse files Browse the repository at this point in the history
add unIdentUrls
  • Loading branch information
gudaoxuri authored Oct 17, 2023
2 parents 0e09e89 + 782ab12 commit fc69516
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,8 @@ public static class Security {

private boolean identInfoEnabled = false;

private String unIdentUrls = "";

private boolean tokenInHeader = true;

private boolean tokenHash = false;
Expand Down Expand Up @@ -809,6 +811,17 @@ public void setIdentInfoEnabled(boolean identInfoEnabled) {
this.identInfoEnabled = identInfoEnabled;
}

/**
* when identInfoEnabled = true then unIdentUrls effective
*/
public String getUnIdentUrls() {
return unIdentUrls;
}

public void setUnIdentUrls(String unIdentUrls) {
this.unIdentUrls = unIdentUrls;
}

/**
* Gets cors.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.ecfront.dew.common.StandardCode;
import group.idealworld.dew.Dew;
import group.idealworld.dew.core.DewContext;
import group.idealworld.dew.core.auth.dto.OptInfo;
import group.idealworld.dew.core.web.error.ErrorController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.AsyncHandlerInterceptor;
Expand All @@ -30,6 +32,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;

/**
Expand All @@ -44,6 +47,19 @@ public class IdentInfoHandlerInterceptor implements AsyncHandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
if (Dew.dewConfig.getSecurity().isIdentInfoEnabled() && StringUtils.isNotEmpty(Dew.dewConfig.getSecurity().getUnIdentUrls())) {
var isUnIdentUrl = Arrays.stream(Dew.dewConfig.getSecurity().getUnIdentUrls().split(","))
.anyMatch(url -> url.equals(request.getRequestURI()));
if (isUnIdentUrl) {
DewContext context = new DewContext();
context.setId($.field.createUUID());
context.setSourceIP(Dew.Util.getRealIP(request));
context.setRequestUri(request.getRequestURI());
context.setInnerOptInfo(Optional.of(new OptInfo()));
DewContext.setContext(context);
return true;
}
}
if (request.getHeader(Dew.dewConfig.getSecurity().getIdentInfoFlag()) == null) {
ErrorController.error(request, response, Integer.parseInt(StandardCode.BAD_REQUEST.toString()),
"The request is missing [" + Dew.dewConfig.getSecurity().getIdentInfoFlag() + "] in header", AuthException.class.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public class IdentInfoTest {

@Test
public void testIdentInfo() throws Exception {
var resultWhite = testRestTemplate.getForEntity("/ident/white", Resp.class);
Assertions.assertEquals(200, resultWhite.getStatusCode().value());
Assertions.assertNotEquals("[]The request is missing [" + Dew.dewConfig.getSecurity().getIdentInfoFlag() + "] in header", resultWhite.getBody().getMessage());

var response = testRestTemplate.getForEntity("/ident-info", Resp.class);
Assertions.assertEquals(200, response.getStatusCode().value());
Assertions.assertTrue(response.getBody().getCode().contains("400"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dew:
security:
ident-info-enabled: true
un_ident_urls: /ident/white

server:
port: 8089
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

import group.idealworld.dew.core.dbutils.dto.Meta;
import group.idealworld.dew.core.dbutils.dto.Page;
import group.idealworld.dew.test.PostgreSqlExtension;
import lombok.Data;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.IOException;
import java.math.BigDecimal;
Expand All @@ -35,9 +40,11 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;


@ExtendWith({SpringExtension.class, PostgreSqlExtension.class})
@ContextConfiguration(initializers = {PostgreSqlExtension.Initializer.class})
@SpringBootApplication
@SpringBootTest
@Testcontainers
public class DbutilsTest {

private static final Logger LOGGER = LoggerFactory.getLogger(DbutilsTest.class);
Expand Down

0 comments on commit fc69516

Please sign in to comment.