0Pricing
Clojure Functional Programming & JVM Backend Development · Lekcja

Uwierzytelnianie i autoryzacja

Zabezpieczaj backend Clojure za pomocą uwierzytelniania opartego na tokenach i autoryzacji opartej na rolach, wykorzystując middleware Ring.

Uwierzytelnianie i autoryzacja to bezpłatna lekcja Clojure Functional Programming & JVM Backend Development na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Clojure Functional Programming & JVM Backend Development, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Uwierzytelnianie i autoryzacja” jest bezpłatna?

Tak — pełny tekst „Uwierzytelnianie i autoryzacja” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Clojure Functional Programming & JVM Backend Development, przejdź na CoddyKit PRO. Kurs Clojure Functional Programming & JVM Backend Development zawiera 4 lekcji w sumie.

Co nauczysz się w „Uwierzytelnianie i autoryzacja”?

Zabezpieczaj backend Clojure za pomocą uwierzytelniania opartego na tokenach i autoryzacji opartej na rolach, wykorzystując middleware Ring. Ćwiczysz Clojure Functional Programming & JVM Backend Development z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Clojure Functional Programming & JVM Backend Development?

Nie wymagamy żadnego doświadczenia. Clojure Functional Programming & JVM Backend Development w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Uwierzytelnianie i autoryzacja”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Clojure Functional Programming & JVM Backend Development?

Tak. Każda lekcja Clojure Functional Programming & JVM Backend Development zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Budowanie RESTful API
  2. Architektury sterowane zdarzeniami
  3. Projektowanie systemów i wzorce skalowalności
  4. Uwierzytelnianie i autoryzacja
← Powrót do Clojure Functional Programming & JVM Backend Development