gpt4 book ai didi

java - Spring Security 多个 url 规则集不能一起工作

转载 作者:行者123 更新时间:2023-12-01 21:19:06 24 4
gpt4 key购买 nike

我有一个 HTTP Spring Security 配置,当我注释掉每个单独的方面时,它似乎可以工作,但当我将 Spring Security 规则组合在一起时,它不起作用,所以我知道问题不在于 regexMatcherantMatcher但结合应用规则。

这是我的 Spring Security 类(class):

package com.driver.website.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.security.web.header.writers.StaticHeadersWriter;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import java.security.AccessControlContext;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Value("${widget.headers.xframeoptions.domains.allowed}")
private String allowedXFrameOptions;

@Value("${widget.headers.origins.allowed}")
private String allowedOrigins;

@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off

http.exceptionHandling().accessDeniedPage("/login")
.and()
.formLogin().loginPage("/login").defaultSuccessUrl("/myaccount", true).permitAll()
.and()
.authorizeRequests()
.antMatchers("/**").permitAll();

http.regexMatcher("^((?!(/widget|/assistedSearch)).)*$")
.headers().frameOptions().disable()
.regexMatcher("^((?!(/widget|/assistedSearch)).)*$")
.headers()
.xssProtection()
.contentTypeOptions()
.addHeaderWriter(new StaticHeadersWriter("X-FRAME-OPTIONS", "SAMEORIGIN"));

http.antMatcher("/widget")
.headers()
.frameOptions()
.disable()
.antMatcher("/widget")
.headers()
.addHeaderWriter(new StaticHeadersWriter("X-FRAME-OPTIONS", "ALLOW-FROM " + allowedXFrameOptions));

http.requestMatchers().antMatchers("/assistedSearch", "/widget")
.and()
.headers()
.addHeaderWriter(new StaticHeadersWriter("Access-Control-Allow-Origin", allowedOrigins))
.addHeaderWriter(new StaticHeadersWriter("Access-Control-Allow-Methods", "GET, POST"))
.addHeaderWriter(new StaticHeadersWriter("Access-Control-Allow-Headers", "Content-Type"));

// @formatter:on
}
}

规则应该是......

  • 对于除/widget 和/assistedSearch 之外的所有网址,我们应该添加 SAMEORIGIN X-Frame-Options header
  • 对于/widget端点我们应该添加 X-Frame-Options: ALLOW-FROM header
  • 对于/widget/assistedSearch端点我们应该添加 Access-Control-Allow-Origin , Access-Control-Allow-MethodsAccess-Control-Allow-Headers标题

正如我上面提到的,如果我注释掉 For all urls然后其他两个规则集协同工作,但使用 For all urls规则取消注释,不会出现任何标题。

有人知道为什么会这样吗?如何在 Spring Security 中添加多个规则集并用新规则集覆盖现有规则集?​​

我试过了

http.antMatcher("/widget")
.headers()
.frameOptions()
.disable()

这似乎再次单独起作用,但不能组合使用。

提前致谢!

最佳答案

您覆盖之前的匹配器,请参阅 HttpSecurity.html#antMatcher :

Invoking antMatcher(String) will override previous invocations of mvcMatcher(String)}, requestMatchers(), antMatcher(String), regexMatcher(String), and requestMatcher(RequestMatcher).

HttpSecurity.html#regexMatcher :

Invoking regexMatcher(String) will override previous invocations of mvcMatcher(String)}, requestMatchers(), antMatcher(String), regexMatcher(String), and requestMatcher(RequestMatcher).

如果您想要 HttpSecurity 的多个配置,参见Spring Security Reference :

We can configure multiple HttpSecurity instances just as we can have multiple <http> blocks. The key is to extend the WebSecurityConfigurationAdapter multiple times. For example, the following is an example of having a different configuration for URL’s that start with /api/.

@EnableWebSecurity
public class MultiHttpSecurityConfig {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) { 1
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}

@Configuration
@Order(1) 2
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**") 3
.authorizeRequests()
.anyRequest().hasRole("ADMIN")
.and()
.httpBasic();
}
}

@Configuration 4
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
}

关于java - Spring Security 多个 url 规则集不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457121/

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