christova

Tech Articles – (please note these posts are collated from AmigosCode, Alex Xu and many others. Full copyright to the owners of their material)

In today's world of rapidly evolving cybersecurity threats, protecting your application from unauthorized access is paramount.

Spring Security, a powerful and flexible framework, plays a critical role in securing Spring Boot applications.

Whether you're dealing with traditional username/password authentication, JWT tokens, or other custom mechanisms, Spring Security provides the necessary tools to handle authentication and authorization seamlessly.

In this blog, we'll dive into the Spring Security architecture, exploring how various components like the Security Filter ChainAuthenticationManager, and Authentication Providers work together to secure your application.

1. Client Request

  • A user or client makes a request to the application, usually to access a protected resource.
  • This request is processed by a chain of Security Filters.

2. Security Filter Chain

  • Security Filter A, B, ... N: These are different filters that apply to the incoming request, where each filter can handle specific types of security logic (like CORS, CSRF protection, and more).
  • One of these filters is responsible for authenticating the user.

3. Authentication Flow

  • UsernamePassword Authentication Token: This token represents the user's credentials (username and password) and is passed to the authentication logic.
  • The authentication logic checks if the user credentials are valid. This typically involves checking a database or other identity source.

4. AuthenticationManager / ProviderManager

  • The AuthenticationManager or ProviderManager manages the overall authentication process. It delegates the authentication request to different Authentication Providers based on the type of authentication required.

5. Authentication Providers

  • JWTAuthentication Provider: If you're using JWT (JSON Web Token) for authentication, this provider handles the verification of JWT tokens.
  • DaoAuthentication Provider: This provider handles traditional authentication using a database, checking user credentials against stored data (e.g., in a relational database).
  • Other Providers (Authentication Provider N): You can define multiple custom authentication providers if your application supports multiple methods of authentication (e.g., OAuth2, LDAP).

6. UserDetailsService

  • The UserDetailsService is responsible for loading user-specific data, typically by looking up the user’s details from a database (using the DaoAuthenticationProvider).
  • The PasswordEncoder ensures that passwords are securely encoded (hashed) before they are compared during the authentication process.

7. SecurityContext & JWT Authentication Filter

  • If the user is successfully authenticated, the SecurityContext is updated to store the user’s authentication status.
  • The JWT Authentication Filter is responsible for handling JWT tokens, ensuring that valid tokens allow access to protected resources.

8. Authentication Request/Response

  • Once the authentication is performed by the filters and providers, an Authentication Request is sent to the backend.
  • After validation, an Authentication Response is returned, which could contain the authentication token (such as a JWT), allowing the client to access secure resources in subsequent requests.

9. SecurityContextHeader

  • The SecurityContextHeader encapsulates important security information like the user’s Principal (authenticated user), Credentials (such as the password or token), and Authorities (permissions or roles).
  • These fields include:
    • getAuthorities(): Fetches the roles or permissions granted to the user.
    • getPassword()getUsername(): Standard user details.
    • isAccountNonExpired()isAccountNonLocked()isCredentialsNonExpired()isEnabled(): These are checks to ensure the user account is in good standing (not expired, locked, etc.).

#spring #SpringSecurity #security

Git Merge vs. Rebase vs. Squash Commit! What are the differences?

When we 𝐦𝐞𝐫𝐠𝐞 𝐜𝐡𝐚𝐧𝐠𝐞𝐬 from one Git branch to another, we can use ‘git merge’ or ‘git rebase’. The diagram above shows how the two commands work.

𝐆𝐢𝐭 𝐌𝐞𝐫𝐠𝐞

This creates a new commit G’ in the main branch. G’ ties the histories of both main and feature branches. Git merge is 𝐧𝐨𝐧-𝐝𝐞𝐬𝐭𝐫𝐮𝐜𝐭𝐢𝐯𝐞. Neither the main nor the feature branch is changed.

𝐆𝐢𝐭 𝐑𝐞𝐛𝐚𝐬𝐞

Git rebase moves the feature branch histories to the head of the main branch. It creates new commits E’, F’, and G’ for each commit in the feature branch.

The benefit of rebase is that it has 𝐥𝐢𝐧𝐞𝐚𝐫 𝐜𝐨𝐦𝐦𝐢𝐭 𝐡𝐢𝐬𝐭𝐨𝐫𝐲.

Rebase can be dangerous if “the golden rule of git rebase” is not followed.

𝐓𝐡𝐞 𝐆𝐨𝐥𝐝𝐞𝐧 𝐑𝐮𝐥𝐞 𝐨𝐟 𝐆𝐢𝐭 𝐑𝐞𝐛𝐚𝐬𝐞 Never use it on public branches!

#Git #git #gitmerge #gitrebase

What is k8s (Kubernetes)? k8s is a container orchestration system. It is used for container deployment and management. Its design is greatly impacted by Google’s internal system Borg. A k8s cluster consists of a set of worker machines, called nodes, that run containerized applications. Every cluster has at least one worker node. The worker node(s) host the Pods that are the components of the application workload. The control plane manages the worker nodes and the Pods in the cluster. In production environments, the control plane usually runs across multiple computers and a cluster usually runs multiple nodes, providing fault-tolerance and high availability.

Control Plane Components

1. API Server The API server talks to all the components in the k8s cluster. All the operations on pods are executed by talking to the API server.

2. Scheduler The scheduler watches the workloads on pods and assigns loads on newly created pods.

3. Controller Manager The controller manager runs the controllers, including Node Controller, Job Controller, EndpointSlice Controller, and ServiceAccount Controller.

4. etcd etcd is a key-value store used as Kubernetes' backing store for all cluster data.

Nodes

1. Pods A pod is a group of containers and is the smallest unit that k8s administers. Pods have a single IP address applied to every container within the pod.

2. Kubelet An agent that runs on each node in the cluster. It ensures containers are running in a Pod.

3. Kube Proxy kube-proxy is a network proxy that runs on each node in your cluster. It routes traffic coming into a node from the service. It forwards requests for work to the correct containers.

#kubernetes #k8s

Enter your email to subscribe to updates.