当前位置: 首页 > 知识库问答 >
问题:

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

段干靖
2023-03-14

与我的团队一起,我们使用Spring Boot编写了Spring application+SAPUI5门户。Web应用程序分为三个独立的位置,例如:

我的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配置为接受不同页面上的多个登录表单,但它不起作用。有可能吗?我阅读了文档,但没有定论。

共有1个答案

吴驰
2023-03-14

您应该能够通过使用不同实例配置多个HttpSecurity对象来做到这一点。它类似于这个问题和这里的Spring Security文档。基本上,在扩展WebSecurityConfigurerAdapter的配置类中定义多个静态类。我自己也用它来根据URL配置不同类型的auth(form/basic)并做了一个快速测试来确认它。我相信你的例子中有这样的东西(如果我正确地理解了你的意图的话):

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

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

 类似资料:
  • 与我的团队一起,我们使用Spring Boot编写了Spring application+SAPUI5门户。Web应用程序分为三个独立的位置,例如: 我的spring应用程序安全性如下所示: 当然,我们也有重定向课。现在我们必须为每个应用程序提供不同的登录页面。我试图将spring security配置为接受不同页面上的多个登录表单,但它不起作用。有可能吗?我阅读了文档,但没有定论。

  • 我尝试将springboot应用程序配置为每天登录一个文件,因此我将logback.xml配置为: 所以当我尝试运行我的应用程序时,我得到了这个错误:

  • 问题内容: 用于登录的配置文件可以在类路径中找到,因此是Eclipse项目特定的,这不是我想要的。我正在使用多个Java实用程序,它们全部都驻留在一个项目中(共享类路径),并且其中一些需要使用特定的配置。 我尝试了变量替换和Joram配置器,但没有任何效果。这很可能是我的错,我有一天要解决,但是现在我需要一个简单的解决方案。 问题答案: 选项1:使用logback.configurationFil

  • 我试图使用python请求登录到我的大学的CAS。我在网上看过一些教程和帖子,但是它们都适用于在同一个页面上有用户名和密码字段的CAS。这是我想登录的网站:https://cas.tamu.edu/cas/login?service=https://howdy.tamu.edu/uPortal/Login 如您所见,只有一个用户名字段,然后单击提交后,您将被带到同一个页面,只有该字段现在是密码。这

  • 在使用QQ登录之前,需要到QQ互联申请通过开发者资料,成为个人开发者或者公司开发者。 如何申请通过开发者? 登录QQ互联平台,点击应用管理,然后点击QQ头像,进入开发者认证页面,如已经通过认证,直接创建应用。 填写开发者资料,按照网页要求填写信息并提交审核即可。如下图: 开发者认证完成后,在应用管理中心显示“审核已通过”状态。 QQ第三方登陆配置 第一步,商城启用QQ登陆,需要在Niushop商城

  • 我有一个示例web应用程序,它使用Google的Spring OAuth2身份验证。HttpSecurity配置如下: 现在,每当发出未经身份验证的请求时,此配置都会将浏览器重定向到登录页面。此页面显示了指向谷歌身份验证的链接 通过谷歌登录以及登录表单。 但我希望浏览器直接重定向到Google的身份验证服务器,而不是应用程序的登录页面。我不想要任何表单登录/注册功能。我只想让用户使用谷歌登录。 为