ユーザー コンテキスト
最新の Web アプリケーションを構築する場合、Web クライアントからサーバー アプリケーションにデータを送信することが必要です。1 つのアプローチは HTTP ヘッダーを使用することです。これは、認証トークン (JWT トークンなど)、またはサーバー側コードの実行に必要なユーザー コンテキスト固有の情報を渡すときに非常に役立ちます。リクエスト ヘッダーを使用すると、機密情報を安全な方法で送信できるため、URL やリクエストの本文に機密情報を埋め込む必要がなくなります。
Reveal では、ユーザー コンテキストは、アプリケーションの認証されたユーザーの ID と、特定のユーザーのコンテキストでサーバー リクエストを実行するために必要なその他の重要な情報を含めることができるオブジェクトです。ユーザー コンテキストは、IRVDashboardProvider、IRVAuthenticationProvider、IRVDataSourceProvider などの Reveal SDK プロバイダーで使用して、ユーザーが持つアクセス許可を制限または定義できます。
Reveal SDK 内のユーザー コンテキストは、IRVUserContextProvider インターフェイスと RVUserContext オブジェクトによって表されます。RVUserContext は IRVUserContext のデフォルト実装であり、現在のユーザーのユーザー ID を保存する機能と、Reveal SDK の他の領域 (前述の IRVObjectFilter、IRVDashboardProvider、IRVDataSourceProvider など) で使用できるリクエストに関連する追加のプロパティを取得して保存する機能を提供します。
UserContextProvider を実装するには、次の手順に従います:
手順 1 - ユーザー コンテキスト プロバイダーを作成します。
- ASP.NET
- Java
- Node.js
- Node.js - TS
internal class UserContextProvider : IRVUserContextProvider
{
public IRVUserContext GetUserContext(HttpContext aspnetContext)
{
//when using standard auth mechanisms, the userId can be obtained using aspnetContext.User.Identity.Name.
var userIdentityName = aspnetContext.User.Identity.Name;
var userId = (userIdentityName != null) ? userIdentityName : "guest";
var props = new Dictionary<string, object>() { { "some-property", aspnetContext.Current.Request.Cookies["some-cookie-name"].Value } };
return new RVUserContext(userId, props);
}
}
public class UserContextProvider extends RVContainerRequestAwareUserContextProvider {
@Override
protected IRVUserContext getUserContext(ContainerRequestContext requestContext) {
// this can be used to store values coming from the request.
var props = new HashMap<String, Object>();
props.put("some-property", "some-value");
return new RVUserContext("user identifier", props);
}
}
const userContextProvider = (request) => {
// this can be used to store values coming from the request.
var props = new Map();
props.set("some-property", "some-value");
return new reveal.RVUserContext("user identifier", props);
};
const userContextProvider = (request:IncomingMessage) => {
// this can be used to store values coming from the request.
var props = new Map<string, Object>();
props.set("some-property", "some-value");
return new RVUserContext("user identifier", props);
};
手順 2 - ユーザー コンテキスト プロバイダーを Reveal SDK に登録します。
- ASP.NET
- Java
- Node.js
- Node.js - TS
builder.Services.AddControllers().AddReveal( builder =>
{
builder.AddUserContextProvider<UserContextProvider>();
});
RevealEngineInitializer.initialize(new InitializeParameterBuilder().
setUserContextProvider(new UserContextProvider()).
build());
const revealOptions = {
userContextProvider: userContextProvider
};
app.use('/reveal-api/', reveal(revealOptions));
const revealOptions: RevealOptions = {
userContextProvider: userContextProvider
};
app.use('/reveal-api/', reveal(revealOptions));
これで、HTTP ヘッダー経由で渡されるコンテキスト固有の情報を取得して保存するようにサーバーが設定されました。サーバー実装を追加したら、クライアント側のコールバック setAdditionalHeadersProvider を使用します。