java - 具有基本身份验证和 OAuth 订单问题的 Spring Boot Security

标签 java spring spring-boot spring-security

我正在尝试实现一个简单的 spring boot 项目。我有几个 REST-Endpoints,我必须以不同的方式保护它们。一个必须由 Basic Auth 保护,另一个必须使用 OAuth,另一个必须使用自定义安全实现。

REST 端点:

  • /基本/授权
  • /应用程序/安全(oauth)
  • /application/secure2(自己的实现)

从教程中,我知道我必须设置安全适配器的顺序。我的第一个意图是以十步为单位设置顺序(例如 @Order(10)@Order(20)),以防我需要在其中添加其他安全过滤器之间。通过这样做,我调查了以下行为:

  • 如果我添加带有 @Order(10) 的基本身份验证过滤器和带有 @Order(20) 的 OAuth 过滤器,只有 OAuth 过滤器起作用。
  • 如果我添加带有 @Order(1)@Order(2) 的基本身份验证过滤器和带有 @Order(4)< 的 OAuth 过滤器 两个过滤器都有效。
  • 如果我向 @Order(3) 添加过滤器,我会收到一条错误消息,指出订单 3 已在使用中,无法配置两次。

所以有一个默认的 spring 安全适配器(或其他),它具有默认顺序 3。我想我通过添加 @EnableWebSecurity 禁用了每个默认的 spring 安全行为。在我没有通过谷歌找到答案后,我的问题是:

  • 我做的事情对吗?
  • Spring 订购 3 的安全适配器是什么?
  • 默认安全适配器是否会阻止我的基本身份验证实现?

网络安全配置:

   @Configuration
   @EnableWebSecurity
   public class WebSecurityConfig {

    @Order(10)
    @Configuration
    public class BasicAuthConfig extends WebSecurityConfigurerAdapter {
        @Value("${security.user.password}")
        private String password;
        @Value("${security.user.name}")
        private String username;

        private static final String ROLE_ADMIN = "ADMIN";

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser(username).password(password).roles(ROLE_ADMIN);
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
            http.requestMatchers().antMatchers("/basic/**", "/") //
                    .and().authorizeRequests().anyRequest().authenticated() //
                    .and().httpBasic();
        }
    }

    @Order(20)
    @Configuration
    @EnableResourceServer
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    protected static class Oauth2ServerConfig extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception {
            System.out.println("Filter called");
            // @formatter:off
            http.csrf().disable();
            http.authorizeRequests().antMatchers("/application/**").authenticated()
                    // .antMatchers(GET, "/application/secure").authenticated()
                    .anyRequest().authenticated(); 
            // @formatter:on
        }

     // offline token validator

    }

最佳答案

这是一个老问题,但如果有人仍然想知道问题是什么,这里是我的观察:

  • @EnableResourceServer进口ResourceServerConfiguration ,阶数为 3。
  • 有一些方法可以让您在 order 3 资源服务器配置器之前添加 2 个以上的过滤器,例如
    • 通过给其中一些负的顺序值(虽然我不认为负值有任何特殊之处,但需要考虑其他隐式 web security configurers——例如顺序为 0 的那个——由默认值。然而,这意味着随着新功能的引入,不同版本框架中的过滤器之间可能会发生冲突);
    • 通过将它们添加为资源配置器(ResourceServerConfiguration 类不添加任何请求匹配器,但如果用户有没有配置任何东西)。
  • 为了更好地了解配置的请求匹配器中的路径是如何匹配的,您可以快速浏览一下Ant path patterns。 .

关于java - 具有基本身份验证和 OAuth 订单问题的 Spring Boot Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49445394/

相关文章:

java - newLine() 在 apache pdfBox 中占据多少高度?

java - 如何在 Spring Boot 中访问 @JmsListener 使用的 Activity JMS 连接/ session

java - Spring Cloud Gateway 用于复合 API 调用?

spring - 如何使用 TestContainers + Spring Boot + oracle-xe

java - 如何手动描述 java @RequestBody Map<String, String> 的示例输入?

java - 用新对象填充对象数组

java - 无法将名称 [org.hibernate.dialect.HSQDialect] 解析为策略 [org.hibernate.dialect.Dialect]

java - 每个租户的 Hibernate 数据库 Multi-Tenancy 连接提供程序正在为单个租户创建多个数据库连接

java - MongoTemplate如何在查询中添加两个以上 "orOperator"

java - 将spring boot maven项目作为依赖添加到另一个项目(本地)