我的示例应用程序在本地环境中工作。但是,它不能在Java8标准环境下工作。以下项目是示例应用程序项目。
https://github.com/nosix/appengine-java8-spring-oauth2
在Java8标准环境中出现以下错误:
Authentication Failed: Could not obtain access token我在Spring OAuth的源代码中添加了日志,并调查了原因。错误的原因似乎是会话数据已丢失。
它的运行方式如下:
AuthorizationCodeAccessTokenProvider::getParametersForTokenRequest中的preservedState为null。因此,抛出了InvalidRequestException。这就是错误的原因。
在OAuth2RestTemplate::acquireAccessToken中调用setPreservedState方法。此时,preservedState被设置为null。
DefaultOAuth2ClientContext实例有preservedState。在Java8标准环境中,DefaultOAuth2ClientContext实例的preservedState为空。但是,它在本地环境中不是空的。
DefaultOAuth2ClientContext实例存储在会话中。我知道它在本地环境中存储在内存中,在标准环境中存储在数据存储中。
从上面看,我猜测会话数据丢失了。
我被困在调查中了。有没有可以作为解决问题的线索的信息?
发布于 2017-10-03 15:54:11
我也有同样的问题。最后,我实现了一个自定义的Spring Session SessionRepository,如下所示:(see also this commit)
存储库类:
class MemcacheSessionRepository(private val memcacheService: MemcacheService) : SessionRepository<MemcacheSession> {
private val log = LoggerFactory.getLogger(javaClass)
private val maxInactiveIntervalInSeconds: Int = 3600
override fun createSession() = MemcacheSession().also { session ->
session.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds
log.debug("createSession() = {}", session.id)
}
override fun save(session: MemcacheSession) {
log.debug("save({}) with expiration {}", session.id, session.maxInactiveIntervalInSeconds)
memcacheService.put(session.id, session, Expiration.byDeltaSeconds(session.maxInactiveIntervalInSeconds))
}
override fun getSession(id: String): MemcacheSession? =
(memcacheService.get(id) as? MemcacheSession)?.also { session ->
session.setLastAccessedTimeToNow()
}.also { session ->
log.debug("getSession({}) = {}", id, session?.id)
}
override fun delete(id: String) {
log.debug("delete({})", id)
memcacheService.delete(id)
}
}实体类:
class MemcacheSession : ExpiringSession, Serializable {
companion object {
const val serialVersionUID: Long = 1
}
private val id: String = UUID.randomUUID().toString()
private val creationTime: Long = System.currentTimeMillis()
private var lastAccessedTime: Long = creationTime
private var maxInactiveIntervalInSeconds: Int = 3600
private val attributes: MutableMap<String, Any> = mutableMapOf()
override fun getId() = id
override fun getCreationTime() = creationTime
override fun getLastAccessedTime() = lastAccessedTime
override fun setLastAccessedTime(time: Long) {
lastAccessedTime = time
}
fun setLastAccessedTimeToNow() {
lastAccessedTime = System.currentTimeMillis()
}
override fun getMaxInactiveIntervalInSeconds() = maxInactiveIntervalInSeconds
override fun setMaxInactiveIntervalInSeconds(interval: Int) {
maxInactiveIntervalInSeconds = interval
}
override fun removeAttribute(key: String) {
attributes.remove(key)
}
override fun getAttributeNames() = attributes.keys
override fun <T> getAttribute(key: String): T? = attributes[key] as T?
override fun setAttribute(key: String, value: Any) {
attributes.put(key, value)
}
override fun isExpired() = false
}这在此时似乎工作得很好,但它只使用了Memcache,并且需要改进以实现高可用性。
https://stackoverflow.com/questions/45217234
复制相似问题