gpt4 book ai didi

spring boot ant匹配器参数

转载 作者:行者123 更新时间:2023-12-04 17:42:47 26 4
gpt4 key购买 nike

我想对这些 URL 中的每一个授予权限:

.antMatchers("/myPage?param1=tata*").hasRole("tata")
.antMatchers("/myPage?param1=toto*").hasRole("toto")

我有这两个网址:
http://localhost:3000/myPage?param1=tata&param2=0001
http://localhost:3000/myPage?param1=toto&param2=0001

如果输入 URL 并且将“ tata”作为参数,我只想使用角色“ tata”访问,并且与“ toto”相同

最佳答案

您可以使用 RegexRequestMatcher而不是 AntPathRequestMatcher

http
.authorizeRequests()
.regexMatchers("\/myPage\?param1=tata(&.*|$)"). hasRole('tata')
.regexMatchers("\/myPage\?param1=toto(&.*|$)"). hasRole('toto')
AntPathRequestMatcher是否 不是 匹配参数,你可以从 code 中读到
private String getRequestPath(HttpServletRequest request) {
if (this.urlPathHelper != null) {
return this.urlPathHelper.getPathWithinApplication(request);
}
String url = request.getServletPath();

String pathInfo = request.getPathInfo();
if (pathInfo != null) {
url = StringUtils.hasLength(url) ? url + pathInfo : pathInfo;
}

return url;
}
RegexRequestMatcher将获得请求路径和 params .
public boolean matches(HttpServletRequest request) {
if (httpMethod != null && request.getMethod() != null
&& httpMethod != valueOf(request.getMethod())) {
return false;
}

String url = request.getServletPath();
String pathInfo = request.getPathInfo();
String query = request.getQueryString();

if (pathInfo != null || query != null) {
StringBuilder sb = new StringBuilder(url);

if (pathInfo != null) {
sb.append(pathInfo);
}

if (query != null) {
sb.append('?').append(query);
}
url = sb.toString();
}

if (logger.isDebugEnabled()) {
logger.debug("Checking match of request : '" + url + "'; against '" + pattern
+ "'");
}

return pattern.matcher(url).matches();
}

关于spring boot ant匹配器参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53726109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com