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

标签 java spring security spring-security

我有一个正在运行的 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/

相关文章:

java:从网络服务请求中获取我的 war 的 list 文件

javascript - 更新前验证字段

spring - 如何在 Spring 中向现有的映射资源或现有的 hbm 列表添加更多的 hbm

php - 从书面脚本外部调用 JavaScript 函数是否安全?

c - 在 C 中定义一个值是一个安全问题吗?

android - 关于调试器安全的问题

java - 在没有 Maven 的情况下设置 JSF 项目

java - 意外绑定(bind) - 为什么我不能在此收集器中使用类似 T extends Collection 的东西?

java - 无法重新抛出异常

Java Spring MVC 在同一个 JSP 中获取/发布