メインコンテンツまでスキップ
バージョン: 20 R5 BETA

Webセッション

The 4D web server provides built-in features for managing web sessions. Webセッションを作成・維持することで、Webアプリケーション上のユーザーエクスペリエンスを管理・向上することができます。 Webセッションが有効かされていると、Webクライアントはリクエスト間で同じコンテキスト (セレクションや変数の値) を再利用できます。

Webセッションでは、以下のことが可能です:

  • handle multiple requests simultaneously from the same web client through an unlimited number of preemptive processes (web sessions are scalable),
  • manage session through a Session object and the Session API,
  • store and share data between processes of a web client using the .storage of the session,
  • セッションを実行しているユーザーに権限を関連付ける。

用途

Webセッションは次のものに使用されます:

Webセッションの有効化

セッション管理機能は、4D Webサーバー上で有効または無効にすることができます。 セッション管理を有効化する方法は複数あります:

  • Using the Scalable sessions option on the "Web/Options (I)" page of the Settings (permanent setting): alt-text

このオプションは、新規プロジェクトではデフォルトで選択されています。 It can however be disabled by selecting the No sessions option, in which case the web session features are disabled (no Session object is available).

  • Using the .scalableSession property of the Web Server object (to pass in the settings parameter of the .start() function). この場合、ストラクチャー設定ダイアログボックスで定義されたオプションよりも、Webサーバーオブジェクトの設定が優先されます (ディスクには保存されません)。

The WEB SET OPTION command can also set the session mode for the main Web server.

いずれの場合も、設定はマシンに対しローカルなものです。つまり、4D Server の Webサーバーと、リモートの 4Dマシンの Webサーバーで異なる設定が可能です。

Compatibility: A Legacy sessions option is available in projects created with a 4D version prior to 4D v18 R6 (for more information, please refer to the doc.4d.com web site).

セッションの実装

When sessions are enabled, automatic mechanisms are implemented, based upon a private cookie set by 4D itself: "4DSID_AppName", where AppName is the name of the application project. この cookie は、アプリケーションのカレントWebセッションを参照します。

info

The cookie name can be get using the .sessionCookieName property.

  1. In each web client request, the Web server checks for the presence and the value of the private "4DSID_AppName" cookie.

  2. cookie に値がある場合、4D は既存セッションの中からこのクッキーを作成したセッションを探し、見つかった場合には再利用します。

  3. クライアントからのリクエストが、すでに開かれているセッションに対応していない場合:

  • a new session with a private "4DSID_AppName" cookie is created on the web server
  • a new Guest Session object is created and is dedicated to the scalable web session.

Creating a web session for a REST request may require that a licence is available, see this page.

The Session object of the current session can then be accessed through the Session command in the code of any web processes.

alt-text

info

Webプロセスは通常終了せず、効率化のためにプールされリサイクルされます。 プロセスがリクエストの実行を終えると、プールに戻され、次のリクエストに対応できるようになります。 Since a web process can be reused by any session, process variables must be cleared by your code at the end of its execution (using CLEAR VARIABLE for example). このクリア処理は、開かれたファイルへの参照など、プロセスに関連するすべての情報に対して必要です。 This is the reason why it is recommended to use the Session object when you want to keep session related information.

セッション情報の保存と共有

Each Session object provides a .storage property which is a shared object. このプロパティにより、セッションで処理されるすべてのプロセス間で情報を共有することができます。

セッションの有効期限

スケーラブルWebセッションは、以下の場合に閉じられます:

  • Webサーバーが停止したとき。
  • セッションcookie がタイムアウトしたとき。

非アクティブな cookie の有効期限は、デフォルトでは 60分です。つまり、Webサーバーは、非アクティブなセッションを 60分後に自動的に閉じます。

This timeout can be set using the .idleTimeout property of the Session object (the timeout cannot be less than 60 minutes) or the connectionInfo parameter of the Open datastore command.

When a web session is closed, if the Session command is called afterwards:

  • the Session object does not contain privileges (it is a Guest session)
  • the .storage property is empty
  • 新しいセッションcookie がセッションに関連付けられています。
info

You can close a session from a Qodly form using the logout feature.

権限

Webユーザーセッションには、権限を関連付けることができます。 セッションの権限に応じて、特定のアクセスや機能を Webサーバー上で提供することができます。

You assign privileges using the .setPrivileges() function. In your code, you can check the session's privileges to allow or deny access using the .hasPrivilege() function. By default, new sessions do not have any privilege: they are Guest sessions (.isGuest() function returns true).

例:

If (Session.hasPrivilege("WebAdmin"))
//Access is granted, do nothing
Else
//Display an authentication page
End if
info

権限は ORDAアーキテクチャーの中心に実装されており、データストアやデータクラス関数へのアクセスを制御するための強力な技術を開発者に提供します。 For more information, please refer to the Privileges page of the ORDA chapter.

例題

CRMアプリケーションを使って、各営業担当者が自分の顧客ポートフォリオを管理します。 データストアには、少なくとも 2つのリンクされたデータクラス Customers と SalesPersons が含まれています (営業担当者は複数の顧客を持ちます)。

alt-text

営業担当者がログインし、Webサーバー上でセッションを開き、上位3名の顧客をセッションに読み込ませたいとします。

  1. セッションを開くために以下の URL を実行します:
http://localhost:8044/authenticate.shtml

In a production environment, it it necessary to use a HTTPS connection to avoid any uncrypted information to circulate on the network.

  1. The authenticate.shtml page is a form containing userId et password input fields and sending a 4DACTION POST action:
<!DOCTYPE html>
<html>
<body bgcolor="#ffffff">
<FORM ACTION="/4DACTION/authenticate" METHOD=POST>
UserId: <INPUT TYPE=TEXT NAME=userId VALUE=""><br/>
Password: <INPUT TYPE=TEXT NAME=password VALUE=""><br/>
<INPUT TYPE=SUBMIT NAME=OK VALUE="Log In">
</FORM>
</body>
</html>

alt-text

  1. The authenticate project method looks for the userID person and validates the password against the hashed value already stored in the SalesPersons table:
var $indexUserId; $indexPassword; $userId : Integer
var $password : Text
var $userTop3; $sales; $info : Object


ARRAY TEXT($anames; 0)
ARRAY TEXT($avalues; 0)

WEB GET VARIABLES($anames; $avalues)

$indexUserId:=Find in array($anames; "userId")
$userId:=Num($avalues{$indexUserId})

$indexPassword:=Find in array($anames; "password")
$password:=$avalues{$indexPassword}

$sales:=ds.SalesPersons.query("userId = :1"; $userId).first()

If ($sales#Null)
If (Verify password hash($password; $sales.password))
$info:=New object()
$info.userName:=$sales.firstname+" "+$sales.lastname
Session.setPrivileges($info)
Use (Session.storage)
If (Session.storage.myTop3=Null)
$userTop3:=$sales.customers.orderBy("totalPurchase desc").slice(0; 3)

Session.storage.myTop3:=$userTop3
End if
End use
WEB SEND HTTP REDIRECT("/authenticationOK.shtml")
Else
WEB SEND TEXT("パスワードに誤りがあります")
End if
Else
WEB SEND TEXT("この userId は登録されていません")
End if

関連項目 (ブログ記事)

Scalable sessions for advanced web applications