spring-authorization-server 认证服务器登录页面停留太久再提交登录 提示404 或则 login?error处理
现象
当使用spring-cloud-gateway 与 spring-authriztion-server做统一登录时, gateway 重定向到auth认证服务器,并在login页面停留太久时, 提交登录现实404 或login?error的处理
1、 认证服务器端配置
server:
servlet:
session:
# 单位s 给一天
timeout: 86400
延长session 超时时间后就不会出现登录后404的问题; 该问题的主要原因是session 失效后找不到访问资源的目标地址。
2、gateway 端处理
认证服务器延迟session 时间后仍会出现login?error 的错误,原因就是gateway端响应超时引起,
gateway 由于使用的是webflux 上面认证服务器的超时配置不生效,session的生成流程具体如下:
步骤一
RedisWebSessionConfiguration 创建bean sessionRepository, 通过@Import ->
步骤二
SpringWebSessionConfiguration (创建bean: WebSessionManager 并注入sessionRepository 实现为ReactiveRedisSessionRepository)
步骤三
SpringSessionWebSessionStore -> 调用ReactiveRedisSessionRepository(ReactiveMapSessionRepository)
-> createWebSession 创建session
覆盖并修改ReactiveRedisSessionRepository 如下:
@Component
public class ReactiveRedisSessionRepository implements ReactiveSessionRepository<ReactiveRedisSessionRepository.RedisSession> {
@Value("${server.servlet.session.timeout}")
private Long seconds;
...
public Mono<RedisSession> createSession() {
return Mono.defer(() -> {
MapSession cached = new MapSession();
// cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
cached.setMaxInactiveInterval(Duration.ofSeconds(seconds));
RedisSession session = new RedisSession(cached, true);
return Mono.just(session);
});
}
...
3、附 认证中心的流程
--- 登录步骤一
post 9000/login
Set-Cookie: JSESSIONID=7D11BEA8C630D073093E800B77539E9F
重定向到
http://xxx:9000/oauth2/authorize?response_type=code
&client_id=client-id&scope=read%20write%20message.read
&state=VWv2VUFPwsHeArfaJCFhX4DQjuObQ2t9hTNBtm7S9UI%3D
&redirect_uri=http://xxx:8888/login/oauth2/code/xx&continue
重定向到
http://xxx:8888/login/oauth2/code/xx?
code=b2YkfN_hTcmo0teC0_RJfU5p1bteLtbg_vChJzOtdjZtXavJikDASfgYXZWA8nTXBD94nsCi-76q0BZtVVKHNoooesiZ8mikSSh-BEBcT74AoAAoO0eAAQk2Nu80FmvR
&state=VWv2VUFPwsHeArfaJCFhX4DQjuObQ2t9hTNBtm7S9UI%3D
重定向到
8888/
相关端点都是EntryPoint结尾
AuthenticationEntryPoint
DelegatingAuthenticationEntryPoint
LoginUrlAuthenticationEntryPoint