From 0b3280d285f1883efc838d92fdd61b60663e1b46 Mon Sep 17 00:00:00 2001 From: Azuma Kasumi Date: Tue, 1 Nov 2022 11:37:13 +0900 Subject: [PATCH 1/2] =?UTF-8?q?sessionId=E5=8F=96=E5=BE=97=E3=82=92?= =?UTF-8?q?=E9=87=8D=E8=A4=87=E3=81=95=E3=81=9B=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agileworks/model/SessionRepository.kt | 2 +- .../view/ui/documentweb/DocumentWebClient.kt | 27 +----- .../ui/documentweb/DocumentWebFragment.kt | 95 +++++++++++++------ .../ui/documentweb/DocumentWebPresenter.kt | 5 + 4 files changed, 74 insertions(+), 55 deletions(-) diff --git a/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt b/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt index c2978b90..59517d66 100644 --- a/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt +++ b/app/src/main/java/jp/atled/agileworks/model/SessionRepository.kt @@ -10,6 +10,6 @@ class SessionRepository(private val activity: Activity?) { } companion object { - fun getInstance(activity: Activity) = SessionRepository(activity) + fun getInstance(activity: Activity?) = SessionRepository(activity) } } \ No newline at end of file diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt index e5681a5b..12961370 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt @@ -5,9 +5,6 @@ import android.net.Uri import android.util.Log import android.webkit.* import jp.atled.agileworks.model.CertRepository -import jp.atled.agileworks.model.LoginRepository -import jp.atled.agileworks.model.api.ApiClient -import kotlinx.coroutines.* /** * [DocumentWebFragment] の [WebView] で用いる [WebViewClient] @@ -56,28 +53,8 @@ class DocumentWebViewClient(private val webShare: DocumentWebShare): WebViewClie } super.doUpdateVisitedHistory(view, url, isReload) - - // 非同期にセッション情報取得,cookie設定 - val job = SupervisorJob() - val scope = CoroutineScope(Dispatchers.Default + job) - scope.launch { - CookieManager.getInstance().apply { - try { - val response = ApiClient.instance.getSession().execute() - if (response.isSuccessful) { - val uri = Uri.parse(url) - response.body()!!.session_id?.apply { - val sessionId = response.body()!!.session_id - setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") - flush() - } - } - } catch (e: Exception) { - // - Log.e(TAG, e.toString(), e) - } - } - } + println("url遷移のsession!!!!!") + webShare.presenter.updateSession(url, false) // ネットワーク切断時Viewを表示する必要があるか設定する(URL変更時) url?.let { webShare.presenter.updateDisconnectViewDisplayState(it) } diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt index 16df17d6..63df9e73 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt @@ -1,6 +1,7 @@ package jp.atled.agileworks.view.ui.documentweb import android.annotation.SuppressLint +import android.app.Activity import android.app.AlertDialog import android.content.Context import android.content.Intent @@ -29,6 +30,7 @@ import kotlinx.android.synthetic.main.fragment_webview_detail.* import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import java.lang.Exception +import kotlin.concurrent.withLock import kotlin.math.absoluteValue class DocumentWebFragment : Fragment(), DocumentWebPresenter { @@ -50,6 +52,8 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { private var clearHistoryFlg = false // タブ選択時初期URLリロードするかどうか private var reloadTab = false + //tokenチェック重複防止フラグ + private var sessionflg = false // スワイプのジェスチャー。折に触れて状態設定をする必要があるのでメンバーとして持つ。 // 必要とされるタイミング次第で null のままのことがある恐れがあるので lateinit にはしていない。 @@ -408,36 +412,16 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { private fun applyCookieManager(url: String?, urlLoading: Boolean) { CookieManager.getInstance().apply { - SessionRepository.getInstance(requireActivity()).getSession() { isSuccess, response -> - setupWebView() - hideLoadingProgressBar() - setClickListeners() - if (isSuccess) { - url?.let { url -> - val uri = Uri.parse(url) - response!!.session_id?.apply { - sessionId = response!!.session_id - setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") - flush() - // セッションID保存 - AwApp.instance.applicationContext.getSharedPreferences("cookie", Context.MODE_PRIVATE) - .edit() - .putString("sessionId", sessionId) - .apply() - } - if (urlLoading) { - // 多言語対応 - response.user.displayLanguage.let { displayLanguage -> - multilingualControl.setNewLanguage(displayLanguage, webview) - } - webview?.let { - it.loadUrl(url!!) - } - } else { - webShare.presenter.updateAutoReloadState(false) - } - } + setupWebView() + hideLoadingProgressBar() + setClickListeners() + updateSession(url, urlLoading) + if (urlLoading) { + webview?.let { + it.loadUrl(url!!) } + } else { + webShare.presenter.updateAutoReloadState(false) } } } @@ -550,6 +534,59 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { return clearHistoryFlg } + //tokenチェック + override fun updateSession(url: String?, updateLanguage: Boolean) { + // 非同期にセッション情報取得,cookie設定 + val lock = java.util.concurrent.locks.ReentrantLock() + lock.withLock { + if (!sessionflg) { + sessionflg = true + + CookieManager.getInstance().apply { + var activity: Activity? = null + try { + activity = requireActivity() + } catch (e: Exception) { + activity = null + } + + SessionRepository.getInstance(activity).getSession() { isSuccess, response -> + if (isSuccess) { + url?.let { url -> + val uri = Uri.parse(url) + response!!.session_id?.apply { + sessionId = response!!.session_id + setCookie( + "https://${uri.host}", + "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None" + ) + flush() + + // セッションID保存 + AwApp.instance.applicationContext.getSharedPreferences( + "cookie", + Context.MODE_PRIVATE + ) + .edit() + .putString("sessionId", sessionId) + .apply() + } + if (updateLanguage) { + // 多言語対応 + response.user.displayLanguage.let { displayLanguage -> + multilingualControl.setNewLanguage(displayLanguage, webview) + } + + } + } + } + sessionflg = false + } + } + } + } + } + companion object { private const val TAG = "DocumentWebFragmentHandler" diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt index fa105ba2..4a734bda 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebPresenter.kt @@ -59,4 +59,9 @@ interface DocumentWebPresenter { * 閲覧履歴の削除フラグ取得 */ fun getClearHistoryFlg(): Boolean + + /** + * tokenチェック + */ + fun updateSession(url: String?, updateLanguage: Boolean) } -- GitLab From b06d62bf9038ef0e8ebc8a8933f3098936cbb33f Mon Sep 17 00:00:00 2001 From: Azuma Kasumi Date: Wed, 2 Nov 2022 16:13:07 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=8D=E8=A6=81=E3=81=AA=E6=96=87?= =?UTF-8?q?=E8=A8=80=E3=81=AE=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt index 12961370..18553106 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt @@ -53,7 +53,6 @@ class DocumentWebViewClient(private val webShare: DocumentWebShare): WebViewClie } super.doUpdateVisitedHistory(view, url, isReload) - println("url遷移のsession!!!!!") webShare.presenter.updateSession(url, false) // ネットワーク切断時Viewを表示する必要があるか設定する(URL変更時) -- GitLab