SPRING BOOT – MONGODB – GEOSPATIAL

Description

Simple project geospatial using java spring boot and mongodb

Prerequisites

  • Java JDK 11
  • MongoDB Database
  • Maven

Installation

Step for installation:

  • Create Database in MongoDB : restaurantDB
  • Create 2dsphere indexes support queries that calculate geometries on an earth-like sphere.
db.restaurantDB.createIndex({location:"2dsphere"});
  • Clone project from github
# Clone this project from gitlab
git@github.com:saptarga/spring-boot-mongodb-geospatial.git

# Clears the target directory and builds the project
mvn clean install

Run Project

You can start this project using

mvn clean spring-boot:run

Example Request Rest API

Request Add Restaurant

POST /restaurant HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 312

{
    "name": "Restauran Ayam Bakar Spesial",
    "address": "Jl. Asia Afrika No.8, RT.1/RW.3, Senayan, Kecamatan Tanah Abang, Kota Jakarta Pusat, Daerah Khusus Ibukota Jakarta 10270",
    "rate": "4.5",
    "latitude": -6.2288667,
    "longitude": 106.7845697
}

Request Get Restaurant Nearby Location

GET /restaurant/nearby?latitude=-6.907329&longitude=107.603198&distance=5 HTTP/1.1
Host: localhost:8080

Author

Created and maintained by saptarga (@saptarga).

SPRING BOOT – RESTFUL API – JWT AUTHENTICATION

Description

Simple project restful api using Spring Boot and JWT Authentication

Prerequisites

  • Java JDK 11
  • Database PostgreSql
  • Maven

Instalation

Step for installation:

# Clone this project from gitlab
git clone git@github.com:saptarga/spring-boot-rest-api.git

# Clears the target directory and builds the project
mvn clean install

Project Structure

.                                        # main directory project 
+-- java
|   +-- dto                             # Define A Data Transfer Object
|   +-- endpoint                        # Rest controllers that handle request/responses
|   +-- entity                          # Define domain models or entities
|   +-- exception                       # Define exception handle
|   +-- repository                      # Talks to data source directly, has operations commonly known as CRUD. It could be simple jdbc, JPA, or even file access
|   +-- security                        # Configuration for security
|   +-- service                         # Business logic abstractions, this layer has no idea how to communicate with datasource.
|   +-- statval                         # Define static value, such as enum variable or constanta variable
|   +-- MainClass.java                  # App starting point
+-- resources 
|   +-- db.migration                    # Db migration script
|   +-- application.properties          # Configurations files               

Configuration

Step for configuration:

  • Create new database in postgresql with database name rest_api.
  • Set database name, user, and password in application.properties.
  • For db migration and seeding data will be created automatically when application running for first time.

Run Project

You can start this project using

mvn clean spring-boot:run

Docker Installation

This project is support with docker. If you want to run this project using docker, you can run start.sh script. And for stop this project you can run stop.sh script.

Example Request Rest API

Login User

Request


POST /v1/auth/login HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 62

{
    "username": "sapta_arga",
    "password": "123456789"
}

Response

{
  "token": {{authToken}},
  "type": null,
  "id": 2,
  "username": "sapta_arga",
  "email": "sapta_arga@gmail.com",
  "roles": [
    "ROLE_USER"
  ]
}

Deposit Money

Request

POST /v1/user-balance/deposit-money HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 93
Authorization: {{authToken}}

{
    "amount": 1000000,
    "bankCode": "3322030300020244"
}

Response

{
  "username": "sapta_arga",
  "activity": "DEPOSIT_MONEY",
  "balance": 2050000,
  "depositMoney": 1000000
}

Transfer Money

Request

POST /v1/user-balance/transfer-money HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 93
Authorization: {{authToken}}

{
    "transferTo" : "dwi_asih",
    "amount": 1000000,
    "bankCodeTo": "3322030300020223",
    "bankCodeFrom" : "3322030300020244"
}

Response

{
  "transferFrom": "sapta_arga",
  "transferTo": "dwi_asih",
  "amount": 1000000
}

Logout User

Request

POST /v1/auth/logout HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Authorization: {{authToken}}

Response

Logout Success

Example Error Response API

Error message if you don’t login for access API

{
  "timestamp": "2021-08-04T08:03:06.024+00:00",
  "status": 401,
  "error": "Unauthorized",
  "path": "/v1/user-balance/deposit-money"
}

Error message if you access API with inappropriate roles

{
  "timestamp": "2021-08-04T08:08:57.309+00:00",
  "status": 403,
  "error": "Forbidden",
  "path": "/v1/user-balance/deposit-money"
}

Client Http

The example above can you try in client.http

Download Source Code

(SOURCE CODE)

Author

Created and maintained by saptarga (@saptarga).

Spring Boot OAuth 2.0 Authentication

Descriptions

Simple project spring boot using OAuth 2.0 Authentication And access token using JWT

Prerequisites

  • Java JDK 8
  • Database PostgreSQL
  • Maven

Installation

Step for installation:

# Clone this project from gitlab
git clone https://github.com/saptarga/spring-boot-oauth-authentication.git

# Clears the target directory and builds the project
mvn clean install

Configuration

Step for configuration:

  • Create new database in postgresql with database name demo_oauth.
  • Set database name, user, and password in application.properties.
  • For db migration and seeding data will be created automatically when application running for first time.
  • If you want to add new oauth_client_details, you can generate password on https://www.browserling.com/tools/bcrypt

Run Project

You can start this project using

mvn clean spring-boot:run

Example Request Rest API

Generate Password With Grant Type: password

Request

POST /oauth/token?username=user001&password=teststaff&grant_type=password HTTP/1.1
Host: localhost:8080
Authorization: Basic bW9iaWxlYXBwOmFiY2Q=

Generate Token With Grant Type: client_credentials

Request

POST /oauth/token?grant_type=client_credentials HTTP/1.1
Host: localhost:8080
Authorization: Basic Y2wtYXBwOjEyMzQ1Njc4OQ==

Get Data Current User

Request

GET /api/user HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Accept: application/json
Authorization: {{authToken}}

Client Http

The example above can you try in Demo OAuth Client.http

Author

Created and maintained by saptarga (@saptarga).

Sepring-Security Using JWT Authtentication

DESCRIPTION

Simple project Spring-Boot and spring-security using JWT Authentication.

Instalation

Step for installation:

# Clone this project from github
https://github.com/saptarga/spring-boot-basic-authentication.git

# Clears the target directory and builds the project
mvn clean install

Configuration

Step for configuration:

  • Create new database in postgresql with database name demo_jwt_auth
  • Set database name, user, and password in application-properties
  • Create table sec_roles, sec_users, and sec_user_roles
    create table sec_roles(
        id bigserial primary key,
        name varchar
    );

    create table sec_users(
        id bigserial primary key,
        username varchar UNIQUE,
        email varchar UNIQUE,
        password varchar
    );

    create table sec_user_roles(
        id bigserial primary key,
        user_id int8,
        role_id int8,
        CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES sec_users(id),
        CONSTRAINT fk_role FOREIGN KEY(role_id) REFERENCES sec_roles(id)
    );
  • Seeding data for table sec_roles
    INSERT INTO sec_roles(name) VALUES('ROLE_USER');
    INSERT INTO sec_roles(name) VALUES('ROLE_ADMIN');
  • set app.jwtSecret and app.jwtExpirationMs in application-properties

Run Project

You can start this project using

mvn clean spring-boot:run

Example Requests API

Register User

POST /api/auth/register-user HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 127

{
    "username": "saptarga",
    "password": "123456789",
    "email": "saptarga@gmail.com",
    "role": ["ROLE_ADMIN"]
}

Login User or Authentication User

POST /api/auth/login HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 62

{
    "username": "saptarga",
    "password": "123456789"
}

Get List Posts with Authentication

GET /posts HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Authorization: Bearer {{your_token_from_login_user}}

Author

Created and maintained by saptarga (@saptarga).

Feel free if you have question for this project.

Demo Spring Boot Security Using Basic Authentication

Spring-Security Using Basic Authentication

DESCRIPTION

Simple project Spring-Boot and spring-security using Basic Authentication.

Instalation

Step for installation:

# Clone this project from github
git clone https://github.com/saptarga/spring-boot-basic-authentication.git

# Clears the target directory and builds the project
mvn clean install

Configuration

Step for configuration:

  • Create new database in postgresql
  • Set database name, user, and password in application-properties
  • Create table sec_user
    create table sec_user(
        id bigserial primary key,
        username varchar,
        email varchar,
        full_name varchar,
        password varchar,
        app_user_role varchar
    );

Run Project

You can start this project using

mvn clean spring-boot:run

Make Requests

Register User

POST /user/register HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Content-Length: 164

{
    "username": "saptarga",
    "email": "saptarga@gmail.com",
    "fullName": "Sapta Arga",
    "password": "1234567890",
    "appUserRole": "ROLE_ADMIN"
}

Get List User

GET /user HTTP/1.1
Host: localhost:8080
Content-Type: application/json
Authorization: Basic c2FwdGFyZ2E6MTIzNDU2Nzg5MA==

Author

Created and maintained by saptarga (@saptarga).

Feel free if you have question for this project.

Download Source Code

(Download)

Membuat RESTful API Menggunakan Node.Js dan MongoDB

Node.js – RESTful API

Description

Simple RESTful API implementation on Node.js, Express.js, MongoDB and JWT Authentication.

Installation

For step instalation

# Clone this project from github
git clone https://github.com/saptarga/rest-api-node-js-mongo-db.git

# Install npm dependencies in project folder
npm install

# if nodemon's dependecy doesn't exist
npm install -g nodemon

Configuration

# create folder logs
mkdir logs

# setup database
Create New Database on MongoDb

# setup env variable on .env
DB_CONNECTION
ENV
JWT_SECRET_KEY

Run Server

You can start this service using

npm start
# alias for
nodemon app

Make Requests

Generate Token

POST /users/generateToken HTTP/1.1
Host: localhost:3000

Get Data Profile User

GET /users/profile HTTP/1.1
Host: localhost:3000
Authorization: your_bearer_token

Add New Post

POST /posts HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Content-Length: 126
Authorization: your_bearer_token

{
    "title": "This is title posts",
    "description": "This is description posts",
    "author": "saptarga",
    "email": "saptarga@gmail.com"
}

Get List All Posts

GET /posts HTTP/1.1
Host: localhost:3000
Authorization: your_bearer_token

for more complete requests, please visit client.http

Author

Created and maintained by saptarga (@saptarga)

Feel free if you have question for this project.

Source Code

(Download)

Yii2 Basic With Yii2 Advance Feature And Profil User

Pada postingan ini saya hanya ingin share aplikasi yii2 sederhana yang sudah saya buat. Pada project yii2 ini saya hanya menambahkan fitur login, register user dan reset password seperti halnya pada yii2 advance, sebenernya saya hanya memindahkan yg ada di template advance ke template basic tapi tidak semua hanya fitur login dsb yang saya masukan ke project ini. Dan ada beberapa tambahan fitur lain yaitu fitur profil user. Pada project ini saya juga membuat manajemen user untuk admin, serta halaman profil user. Pada halaman profil, user dapat merubah data profil user, password, username, sosmed, foto profil user, serta melakukan inactive pada account user.  Oh iyah untuk template sendiri saya menggunakan template Admin Lte. Berikut beberapa screen shot dari aplikasi yg saya buat.

profile user

Continue reading