【问题标题】:Spring Boot Security Authentication: Application having 2 different domain URL and need to authenticate with Spring SecuritySpring Boot Security Authentication:应用程序具有 2 个不同的域 URL,需要通过 Spring Security 进行身份验证
【发布时间】:2020-03-16 01:48:54
【问题描述】:

我需要帮助解决我在 Spring Boot 安全方面面临的问题之一。我的应用程序有 2 个不同的 Url。(Infoblock CNAME)

  1. domain1.com
  2. domain2.com

两个 URL 都指向同一个应用程序。

由于业务原因,我们需要 2 个不同的 URL,我们计划根据在浏览器中输入的 URL 登陆差异页面。问题在于 Spring Security AntMatcher。 使用 AntMatcher,我们只能提供路径,但我们如何使用它来寻址域

请指导一下。

提前致谢。

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    你可以使用 AntMatcher 来代替

     http.requestMatcher(new RequestHeaderRequestMatcher("Host", "127.0.0.1:8080"))
    

    与来自org.springframework.security.web.util.matcher 包的任何其他matcher

    这是一个例子:

    @EnableWebSecurity
    @Configuration
    public static class WebSecurityConfig extends WebSecurityConfigurerAdapter   {
    
        @Configuration
        @Order(1)
        public static class SecConfig1 extends WebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.requestMatcher(new RequestHeaderRequestMatcher("Host", "127.0.0.1:8080"))
                        .authorizeRequests()
                        .anyRequest()
                        .authenticated()
                .and()
                        .formLogin();
            }
        }
        @Configuration
        @Order(2)
        public static class SecConfig2 extends WebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.requestMatcher(new RequestHeaderRequestMatcher("Host", "127.0.0.2:8080"))
                        .authorizeRequests()
                        .anyRequest()
                        .authenticated()
                        .and()
                        .httpBasic();
            }
        }
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            //default deny all
            http.authorizeRequests().anyRequest().denyAll();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 2013-02-23
      • 1970-01-01
      • 2016-02-10
      • 2013-04-07
      • 2014-02-01
      • 2013-06-12
      • 2016-01-05
      • 2019-05-15
      相关资源
      最近更新 更多