gpt4 book ai didi

java - Spring Security oauth 并关闭某些 url 的安全性,但保持基本身份验证激活

转载 作者:行者123 更新时间:2023-12-01 09:45:03 24 4
gpt4 key购买 nike

我有一个正在运行的 spring security oauth2 项目。

HttpSecurity 配置如下所示:

http.antMatcher("/**").authorizeRequests().antMatchers(RESOURCE_LOCATIONS).permitAll().and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and().httpBasic().disable()
.authorizeRequests().anyRequest().authenticated();

通过此设置,除了 RESOURCE_LOCATIONS 下面的 URL 之外的任何请求都将受到 ouath2 协议(protocol)的保护。对于这些请求,HTTP Basic 已关闭。

现在我希望/internal/** 下面的网址受到保护,但只能通过 HTTP 基本身份验证。

我正在尝试类似的方法或类似的方法:

http.antMatcher("/**").authorizeRequests().antMatchers(RESOURCE_LOCATIONS).permitAll().and()
.antMatcher("/internal/**").httpBasic().and() //<--ADDED THIS LINE
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and().httpBasic().disable()
.authorizeRequests().anyRequest().authenticated();

但令人惊讶的是,所有请求都是公开的并且没有基本身份验证。

有人有解决这个问题的提示吗?

BR,迈克尔

最佳答案

嗯,

如果您希望 url/internal/** 受到保护,更好的形式是使用 hasRole('') 和 httpBasic。您的应用程序需要通过简单的 HTTP 基本身份验证进行身份验证。但其他网页不应该使用 HTTP basic,而应该使用普通形式登录。因此,您可以创建一个 WebSecurity 只是为了 url 'Internal',如下所示。我希望这可以帮助你!

@Configuration
@Order(1)
public static class InternalWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.antMatcher("/internal/**")
.authorizeRequests()
.anyRequest().hasAnyRole("ADMIN", "USER")
.and()
.httpBasic();
}
}

关于java - Spring Security oauth 并关闭某些 url 的安全性,但保持基本身份验证激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38123687/

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