会话是在您登录时创建的。会话将处于活动状态,直到您注销(销毁会话)或时间到期。
看见
example
编辑:
Spring应用程序有一些与会话相关的重要设置。
第一个是会话创建策略(默认情况下,如果需要\u-如果与请求链接的会话已经存在,则不会销毁并再次创建)。
会话保存在cookie中-您可以点击f12检查它。
应用程序“检查”请求中是否存在cookie。当您进入登录页面时,有两种情况:
-
您没有会话->出现登录弹出窗口,您可以登录,
-
您拥有会话,因为SecurityContextHolder包含有关当前会话的信息。
它是如何工作的?
当您使用时。httpBasic(),Spring安全寄存器BasicAuthenticationFilter。在方法doFilterInternal中,您可以看到:
if (authenticationIsRequired(username)) {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(
username, tokens[1]);
authRequest.setDetails(
this.authenticationDetailsSource.buildDetails(request));
Authentication authResult = this.authenticationManager
.authenticate(authRequest);
if (debug) {
this.logger.debug("Authentication success: " + authResult);
}
SecurityContextHolder.getContext().setAuthentication(authResult);
this.rememberMeServices.loginSuccess(request, response, authResult);
onSuccessfulAuthentication(request, response, authResult);
}
首次登录成功后,将设置身份验证。
当您尝试再次登录时,AuthenticationsRequired方法返回false。为什么?
查看来源:
private boolean authenticationIsRequired(String username) {
// Only reauthenticate if username doesn't match SecurityContextHolder and user
// isn't authenticated
// (see SEC-53)
Authentication existingAuth = SecurityContextHolder.getContext()
.getAuthentication();
if (existingAuth == null || !existingAuth.isAuthenticated()) {
return true;
}
// Limit username comparison to providers which use usernames (ie
// UsernamePasswordAuthenticationToken)
// (see SEC-348)
if (existingAuth instanceof UsernamePasswordAuthenticationToken
&& !existingAuth.getName().equals(username)) {
return true;
}
// Handle unusual condition where an AnonymousAuthenticationToken is already
// present
// This shouldn't happen very often, as BasicProcessingFitler is meant to be
// earlier in the filter
// chain than AnonymousAuthenticationFilter. Nevertheless, presence of both an
// AnonymousAuthenticationToken
// together with a BASIC authentication request header should indicate
// reauthentication using the
// BASIC protocol is desirable. This behaviour is also consistent with that
// provided by form and digest,
// both of which force re-authentication if the respective header is detected (and
// in doing so replace
// any existing AnonymousAuthenticationToken). See SEC-610.
if (existingAuth instanceof AnonymousAuthenticationToken) {
return true;
}
return false;
}
正如您所看到的,在前一个请求中设置的SecurityContextHolder返回对象上调用了getAuthhentication。
对不起,我英语不好。
更新:您可以使用“/注销”url使会话无效。