0Pricing
Clojure Functional Programming & JVM Backend Development · レッスン

認証と認可

Ringミドルウェアを使ったトークンベース認証とロールベース認可で、Clojureバックエンドを保護します。

「認証と認可」はCoddyKit上の無料Clojure Functional Programming & JVM Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはClojure Functional Programming & JVM Backend Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Clojure Functional Programming & JVM Backend Developmentコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

AuthN vs AuthZ

Two distinct concerns:

  • Authentication (AuthN): who are you?
  • Authorization (AuthZ): what are you allowed to do?

You must verify identity before checking permissions.

Hashing Passwords

Never store plain passwords. Use a slow, salted hash like bcrypt via the buddy library.

(require '[buddy.hashers :as hashers])

(def stored (hashers/derive "secret123"))
(hashers/check "secret123" stored) ; => true

What Is a JWT?

A JSON Web Token is a signed, self-contained token holding claims (user id, roles, expiry). The server can verify it without a database lookup.

Issuing a Token

On successful login, sign a token containing the user's claims with a secret key.

(require '[buddy.sign.jwt :as jwt])

(defn make-token [user]
  (jwt/sign {:user-id (:id user) :role (:role user)}
            secret-key))

Verifying a Token

On each request, read the token from the Authorization header and verify the signature. An invalid or expired token throws.

(defn verify [token]
  (jwt/unsign token secret-key))

Authentication Middleware

Wrap handlers so the verified identity is attached to the request, or a 401 is returned.

(defn wrap-auth [handler]
  (fn [request]
    (if-let [token (bearer-token request)]
      (handler (assoc request :identity (verify token)))
      {:status 401 :body "Unauthorized"})))

Role-Based Authorization

Once identity is known, check roles before allowing an action.

(defn require-role [role handler]
  (fn [request]
    (if (= role (get-in request [:identity :role]))
      (handler request)
      {:status 403 :body "Forbidden"})))

401 vs 403

Use the right status:

  • 401 Unauthorized: not authenticated (no/invalid token)
  • 403 Forbidden: authenticated but lacks permission

Protecting Routes

Combine middleware with Compojure to guard specific endpoints while leaving public ones open.

(defroutes app
  (GET "/health" [] ok)
  (-> (GET "/admin" [] admin-page)
      (#(require-role :admin %))
      wrap-auth))

Token Expiry & Refresh

Set short expiry on access tokens to limit damage if leaked, and issue long-lived refresh tokens to get new ones without re-login.

(jwt/sign {:user-id 1 :exp (+ (now) 900)} secret-key)

Security Best Practices

Key rules:

  • Always serve auth over HTTPS
  • Store secrets in environment variables, not code
  • Hash passwords with bcrypt/argon2
  • Keep access tokens short-lived

Quick Check

Test your security knowledge.

Recap

You learned to secure a Clojure backend.

  • Hash passwords with bcrypt
  • Issue and verify JWTs for stateless auth
  • Use middleware for AuthN and role checks for AuthZ
  • Return 401 vs 403 correctly

よくある質問

「認証と認可」レッスンは無料ですか?

はい。「認証と認可」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Clojure Functional Programming & JVM Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Clojure Functional Programming & JVM Backend Developmentコースには全4レッスンが含まれています。

「認証と認可」で何を学びますか?

Ringミドルウェアを使ったトークンベース認証とロールベース認可で、Clojureバックエンドを保護します。 ブラウザで直接実行するハンズオンコードでClojure Functional Programming & JVM Backend Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Clojure Functional Programming & JVM Backend Developmentを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのClojure Functional Programming & JVM Backend Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「認証と認可」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このClojure Functional Programming & JVM Backend Developmentレッスンでコードを書いて実行できますか?

はい。すべてのClojure Functional Programming & JVM Backend Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. RESTful APIの構築
  2. イベント駆動アーキテクチャ
  3. システム設計とスケーラビリティパターン
  4. 認証と認可
← Clojure Functional Programming & JVM Backend Developmentに戻る