spring - spring boot ant matchers 参数
问题描述
我想授予以下每个 URL 的权限:
.antMatchers("/myPage?param1=tata*").hasRole("tata")
.antMatchers("/myPage?param1=toto*").hasRole("toto")
我有这两个网址:
http://localhost:3000/myPage?param1=tata¶m2=0001
http://localhost:3000/myPage?param1=toto¶m2=0001
如果输入了 URL 并且将“ tata
”作为参数,我只想使用角色“ tata
”和“”访问相同的toto
东西
解决方案
您可以使用RegexRequestMatcher
而不是AntPathRequestMatcher
http
.authorizeRequests()
.regexMatchers("\/myPage\?param1=tata(&.*|$)"). hasRole('tata')
.regexMatchers("\/myPage\?param1=toto(&.*|$)"). hasRole('toto')
AntPathRequestMatcher
与参数不匹配,您可以从代码中读取
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
将获得请求路径和参数。
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();
}
推荐阅读
- typo3 - 如何覆盖后端流体模板?
- docker - 292 年前关于 kaniko 构建的 Docker 历史
- sql-server - 如何加快从具有大量条目的表中删除条目的速度
- linux - 如何使用 tor 运行颤振命令
- javascript - Next js 服务器错误:元素类型无效:需要一个字符串(用于内置组件)或一个类/函数,但得到:未定义
- javascript - 如何让 JavaScript 按钮调用新的 Firebase (v9.0.0+) signInWithPopup
- python - python:将csv文件转换为json格式并尝试调用数据中没有另一列的列
- go - 无法在 Golang 中返回值
- ios - CoreData+CloudKit 集成问题
- sql - 如何从 SQL 中以逗号存储的 id 获取名称