Spring Boot Security身份验证:具有2个不同域URL的应用程序,需要通过Spring Security进行身份验证

时间:2020-03-16 01:48:54

标签: spring spring-boot

我需要有关Spring Boot Security面临的问题之一的帮助。我有两个不同的Urls的应用程序。(Infoblock CNAME)

  1. domain1.com
  2. domain2.com

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

由于商业原因,我们需要2个不同的网址,并且我们计划根据在浏览器中输入的URL进入差异页面。 Spring Security AntMatcher存在问题。 使用AntMatcher,我们只能提供路径,但只能提供路径

你能指导我吗?

预先感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用代替AntMatcher的方式

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

matcher软件包中的任何其他org.springframework.security.web.util.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();
    }
}