gpt4 book ai didi

java - URL 和方法的 spring 安全设置

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:02 26 4
gpt4 key购买 nike

我有一个关于 Spring Security 的问题。我有几个页面 - A、B 和 C - 以及 4 个操作它们的 http 方法:GET、PUT、POST、DELETE。对于每个 A+GET 组合,我希望拥有 resource-Page-Method 形式的特殊权限。我该如何实现?以下代码不起作用:如果用户没有任何权限,它会允许所有用户事件执行所有操作。

@Override
protected void configure(HttpSecurity http) throws Exception {


RequestHeaderAuthenticationFilter siteMinderFilter = new RequestHeaderAuthenticationFilter();
siteMinderFilter.setPrincipalRequestHeader("SM_USER");
siteMinderFilter.setAuthenticationManager(authenticationManager());

http.addFilter(siteMinderFilter);


List<HttpMethod> methods = new ArrayList<HttpMethod>();
methods.add(HttpMethod.GET);
methods.add(HttpMethod.POST);
methods.add(HttpMethod.PUT);
methods.add(HttpMethod.DELETE);

List<String> resources = new ArrayList<String>();
resources.add("A");
resources.add("B");
resources.add("C");
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();

for (HttpMethod method:methods){
for (String resource: resources){
String auth = "resource-"+resource+"-"+method.name();
registry.antMatchers(method, "**/"+resource+"/**")
.hasAuthority(auth);
}
}

http = registry.and();
http.formLogin();

}

最佳答案

过滤器不起作用的唯一原因是匹配器模式开头缺少斜杠 /:

代替这个

 registry.antMatchers(method, "**/"+resource+"/**") 

我应该写这个

 registry.antMatchers(method, "/**/"+resource+"/**")

关于java - URL 和方法的 spring 安全设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31788636/

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