Skip to content

Instantly share code, notes, and snippets.

@maykon-oliveira
Last active July 17, 2020 00:15
Show Gist options
  • Save maykon-oliveira/2e52c287e8c555e8364c11c9b1aced58 to your computer and use it in GitHub Desktop.
Save maykon-oliveira/2e52c287e8c555e8364c11c9b1aced58 to your computer and use it in GitHub Desktop.
Matches if the Pattern and/or HttpMethod matches the path within the application.
package com.maykonoliveira.examechunindevdojo.config;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.regex.Pattern;
/** @author maykon-oliveira */
public class RegexServerWebExchangeMatcher implements ServerWebExchangeMatcher {
private final Pattern pattern;
private final HttpMethod method;
public RegexServerWebExchangeMatcher(Pattern pattern) {
this.pattern = pattern;
this.method = null;
}
public RegexServerWebExchangeMatcher(HttpMethod method, Pattern pattern) {
this.pattern = pattern;
this.method = method;
}
@Override
public Mono<MatchResult> matches(ServerWebExchange serverWebExchange) {
var request = serverWebExchange.getRequest();
if (this.method != null && !this.method.equals(request.getMethod())) {
return MatchResult.notMatch();
} else {
var uri = request.getPath().toString();
var matcher = pattern.matcher(uri);
return matcher.find()
? ServerWebExchangeMatcher.MatchResult.match()
: ServerWebExchangeMatcher.MatchResult.notMatch();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment