【问题标题】:Issue with using Spring OAuth on Java8 standard environment在 Java 8 标准环境中使用 Spring OAuth 的问题
【发布时间】:2017-12-26 07:11:41
【问题描述】:

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

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

Java8标准环境出现如下错误:

Authentication Failed: Could not obtain access token

我在 Spring OAuth 的源代码中添加了日志并调查了原因。错误的原因似乎是会话数据已丢失。

它的操作如下:

preservedStateAuthorizationCodeAccessTokenProvider::getParametersForTokenRequest 中为空。所以,InvalidRequestException 被抛出。这是错误的原因。

setPreservedState 方法在OAuth2RestTemplate::acquireAccessToken 中被调用。此时,preservedState 设置为 null。

DefaultOAuth2ClientContext 实例有preservedStateDefaultOAuth2ClientContextpreservedState 实例在 Java8 标准环境中为空。但是,它在本地环境中不为空。

DefaultOAuth2ClientContext 实例存储在会话中。我知道它存储在本地环境中的内存和标准环境中的数据存储中。

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

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

【问题讨论】:

    标签: java spring google-app-engine oauth-2.0


    【解决方案1】:

    我遇到了同样的问题。最后我实现了一个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 并且需要改进以实现高可用性。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多