Spring Boot Security Authentication: Application having 2 different domain URL and need to authenticate with Spring Security

Viewed 389

I need help with one of the issue that I am facing with Spring Boot Security. I have application which has 2 different Urls.(Infoblock CNAME)

  1. domain1.com
  2. domain2.com

both Url are point to the same application.

Due to business reason we need 2 different Url and we are planning to land on diff page based on the URL entered in browser. Issue is with Spring Security AntMatcher. With AntMatcher we can only provide path but how we can address the domain with it.

Could you please guide me.

Thanks in Advance.

1 Answers

Instead of AntMatcher you can use

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

with any other matcher from org.springframework.security.web.util.matcher package.

Here is an example:

@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();
    }
}
Related