对于最新的稳定版本,请使用 Spring Security 6.5.3spring-doc.cadn.net.cn

表单登录

Spring Security 支持通过 html 表单提供用户名和密码。 本节详细介绍了基于表单的身份验证在 Spring Security 中的工作原理。spring-doc.cadn.net.cn

让我们看一下基于表单的登录在 Spring Security 中是如何工作的。 首先,我们看到用户如何被重定向到登录表单。spring-doc.cadn.net.cn

登录URL身份验证入口点
图 1.重定向到登录页面

该图建立在我们的SecurityFilterChain图。spring-doc.cadn.net.cn

1号首先,用户向资源发出未经身份验证的请求/private它没有被授权。spring-doc.cadn.net.cn

2号Spring Security 的FilterSecurityInterceptor表示通过抛出AccessDeniedException.spring-doc.cadn.net.cn

3号由于用户未经过身份验证,ExceptionTranslationFilter启动启动身份验证,并发送重定向到登录页面,并配置AuthenticationEntryPoint. 在大多数情况下,AuthenticationEntryPointLoginUrlAuthenticationEntryPoint.spring-doc.cadn.net.cn

4号然后,浏览器将请求重定向到的登录页面。spring-doc.cadn.net.cn

5号应用程序中的某些内容必须呈现登录页面spring-doc.cadn.net.cn

提交用户名和密码后,UsernamePasswordAuthenticationFilter验证用户名和密码。 这UsernamePasswordAuthenticationFilterextends AbstractAuthenticationProcessingFilter,因此此图看起来应该非常相似。spring-doc.cadn.net.cn

用户名密码身份验证过滤器
图 2.验证用户名和密码

该图建立在我们的SecurityFilterChain图。spring-doc.cadn.net.cn

1号当用户提交用户名和密码时,UsernamePasswordAuthenticationFilter创建一个UsernamePasswordAuthenticationToken这是一种Authentication通过从HttpServletRequest.spring-doc.cadn.net.cn

2号接下来,UsernamePasswordAuthenticationToken被传递到AuthenticationManager待认证。 什么的细节AuthenticationManager看起来取决于用户信息的存储方式。spring-doc.cadn.net.cn

3号如果身份验证失败,则失败spring-doc.cadn.net.cn

4号如果身份验证成功,则成功spring-doc.cadn.net.cn

默认情况下,Spring Security 表单登录处于启用状态。 但是,一旦提供了任何基于 servlet 的配置,就必须显式提供基于表单的登录。 可以在下面找到最小的、显式的 Java 配置:spring-doc.cadn.net.cn

表单登录
public SecurityFilterChain filterChain(HttpSecurity http) {
	http
		.formLogin(withDefaults());
	// ...
}
<http>
	<!-- ... -->
	<form-login />
</http>
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
	http {
		formLogin { }
	}
	// ...
}

在此配置中,Spring Security 将呈现默认登录页面。 大多数生产应用程序都需要自定义登录表单。spring-doc.cadn.net.cn

以下配置演示了如何提供自定义登录表单。spring-doc.cadn.net.cn

自定义登录表单配置
public SecurityFilterChain filterChain(HttpSecurity http) {
	http
		.formLogin(form -> form
			.loginPage("/login")
			.permitAll()
		);
	// ...
}
<http>
	<!-- ... -->
	<intercept-url pattern="/login" access="permitAll" />
	<form-login login-page="/login" />
</http>
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
	http {
		formLogin {
			loginPage = "/login"
			permitAll()
		}
	}
	// ...
}

在 Spring Security 配置中指定登录页面时,您负责渲染该页面。 下面是一个 Thymeleaf 模板,它生成一个符合/login:spring-doc.cadn.net.cn

登录表格 - src/main/resources/templates/login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
	<head>
		<title>Please Log In</title>
	</head>
	<body>
		<h1>Please Log In</h1>
		<div th:if="${param.error}">
			Invalid username and password.</div>
		<div th:if="${param.logout}">
			You have been logged out.</div>
		<form th:action="@{/login}" method="post">
			<div>
			<input type="text" name="username" placeholder="Username"/>
			</div>
			<div>
			<input type="password" name="password" placeholder="Password"/>
			</div>
			<input type="submit" value="Log in" />
		</form>
	</body>
</html>

关于默认 HTML 表单有几个关键点:spring-doc.cadn.net.cn

许多用户只需要自定义登录页面。 但是,如果需要,上述所有内容都可以通过附加配置进行定制。spring-doc.cadn.net.cn

如果您使用的是 Spring MVC,您将需要一个将GET /login到我们创建的登录模板。 最小样本LoginController可以看到下面:spring-doc.cadn.net.cn

登录控制器
@Controller
class LoginController {
	@GetMapping("/login")
	String login() {
		return "login";
	}
}
@Controller
class LoginController {
    @GetMapping("/login")
    fun login(): String {
        return "login"
    }
}