繁体   English   中英

Spring boot - 如何配置多个登录页面?

[英]Spring boot - how to configure multiple login pages?

在我的团队中,我们使用Spring Boot编写了Spring应用程序+ SAPUI5门户。 Web应用程序分为三个不同的位置,例如:

webapp: - app1 - app2 - app3

为了访问这些应用程序,我们实现了登录页面。 根据用户角色,我们将用户重定向到确切的应用。

我的spring应用程序安全性如下:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/app1/**/*.*")
                .permitAll()
                .antMatchers("/register.html")
                .permitAll()
                //
                .antMatchers("/app2/*.*")
                .hasRole("USER")
                //
                //
                .antMatchers("/login*")
                .permitAll()
                .antMatchers("/soap/*")
                .permitAll()
                .antMatchers("/postLogin")
                .authenticated()
                //
                .antMatchers("/app3/*")
                //.permitAll()
                .hasRole("ADMIN")
                //
                .anyRequest()
                .authenticated()
                // log in
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=loginError")
                .defaultSuccessUrl("/postLogin")
                // logout
                .and().logout().logoutUrl("/**/logout")
                .logoutSuccessUrl("/login").deleteCookies("JSESSIONID").and()
                .csrf()
                .disable()

当然我们上课有重定向。 现在我们必须提供每个应用程序,不同的登录页面。 我尝试将spring security配置为在不同页面上接受多个登录表单,但它不起作用。 可能吗? 我阅读了文档,但它没有结果。

您应该可以通过使用不同的实例配置多个HttpSecurity对象来完成此操作。 它类似于这个问题和Spring Security 文档 基本上,您在配置类中定义了多个用于扩展WebSecurityConfigurerAdapter的静态类。 我自己使用它来配置基于URL的不同类型的auth(表单/基本)并进行快速测试以确认它。 在你的例子中我相信这样的事情(如果我正确地读你的意图):

@EnableWebSecurity
public class MultiHttpSecurityConfig {

    @Configuration
    @Order(1)
    public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter {
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/app1/**/*.*")
                    .permitAll()
                    .antMatchers("/register.html")
                    .permitAll()
                    .anyRequest()
                    .authenticated()
                    // log in
                    .and()
                    .formLogin()
                    .loginPage("/login")
                    .failureUrl("/login?error=loginError")
                    .defaultSuccessUrl("/postLogin")
                            // logout
                    .and().logout().logoutUrl("/**/logout")
                    .logoutSuccessUrl("/login").deleteCookies("JSESSIONID").and()
                    .csrf()
                    .disable();
        }
    }

    @Configuration
    public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/app2/*.*")
                    .hasRole("USER")
                            // log in
                    .and()
                    .formLogin()
                    .loginPage("/login2")
                    .failureUrl("/login2?error=loginError")
                    .defaultSuccessUrl("/postLogin")
                            // logout
                    .and().logout().logoutUrl("/**/logout")
                    .logoutSuccessUrl("/login2").deleteCookies("JSESSIONID").and()
                    .csrf()
                    .disable();
        }
    }
}

请注意,这些并不是真正不同的应用程序实例,因此如果您作为特定用户进行身份验证,然后转到未经授权的区域,则不会将您重定向到登录名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM