首页 > 解决方案 > SpringBoot 2.1.3 首次认证的安全多重可用登录页面

问题描述

我正在开发一个SpringBoot 2.1.3++ Thymeleaf 3WebApp Tomcat 8。我已经实施Spring Security并且一切正常。现在我有一个小问题,因为我想实现两个表单登录页面,一个用于后台用户,另一个用于所有其他用户。

我不想将两个表单放在同一个页面中,并且喜欢为后台用户创建一个带有简单表单的页面,并为客户用户创建一个重定向到另一个页面(使用另一个表单)的链接。

我已经阅读了一些内容,并且找到了一种订购WebSecurityConfigurerAdapter和创建多个入口点的方法,但是通过这种方式,我可以使用订单 1 的页面登录,然后我可以转到另一个表单页面。这不是我想做的。

你知道是否有办法做到这一点?谢谢

标签: spring-bootspring-securitythymeleaf

解决方案


I found a way and it is pretty simple. I have configured the WebSecurityConfigurerAdapter as follow:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/login-procout").permitAll() // login for furnishers
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login-officers") // default login page
            .permitAll()
            .successHandler(customAuthenticationSuccessHandler())
            .and()
        .logout()
            .permitAll();

    http.csrf().disable();
}

Then I create two html page login-officers.html and login-procout.html referred by Controller as follow:

@Controller
public class MyController {

   @GetMapping("/login-officers")
   public String loginOfficers() {
      return "login-officers";
   }

   @GetMapping("/login-procout")
   public String loginProcout() {
      return "login-procout";
   }

}

And in both page I have default Spring Security Form:

<form action="" th:action="@{/login}" method="post">
   <input type="text" name="username" id="username" placeholder="Username"/>
   <input type="password" name="password" id="password" placeholder="Password"/>
   <input type="submit" id="login" value="Log In" />
</form>

I don't know if is the right way to do but it works.


推荐阅读