복붙노트

[SPRING] 봄 부팅 및 봄 보안 다중 로그인 페이지

SPRING

봄 부팅 및 봄 보안 다중 로그인 페이지

@EnableWebSecurity
public class MultiHttpSecurityConfig {

@Configuration
@Order(1)
public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
        .antMatchers("/my/**", "/account/**").access("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
        .and().formLogin().loginPage("/login");
    }
}

@Configuration
@Order(2)
public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .and().formLogin().loginPage("/adminlogin");
    }
}
}

이것은 두 가지 다른 로그인 양식으로 가정합니다. 내 문제는 가장 높은 주문 / adminlogin이 표시되지 않는다는 것입니다. 왜 그런지 알 겠어? 도와주세요. 이 코드는 스프링 부팅에서 가져온 것입니다 - 여러 로그인 페이지를 구성하는 방법은 무엇입니까?

소피아의 제안에 따라 나는 이것을 시도했다.

@Configuration
@Order(2)
public static class UserConfigurationAdapter extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .requestMatcher(new AntPathRequestMatcher("/my/**"))
        .csrf().disable()      
        .authorizeRequests().antMatchers("/my/**").access("hasRole('ROLE_USER')")
        .and().formLogin().loginPage("/login");
    }
}

@Configuration
@Order(1)
public static class AdminConfigurationAdapter extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .requestMatcher(new AntPathRequestMatcher("/admin/**"))
        .csrf().disable()      
        .authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .and().formLogin().loginPage("/adminlogin");
    }
}

그러나 두 경우 모두 / login이 호출됩니다.

해결법

  1. ==============================

    1.관리자 로그인이 활성화되지 않는 이유는 다음과 같습니다. 첫째, 우선 순위가 높지 않기 때문입니다.

    관리자 로그인이 활성화되지 않는 이유는 다음과 같습니다. 첫째, 우선 순위가 높지 않기 때문입니다.

    둘째, HttpSecurity의 Javadoc에 따르면 :

    먼저 다음과 같이 requestMatcher를 구성하여 관리자 페이지에 대해 활성화하도록 HttpSecurity 객체를 제한하십시오.

        http
          .requestMatcher(new AntPathRequestMatcher("/admin/**"))
          .csrf().disable()      
          .authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
          .and().formLogin().loginPage("/adminlogin");
    
  2. from https://stackoverflow.com/questions/39575907/spring-boot-and-spring-security-multiple-login-pages by cc-by-sa and MIT license