session-management
Implement secure session management systems with JWT tokens, session storage, token refresh, logout handling, and CSRF protection. Use when managing user authentication state, handling token lifecycle, and securing sessions.
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o session-management.zip https://jpskill.com/download/21533.zip && unzip -o session-management.zip && rm session-management.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/21533.zip -OutFile "$d\session-management.zip"; Expand-Archive "$d\session-management.zip" -DestinationPath $d -Force; ri "$d\session-management.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
session-management.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
session-managementフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 9
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
セッション管理
目次
概要
安全なトークン処理、セッション永続化、トークン更新メカニズム、適切なログアウト手順、および異なるバックエンドフレームワーク間での CSRF 保護を備えた包括的なセッション管理システムを実装します。
使用場面
- ユーザー認証システムの実装
- セッション状態とユーザーコンテキストの管理
- JWT トークン更新サイクルの処理
- ログアウト機能の実装
- CSRF 攻撃からの保護
- セッションの有効期限とクリーンアップの管理
クイックスタート
最小限の動作例:
# Python/Flask Example
from flask import current_app
from datetime import datetime, timedelta
import jwt
import os
class TokenManager:
def __init__(self, secret_key=None):
self.secret_key = secret_key or os.getenv('JWT_SECRET')
self.algorithm = 'HS256'
self.access_token_expires_hours = 1
self.refresh_token_expires_days = 7
def generate_tokens(self, user_id, email, role='user'):
"""Generate both access and refresh tokens"""
now = datetime.utcnow()
# Access token
access_payload = {
'user_id': user_id,
'email': email,
'role': role,
'type': 'access',
'iat': now,
'exp': now + timedelta(hours=self.access_token_expires_hours)
// ... (see reference guides for full implementation)
リファレンスガイド
references/ ディレクトリ内の詳細な実装:
| ガイド | 内容 |
|---|---|
| JWT Token Generation and Validation | JWT トークンの生成と検証 |
| Node.js/Express JWT Implementation | Node.js/Express JWT の実装 |
| Session Storage with Redis | Redis を使用したセッションストレージ |
| CSRF Protection | CSRF 保護 |
| Session Middleware Chain | セッションミドルウェアチェーン |
| Token Refresh Endpoint | トークン更新エンドポイント |
| Session Cleanup and Maintenance | セッションのクリーンアップとメンテナンス |
ベストプラクティス
✅ 実施すべきこと
- すべてのセッション送信に HTTPS を使用する
- セキュアな Cookie (httpOnly、sameSite、secure フラグ) を実装する
- 適切な有効期限を持つ JWT を使用する
- トークン更新メカニズムを実装する
- リフレッシュトークンを安全に保存する
- すべてのリクエストでトークンを検証する
- 強力な秘密鍵を使用する
- セッションタイムアウトを実装する
- 認証イベントをログに記録する
- ログアウト時にセッションデータをクリアする
- 状態変更リクエストに CSRF トークンを使用する
❌ 実施すべきでないこと
- トークンに機密データを保存する
- 短い秘密鍵を使用する
- URL でトークンを送信する
- トークンの有効期限を無視する
- 環境間でトークンの秘密鍵を再利用する
- localStorage にトークンを保存する (httpOnly Cookie を使用する)
- HTTPS なしでセッションを実装する
- トークン署名の検証を忘れる
- ログにセッション ID を公開する
- 予測可能なセッション ID を使用する
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
Session Management
Table of Contents
Overview
Implement comprehensive session management systems with secure token handling, session persistence, token refresh mechanisms, proper logout procedures, and CSRF protection across different backend frameworks.
When to Use
- Implementing user authentication systems
- Managing session state and user context
- Handling JWT token refresh cycles
- Implementing logout functionality
- Protecting against CSRF attacks
- Managing session expiration and cleanup
Quick Start
Minimal working example:
# Python/Flask Example
from flask import current_app
from datetime import datetime, timedelta
import jwt
import os
class TokenManager:
def __init__(self, secret_key=None):
self.secret_key = secret_key or os.getenv('JWT_SECRET')
self.algorithm = 'HS256'
self.access_token_expires_hours = 1
self.refresh_token_expires_days = 7
def generate_tokens(self, user_id, email, role='user'):
"""Generate both access and refresh tokens"""
now = datetime.utcnow()
# Access token
access_payload = {
'user_id': user_id,
'email': email,
'role': role,
'type': 'access',
'iat': now,
'exp': now + timedelta(hours=self.access_token_expires_hours)
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| JWT Token Generation and Validation | JWT Token Generation and Validation |
| Node.js/Express JWT Implementation | Node.js/Express JWT Implementation |
| Session Storage with Redis | Session Storage with Redis |
| CSRF Protection | CSRF Protection |
| Session Middleware Chain | Session Middleware Chain |
| Token Refresh Endpoint | Token Refresh Endpoint |
| Session Cleanup and Maintenance | Session Cleanup and Maintenance |
Best Practices
✅ DO
- Use HTTPS for all session transmission
- Implement secure cookies (httpOnly, sameSite, secure flags)
- Use JWT with proper expiration times
- Implement token refresh mechanism
- Store refresh tokens securely
- Validate tokens on every request
- Use strong secret keys
- Implement session timeout
- Log authentication events
- Clear session data on logout
- Use CSRF tokens for state-changing requests
❌ DON'T
- Store sensitive data in tokens
- Use short secret keys
- Transmit tokens in URLs
- Ignore token expiration
- Reuse token secrets across environments
- Store tokens in localStorage (use httpOnly cookies)
- Implement session without HTTPS
- Forget to validate token signatures
- Expose session IDs in logs
- Use predictable session IDs
同梱ファイル
※ ZIPに含まれるファイル一覧。`SKILL.md` 本体に加え、参考資料・サンプル・スクリプトが入っている場合があります。
- 📄 SKILL.md (3,481 bytes)
- 📎 references/csrf-protection.md (1,052 bytes)
- 📎 references/jwt-token-generation-and-validation.md (2,486 bytes)
- 📎 references/nodejsexpress-jwt-implementation.md (3,033 bytes)
- 📎 references/session-cleanup-and-maintenance.md (1,483 bytes)
- 📎 references/session-middleware-chain.md (1,875 bytes)
- 📎 references/session-storage-with-redis.md (2,880 bytes)
- 📎 references/token-refresh-endpoint.md (890 bytes)
- 📎 scripts/security-checklist.sh (734 bytes)