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

Add local API authorization #2975

Merged
merged 9 commits into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions shenyu-bootstrap/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ shenyu:
printInterval: 60000
ribbon:
serverListRefreshInterval: 10000
localAPI:
sha512Key: "your_plaintext_password"
local:
enabled: true
sha512Key: "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413"

eureka:
client:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ public class ShenyuConfig {

private RibbonConfig ribbon = new RibbonConfig();

private Local local = new Local();

/**
* Gets the local config.
*
* @return the local config
*/
public Local getLocal() {
return local;
}

/**
* Sets the local config.
*
* @param local the local config
*/
public void setLocal(final Local local) {
this.local = local;
}

/**
* Gets ribbon.
*
Expand Down Expand Up @@ -1050,4 +1070,57 @@ public void setServerListRefreshInterval(final Integer serverListRefreshInterval
this.serverListRefreshInterval = serverListRefreshInterval;
}
}

/**
* The local config.
*/
public static class Local {

private Boolean enabled = true;

private String sha512Key;

public Local() {
}

public Local(final String sha512Key) {
this.sha512Key = sha512Key;
}

/**
* Gets enabled.
*
* @return the enabled
*/
public Boolean getEnabled() {
return enabled;
}

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

/**
* Get Sha512Key.
*
* @return the key
*/
public String getSha512Key() {
return sha512Key;
}

/**
* Set Sha512Key.
*
* @param sha512Key sha512Key
*/
public void setSha512Key(final String sha512Key) {
this.sha512Key = sha512Key;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@ public RemoteAddressResolver remoteAddressResolver() {
* Local dispatcher filter web filter.
*
* @param dispatcherHandler the dispatcher handler
* @param shenyuConfig the shenyuConfig
*
* @return the web filter
*/
@Bean
@Order(-200)
@ConditionalOnProperty(name = "shenyu.switchConfig.local", havingValue = "true", matchIfMissing = true)
public WebFilter localDispatcherFilter(final DispatcherHandler dispatcherHandler) {
return new LocalDispatcherFilter(dispatcherHandler);
@ConditionalOnProperty(name = "shenyu.local.enable", havingValue = "false", matchIfMissing = true)
public WebFilter localDispatcherFilter(final DispatcherHandler dispatcherHandler, final ShenyuConfig shenyuConfig) {
return new LocalDispatcherFilter(dispatcherHandler, shenyuConfig.getLocal().getSha512Key());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.utils.PathMatchUtils;
import org.apache.shenyu.common.utils.ShaUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.ResponseStatusException;
Expand All @@ -41,16 +40,16 @@ public class LocalDispatcherFilter implements WebFilter {

private final DispatcherHandler dispatcherHandler;

@Value("${shenyu.localAPI.sha512Key}")
private String sha512Key;

/**
* Instantiates a new Local dispatcher filter.
*
* @param dispatcherHandler the dispatcher handler
*/
public LocalDispatcherFilter(final DispatcherHandler dispatcherHandler) {
public LocalDispatcherFilter(final DispatcherHandler dispatcherHandler, final String sha512Key) {
this.dispatcherHandler = dispatcherHandler;
this.sha512Key = sha512Key;
}

/**
Expand All @@ -67,7 +66,7 @@ public Mono<Void> filter(@Nonnull final ServerWebExchange exchange, @Nonnull fin
String path = exchange.getRequest().getURI().getPath();
if (PathMatchUtils.match(DISPATCHER_PATH, path)) {
String key = exchange.getRequest().getHeaders().getFirst(Constants.X_ACCESS_TOKEN);
if (Objects.isNull(sha512Key) || !ShaUtils.shaEncryption(sha512Key).equals(key)) {
if (Objects.isNull(sha512Key) || !sha512Key.equalsIgnoreCase(ShaUtils.shaEncryption(key))) {
tuohai666 marked this conversation as resolved.
Show resolved Hide resolved
return Mono.error(new ResponseStatusException(HttpStatus.FORBIDDEN, "The key is not correct."));
}
return dispatcherHandler.handle(exchange);
Expand Down