Несколько страниц входа в систему Spring Spring с помощью Spring Boot - PullRequest
0 голосов
/ 04 октября 2018

У меня есть приложение весенней загрузки, которое будет использоваться двумя разными группами людей, с разными взглядами и функциональностью.Я хочу иметь две разные страницы входа для них

/admin/login

и

/company/login

Я создал два статических класса WebSecurityConfigurerAdapter в своем классе конфигурации безопасности, и у каждой есть аннотация @Order.Один с @Order (1), а другой с @Order (2), как показано ниже

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private final AdminUserDetailService adminUserDetailService;

@Autowired
public SecurityConfig(AdminUserDetailService adminUserDetailService) {
    this.adminUserDetailService = adminUserDetailService;
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(adminUserDetailService);
}
@Configuration
@Order(2)
public static class AdminConfigurationAdapter extends WebSecurityConfigurerAdapter {
    public AdminConfigurationAdapter(){
        super();
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/admin/login").hasRole("Admin")
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/admin/login")
                .permitAll(true)
                .and()
                .logout()
                .logoutSuccessUrl("/admin/logout")
                .permitAll();
    }
}

@Configuration
@Order(1)
public static class CompanyConfigurationAdapter extends WebSecurityConfigurerAdapter {
    public CompanyConfigurationAdapter(){
        super();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/company/login").hasRole("Company Admin")
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .loginPage("/company/login")
                .permitAll(true)
                .and()
                .logout()
                .logoutSuccessUrl("/company/logout")//our new logout success url, we are not replacing other defaults.
                .permitAll();//allow all as it will be accessed when user is not logged in anymore
    }
}
@Override
public void configure(WebSecurity web) throws Exception {
    web
            .ignoring()
            .antMatchers("/resources/**", "/static/**", "/css/**", "/scripts/**", "/img/**", "/vendor/**", "/api/user/**");
}

}

Моя проблема сейчас заключается в том, что весенняя безопасность всегда принимает значение / company / login , даже если я введу / admin / login в URL.Я не уверен, что не так с моей конфигурацией.

1 Ответ

0 голосов
/ 04 октября 2018

Согласно официальному руководству множественный httpsecurity , определите свою первую конфигурацию следующим образом: вам нужно определить базовый путь, иначе он всегда будет вызываться первым.

 @Configuration
    @Order(1)
    public static class CompanyConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.antMatcher("/company/**")
                    .authorizeRequests()
                    .antMatchers("/company/login").hasRole("Company Admin")
                    .anyRequest()
                    .authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/company/login")
                    .permitAll(true)
                    .and()
                    .logout()
                    .logoutSuccessUrl("/company/logout")//our new logout success url, we are not replacing other defaults.
                    .permitAll();//allow all as it will be accessed when user is not logged in anymore
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...