我正在编写一个使用 Spring Boot 2 作为后端和 Angular6/Ionic 作为前端的小型 Web 应用程序。目的是让用户将网站添加到他们的主屏幕,并使其基本上看起来/感觉像一个原生应用程序。这工作得很好,但我想使用谷歌登录 Spring Security Oauth2。我的问题是 Spring Boot 将用户身份验证令牌保留在与会话关联的服务器上,并且每次单击该图标时,都会加载 IOS 主屏幕图标并清除所有 cookie。由于页面加载时cookie已经消失,用户需要重新登录。
显然 html5 本地存储应该从启动到启动持续存在,所以我想我需要在身份验证后为用户生成一个密钥,该密钥可以存储在设备上的本地存储中,然后当用户访问页面时它可以提供这个我可以用来在服务器上“验证”它们的密钥……类似的东西。
我正在寻找有关如何允许用户保持“登录”状态的想法,而无需可靠地存储任何时间长度的 cookie。
当前使用 Spring Boot 2 Angular 6 Ionic 4 Spring Security Spring Oauth2
除了登录页面,一切都在安全之后。
目前我正在持久化与 jdbc 的会话,我的配置如下所示:
应用程序.yml
spring.security.oauth2.client.registration.google.client-id=XXXXX
spring.security.oauth2.client.registration.google.client-secret=XXXX
server.servlet.session.persistent=true
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
MvcConfig.java
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("/static");
registry.addResourceHandler("/static/*.js", "/static/*.css", "/static/*.svg")
.addResourceLocations("/static")
.setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS));
}
}
安全配置.java
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login().and().logout().logoutSuccessUrl("/");
}
}
我已经尝试告诉 spring 将会话 ID 作为 x-auth 标头,但 google oauth 似乎停止工作。就像我进入一个页面一样,点击屏幕以使用 google 登录,使用 google 登录并返回到我的登录页面并显示错误:“您的登录尝试不成功,请重试。”
所以基本上 google oauth 可以与上面的配置一起使用,但是在添加到 SecurityConfig.java 时失败了
@Bean
public HttpSessionIdResolver httpSessionIdResolver() {
return new HeaderHttpSessionIdResolver("X-Auth-Token");
}
这显然是失败的,因为适当的会话信息没有被传递到谷歌/从谷歌传回。我的登录过程产生 3 个“会话”
1)当用户第一次尝试访问该页面并获取登录页面时 2)当令牌响应从谷歌返回时。此会话指示“authorization_request_not_found”错误 3) 当用户被重定向回登录页面时。
看起来有关会话的一些信息正在从谷歌传递到/从谷歌传回,但会话 ID 看起来正确
对谷歌身份验证的请求是:
https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=1111111111111-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&scope=openid%20profile%20email&state=NGW6kTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&redirect_uri=http://localhost.com:9733/login/oauth2/code/google
来自谷歌身份验证的回调:
http://localhost:9733/login/oauth2/code/google?state=NGW6kTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&code=4/xxxx_xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&scope=openid+email+profile+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/plus.me+https://www.googleapis.com/auth/userinfo.email&authuser=0&session_state=6ee92xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..2618&prompt=none