Powered by Blogger.

How to receive a JWT-based access token from the client, verify the token, and push data using the client's URL?

1 comment :
JWT OAuth2 Integration and JSON Payload Handling in Java: JWT (JSON Web Token) is a token format used in OAuth2.
A JWT is:
  • Stateless
  • Digitally signed
  • Self-contained
Integrating with client APIs using APIs and JSON-based payloads involves an end-to-end process that includes making API calls with JSON payloads and pushing master/other data to REST API endpoints.

Step 1: Obtain the authentication details/Requirements from the client as shown in the example below.

############_MK_TOKEN_CLIENT_SECRET_#################### 

TOKEN_URL=https://a143mk-uat.client.com/api/v1/client/authentication/generate_token CLIENT_ID=AA143Mkbolgs  
CLIENT_SECRET=testa143mk

 ####Example of Configure URL ############# 
CUST_URL=https://a143mk-uat.client.com/api/master/push_customer 
CUST_USER_PASS_AUTH=useradmin:password //if it is required or not


Step 2: Making an API call with an Json payload and pushing master data to the location.
  1. A REST API Controller is responsible for handling HTTP requests (such as GET, POST, PUT, DELETE) from clients and routing them to the appropriate service methods. It acts as the entry point for external clients to interact with the backend application. 
  2. Purpose: The main role of the controller is to manage incoming HTTP requests, map them to specific methods (often called endpoints), and return appropriate responses to the client. 
  3. Responsibilities: Handle HTTP requests (e.g., GET, POST, PUT, DELETE). Map the incoming requests to service methods. Return HTTP responses, often in JSON or Json format. Perform basic request validation or authentication. Act as the "interface" between the client and the service layer. In a Spring Boot application, for example, the controller is typically annotated with @RestController and the request mappings are handled using annotations like @GetMapping, @PostMapping, etc. 
Example "ClientApiController.java" of a REST Controller in Spring Boot with handlined Json Payload :
 
 
The Service class is a part of the business logic layer of your application. It contains methods that implement the core functionality of the application (e.g., creating, reading, updating, deleting data). The service class is called by the controller to perform operations that are not directly related to the web request itself. 
  1. Purpose: The service layer acts as a mediator between the controller and the data layer (such as a repository or database). It focuses on business logic and operations. 
  2. Responsibilities: Perform the core business logic of the application. Interact with the database (via repositories or DAOs). Provide methods for the controller to use (i.e., encapsulate business operations). Handle any necessary validation or transformation before sending data back to the controller. In a Spring Boot application, service classes are typically annotated with @Service, and they are injected into the controller using @Autowired (or constructor injection). 
Example "CustMastSystemNameService.java" of a Service Class in Spring Boot: 
 Method Internal Working ->geta143mkCustMasterFOrSYS() ->pusha143mkCustToSystemNameSystem().
 


Step 3: The authorization server issues an access token in JWT format The client sends this token in requests using Authorization: Bearer<Token>
The API validates the JWT by checking its signature, expiration, issuer, and audience See the example below for how to obtain an access token for the client: Use this for server-to-server authentication.
 Method Internal Working ->geta143mkCustMasterFOrSYS() ->pusha143mkCustToSystemNameSystem().
 

JWT-based access tokens are secure, stateless, and scalable, making them ideal for modern authentication and authorization systems.

Social Client Login with OAuth2 Spring Boot Application

2 comments :
First of all you have to take auth2 access to the social web site, we see it in the video, then we will create a services and test it and see some services we learned in the previous post, we will give their link below which includes Eureka (client-1, client-2)ApiGetway, services.



Add client and secret Id in ApiGetway application.properties  file:

spring.security.oauth2.client.registration.Oauth2_Security.client-id = Write your Client ID spring.security.oauth2.client.registration.Oauth2_Security.client-secret = Write your Secret ID

Add use Dependency in ApiGetway pom.xml  file:

Add New Package in ApiGetway-> com.javatechie.spring.zulu.api.cofig and create SecurityConfiguration.java file.


See the video step by step to run/learn past post video and current configuration post.




How to Develop Some Other Rest API How to Develop Eureka Server and Eureka Client Configuration How to Develop GateWayAPI How to get Google Token

OAuth2 Security with Spring Boot

1 comment :
Introduction
OAuth2 is a token-based security authentication and authorization framework that breaks security down into four components. These four components are 
  1.  A protected resource—This is the resource (in our case, a microservice) you want to protect and ensure that only authenticated users who have the proper authorization can access 
  2.  A resource owner—A resource owner defines what applications can call their service, which users are allowed to access the service, and what they can do with the service. Each application registered by the resource owner will be given an application name that identifies the application along with an application secret key. The combination of the application name and the secret key are part of the credentials that are passed when authenticating an OAuth2 token. 
  3.  An application—This is the application that’s going to call the service on a behalf of a user. After all, users rarely invoke a service directly. Instead, they rely on an application to do the work for them. 
  4.  OAuth2 authentication server—The OAuth2 authentication server is the intermediary between the application and the services being consumed. The OAuth2 server allows the user to authenticate themselves without having to pass their user credentials down to every service the application is going to call on behalf of the user. 


OAuth2 is a token-based security framework. A user authenticates against the OAuth2 server by providing their credentials along with the application that they’re using to access the resource. If the user’s credentials are valid, the OAuth2 server provides a token that can be presented every time a service being used by the user’s application tries to access a protected resource (the microservice). The OAuth2 specification has four types of grants: 
  •  Password 
  • Client credential 
  • Authorization code 
  • Implicit

To set up an OAuth2 authentication server, you need the following Spring Cloud dependencies in the authentication-service/pom.xml file


For Example: You have to Create Project Node Name is: "a143mk-Oauth2-Security" on STS4 Tools.

 

Pom.xml file.

Create AuthServer.java class for use Authorization Server Configuration.



The first thing to note in this listing is the @EnableAuthorizationServer annotation. This annotation tells Spring Cloud that this service will be used as an OAuth2 service and to add several REST-based endpoints that will be used in the OAuth2 authentication and authorization processes.

Like other pieces of the Spring Security framework, to set up users (and their roles), start by extending the WebSecurityConfigurerAdapter class and mark it with the @Configuration annotation.
As such, you need to provide the OAuth2 server a mechanism to authenticate users and return the user information about the authenticating user. This is done by defining two beans in your Spring WebSecurityConfigurerAdapter implementation: authenticationManagerBean() and userDetailsServiceBean(). These two beans are exposed by using the default authentication authenticationManagerBean() and userDetailsServiceBean() methods from the parent WebSecurityConfigurerAdapter class.
you have to Create ConfigWeb.java class.

Run As-> Spring Boot App for Output:


For step by step development working watch the video and learn yourself.