Security techniques protect data and communication from attackers, for example with TLS, hashing, encryption and input validation. Access control decides what an already identified user or service may do, for example with roles, permissions and middleware checks. Go ships the building blocks for the first group in its standard library (crypto/*, net/http), while access control is logic you write yourself or take from a library.
A secure Go service needs both. Authentication proves who the caller is, authorization decides what that caller can do, and encryption stops outsiders from reading or changing the traffic in between.
| Concern | Security techniques | Access control |
|---|---|---|
| Question answered | Is the data and channel safe from attackers? | Is this caller allowed to do this action? |
| Typical tools | TLS, hashing, encryption, input validation | Roles, permissions, policies, middleware |
| Go packages | crypto/tls, crypto/aes, crypto/rand, html/template, database/sql | Your own code, or libraries such as Casbin |
| Failure example | Passwords stored in plain text | A normal user can open an admin endpoint |
| HTTP status on failure | Connection rejected, or 400 for bad input | 401 Unauthorized or 403 Forbidden |
Serve HTTPS with http.ListenAndServeTLS or put the service behind a TLS-terminating proxy. A TLS configuration should set a modern minimum version.
On the client side, never set InsecureSkipVerify: true outside of throwaway tests. It disables certificate validation and makes man-in-the-middle attacks trivial. If you need a private certificate authority, add it to a x509.CertPool and set RootCAs.
Never store passwords directly and never use plain SHA-256 for them. Use bcrypt, scrypt or Argon2 from golang.org/x/crypto.
Use crypto/rand for tokens, session identifiers and keys. The math/rand packages are predictable and unsuitable for security.
db.QueryContext(ctx, "SELECT ... WHERE id = $1", id)) and never build SQL with string concatenation.html/template, not text/template, for HTML so output is escaped automatically.http.MaxBytesReader and set timeouts on http.Server.govulncheck ./... to find known vulnerabilities in the modules you use.Access control has two steps. Authentication establishes the identity, and authorization checks the permission.
The old github.com/dgrijalva/jwt-go library is archived and has known issues. The maintained fork is github.com/golang-jwt/jwt/v5.
Always restrict the accepted signing methods with jwt.WithValidMethods, keep access tokens short-lived, and load the key from a secret manager rather than source code.
Role-based access control (RBAC) maps roles to permissions. A middleware that checks the role keeps the rule in one place instead of scattered across handlers.
Return 401 when the caller is not authenticated and 403 when the caller is known but not allowed. Also check ownership inside the handler: a user who may read orders still must not read another user's order.
When rules grow beyond a few roles, consider attribute-based access control or a policy engine. Casbin and Open Policy Agent both have Go support. They keep rules as data, so you can change policy without redeploying.
Authentication verifies who the caller is, for example by checking a password or token. Authorization checks what that identity may do, for example whether it may delete a record.
Use github.com/golang-jwt/jwt/v5. The older dgrijalva/jwt-go package is archived and unmaintained.
Only in local tests against a certificate you control. In production it removes the protection TLS is meant to give, so configure the correct root certificates instead.
No. Use the standard library's crypto packages and well-reviewed modules, and choose high-level APIs such as AES-GCM through cipher.NewGCM over raw primitives.
Security techniques protect data and channels with TLS, strong password hashing, secure randomness and input validation. Access control protects actions through authentication, then role or policy checks in middleware and handlers. Use both layers, return 401 and 403 correctly, and keep secrets and dependencies up to date.