SpringSecurity------Persisting Authentication(十一)

一、Persisting Authentication

当用户第一次请求受保护的资源时,客户端HTTP请求的汇总:

1、未经认证的用户请求受保护资源

GET / HTTP/1.1
Host: example.com
Cookie: SESSION=91470ce0-3f3c-455b-b7ad-079b02290f7b

HTTP/1.1 302 Found
Location: /login

2、用户提交他们的用户名和密码

POST /login HTTP/1.1
Host: example.com
Cookie: SESSION=91470ce0-3f3c-455b-b7ad-079b02290f7b

username=user&password=password&_csrf=35942e65-a172-4cd4-a1d4-d16a51147b3e```

3、在对用户进行认证时,会将用户关联一个新的会话id,以防止会话固定攻击

```xml
HTTP/1.1 302 Found
Location: /
Set-Cookie: SESSION=4c66e474-3f5a-43ed-8e48-cc1d8cb1d1c8; Path=/; HttpOnly; SameSite=Lax

4、后续请求包括会话cookie,该cookie用于在会话剩余时间对用户进行身份验证

GET / HTTP/1.1
Host: example.com
Cookie: SESSION=4c66e474-3f5a-43ed-8e48-cc1d8cb1d1c8

二、SecurityContextRepository

Spring Security使用SecurityContextRepository关联登录用户和登录之后发往服务器的请求

1、HttpSessionSecurityContextRepository

HttpSecurityContextRepository是SecurityContextRepository的默认实现,它会将SecurityContext关联到HttpSession。

2、NullSecurityContextRepository

不做任何关联处理

3、RequestAttributeSecurityContextRepository

RequestAttributeSecurityContextRepository将SecurityContext设置为一个请求参数,可以保证在一个请求发生分派时还能获取到SecurityContext。比如,客户端在认证通过之后向服务器发送一个请求,但是在后续处理请求的过程中出现异常。异常出现就意味着已经建立的SecurityContext会被清除,然后分派错误请求,错误请求分派不会创建SecurityContext。这就意味着错误页面不能使用SecurityContext,除非使用某种方式持久化SecurityContext。下面是配置使用方式:

public SecurityFilterChain filterChain(HttpSecurity http) {
	http
		// ...
		.securityContext((securityContext) -> securityContext
			.securityContextRepository(new RequestAttributeSecurityContextRepository())
		);
	return http.build();
}

三、SecurityContextPersistenceFilter

SecurityContextPersistenceFilter负责使用SecurityContextRepository处理请求之间的SecurityContext的持久化

请添加图片描述

(1)在应用程序处理后续业务之前,SecurityContextHolderFilter从SecurityContextRepository加载SecurityContext,并将其设置到SecurityContextHolder

(2)应用程序处理后续业务

(3)最后,如果SecurityContext有变动,使用SecurityContextRepository保存SecurityContext。
这就意味着当我们使用SecurityContextPersistenceFilter时,只需要配置SecurityContextHolder就能保证SecurityContext的持久化

在某些情况下,响应会在SecurityContextPersisteneFilter执行完成之前响应到客户端。例如,重定向响应会立即被响应到客户端,这就意味着步骤3中是不可能建立HttpSession的;另一种可能发生的情况是,如果客户端身份验证成功,在SecurityContextPersistenceFilter完成之前提交响应,并且客户端在SecurityContextPersistenceFilter完成之前发出第二个请求,第二个请求中可能出现错误的身份验证。

为了避免这些问题,SecurityContextPersistenceFilter封装了HttpServletRequest和HttpServletResponse来检测SecurityContext是否发生了变化,如果发生了变化,则在响应提交之前保存SecurityContext。

四、SecurityContextHolderFilter

SecurityContextHolderFilter使用SecurityContextRepository在处理请求之间加载SecurityContext
securitycontextholderfilter

(1)在应用程序处理后续业务之前,SecurityContextHolderFilter使用SecurityContextRepository加载SecurityContext,然后设置到SecurityContextHolder中

(2)应用程序处理后续业务

和SecurityContextPersistenceFilter不同,SecurityContextHolderFilter只加载SecurityContext,而不保存SecurityContext。这意味着当使用SecurityContextHolderFilter时,需要显式保存SecurityContext。 配置显示保存SecurityContext:

public SecurityFilterChain filterChain(HttpSecurity http) {
	http
		// ...
		.securityContext((securityContext) -> securityContext
			.requireExplicitSave(true)
		);
	return http.build();
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 `kubectl` 命令行工具中,你可以使用 `--help` 参数来查看命令的帮助文档,包括 YAML 文件的格式要求和示例。例如,使用 `kubectl create --help` 命令可以查看如下内容: ``` Create a resource from a file or from stdin. JSON and YAML formats are accepted. Usage: kubectl create (-f FILENAME | --filename=FILENAME) [options] kubectl create (-k DIRECTORY | --kustomize=DIRECTORY) [options] kubectl create clusterrolebinding NAME --clusterrole=ROLE [--user=USER] kubectl create clusterrole NAME --verb=verb --resource=resource.group [--resource-name=resource.name] kubectl create configmap NAME [--from-literal=key1=value1] [--from-literal=key2=value2] [--from-file=[key=]source] [--dry-run=server|client|none] [--output=wide|yaml|json] [options] kubectl create cronjob NAME --image=image [--schedule=''] --command -- [COMMAND] [args...] [options] kubectl create deployment NAME --image=image [--dry-run=server|client|none] [--output=wide|yaml|json] [options] kubectl create job NAME --image=image --command -- [COMMAND] [args...] [options] kubectl create namespace NAME [--dry-run=server|client|none] [--output=wide|yaml|json] [options] kubectl create secret generic NAME [--from-literal=key1=value1] [--from-literal=key2=value2] [--from-file=[key=]source] [--dry-run=server|client|none] [--output=wide|yaml|json] [options] kubectl create service NAME --tcp=port1,port2,... [--dry-run=server|client|none] [--output=wide|yaml|json] [options] kubectl create serviceaccount NAME [--dry-run=server|client|none] [--output=wide|yaml|json] [options] Examples: # Create a pod using the data in pod.json. kubectl create -f ./pod.json # Create a pod based on the JSON passed into stdin. cat pod.json | kubectl create -f - # Edit the data in EDITOR (default vim). kubectl create configmap my-config --from-file=config.json --edit # Create a new namespace named my-namespace kubectl create namespace my-namespace Options: -f, --filename='': Filename, directory, or URL to files to use to create the resource -k, --kustomize='': Process a kustomization directory. This flag can't be used together with -f or -R. --edit=false: Edit the data in $EDITOR --force=false: Create resource even if it already exists --dry-run='none': Must be "none", "server", or "client". If client strategy, only print the object that would be sent, without sending it. If server strategy, submit server-side request without persisting the resource. -o, --output='': Output format. One of: yaml, json, wide, name, go-template-file, go-template, jsonpath-file, jsonpath. If unspecified, will default to yaml for standalone objects and table for lists. --record=false: Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists. ``` 在这个帮助文档中,你可以看到 `kubectl create` 命令支持不同类型的资源对象,包括 Pod、Deployment、Service、Secret 等。对于每个资源对象类型,文档都提供了示例和参数说明。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豢龙先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值