首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java8标准环境中使用Spring OAuth的问题

在Java8标准环境中使用Spring OAuth的问题
EN

Stack Overflow用户
提问于 2017-07-20 22:11:37
回答 1查看 248关注 0票数 3

我的示例应用程序在本地环境中工作。但是,它不能在Java8标准环境下工作。以下项目是示例应用程序项目。

https://github.com/nosix/appengine-java8-spring-oauth2

在Java8标准环境中出现以下错误:

代码语言:javascript
复制
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实例存储在会话中。我知道它在本地环境中存储在内存中,在标准环境中存储在数据存储中。

从上面看,我猜测会话数据丢失了。

我被困在调查中了。有没有可以作为解决问题的线索的信息?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-03 15:54:11

我也有同样的问题。最后,我实现了一个自定义的Spring Session SessionRepository,如下所示:(see also this commit)

存储库类:

代码语言:javascript
复制
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)
  }
}

实体类:

代码语言:javascript
复制
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,并且需要改进以实现高可用性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45217234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档