spring-boot - SpringBoot 2.1.3 首次认证的安全多重可用登录页面
问题描述
我正在开发一个SpringBoot 2.1.3
++ Thymeleaf 3
WebApp Tomcat 8
。我已经实施Spring Security
并且一切正常。现在我有一个小问题,因为我想实现两个表单登录页面,一个用于后台用户,另一个用于所有其他用户。
我不想将两个表单放在同一个页面中,并且喜欢为后台用户创建一个带有简单表单的页面,并为客户用户创建一个重定向到另一个页面(使用另一个表单)的链接。
我已经阅读了一些内容,并且找到了一种订购WebSecurityConfigurerAdapter
和创建多个入口点的方法,但是通过这种方式,我可以使用订单 1 的页面登录,然后我可以转到另一个表单页面。这不是我想做的。
你知道是否有办法做到这一点?谢谢
解决方案
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.
推荐阅读
- amazon-web-services - 阶跃函数 - 数组输入
- xcode - 如果我删除所有苹果证书、个人资料会怎样?
- python - 算法可视化工具 DFS 实施错误
- python - 在 SQLAlchemy 中建模多项选择题和正确答案
- firebase - 在 React Native 中从 Firestore 文档中读取和显示数据
- c# - SQL SERVER 上的 SqlBulkCopy 标识
- python - 如何修复 Python 文件中的 ModuleNotFoundError
- javascript - 如何使用钩子定期触发功能,当满足某些条件时,我想清除时间间隔?
- java - 用于 Apache Cassandra 安装的 DataStax Bulk Loader 1.7.0 在 Ubuntu 上不起作用
- scala - 日期列查询的 InvalidQueryException:无法从:'1601856000000' 生成无符号整数(用于日期)