【问题标题】:Springboot REST - Expose same service as secured & non-securedSpring Boot REST - 公开与安全和不安全相同的服务
【发布时间】:2019-07-08 12:26:52
【问题描述】:

我想在 springboot 应用程序中为我的 Spring https REST 服务(当前不安全)启用基本身份验证。多个客户正在使用这些服务,而有些客户可以转移到安全版本,有些客户仍想再使用几个月的不安全版本。如何在同一个 Spring Boot 应用程序中将相同的服务公开为既安全又不安全?

虽然我已经为 Apache cxf REST 服务做到了这一点,方法是在不同的端口托管相同的服务并只保护一个端口,但不知道如何在 springboot 中完成。

【问题讨论】:

    标签: rest spring-boot


    【解决方案1】:

    使用两个端点创建 RequestMapping,如下所示。想要使用基本身份验证的客户将使用/secure/** 提供服务(没有身份验证无法访问),而其他几个月后将迁移到安全的客户将使用/unsecure/** 提供服务(任何人都可以访问)。您可以使用类级别 RequestMapping 来避免在方法级别更改每个端点

    @GetMapping(value= {"/secure/users","/unsecure/users"})
        public ResponseEntity<List<User>> findAllUsers()
        {   
            ...
        }
    

    现在如下配置安全性。为此,您需要将客户端角色存储在数据库中

    @Override
        protected void configure(HttpSecurity http) throws Exception{
    
             http
             .csrf().disable()
             .authorizeRequests()
             .antMatchers("/unsecure/**").permitAll()
             .antMatchers("/secure/**").hasRole("CLIENT_SECURE") 
             .anyRequest().authenticated();
    
        }
    

    Working Git Example

    安全端点: GET http://localhost:8088/secure/users 状态 403

    不安全的端点: GET: http://localhost:8088/unsecure/users 状态 200

    【讨论】:

      猜你喜欢
      • 2018-03-25
      • 2015-01-31
      • 1970-01-01
      • 2023-01-15
      • 2022-01-03
      • 1970-01-01
      • 2017-03-13
      • 2012-03-31
      • 1970-01-01
      相关资源
      最近更新 更多