This project shows how to configure basic auth to secure our rest API and basic transaction on Data JPA

Overview

Basic Atuthentication Spring Boot

Data JPA, MySQL

This project shows how to configure basic auth to secure our rest API and basic transaction on Data JPA. You can use this project as a templete to create your own Spring Project using Basic Autentication, Data JPA, and SQL connector.

Security Configuration

Add the following functions to your security configuration file.
Note: to not allow the spring to save the session for each request then add the SessionCreationPolicy.STATELESS

  @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().httpBasic().authenticationEntryPoint(basicAuthEntryPoint).and().authorizeRequests().anyRequest().authenticated();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("samim")
                .password(getPasswordEncoder().encode("samimjan"))
                .authorities("ROLE_USER");
    }  

Custom Auth Entry Point

To handle your http error and generete your custom error response use the folloing function to your CustomEntryPoint class which implement AuthenticationEntryPoint

@Override
  public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authEx)
          throws IOException {
      JSONObject resultMap = new JSONObject();
      resultMap.put("success",false);
      resultMap.put("timestamp",new Date().getTime());
      resultMap.put("status","403");
      resultMap.put("error",authEx.getMessage());
      resultMap.put("path",request.getRequestURI());

      response.setContentType("application/json;charset=UTF-8");
      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

      response.getWriter().write(resultMap.toString());

  }

Don't forget to add the JSONObject library dependency in pom.xml file.
You might also like...

This sample shows how to implement two-way text chat over Bluetooth between two Android devices, using all the fundamental Bluetooth API capabilities.

This sample shows how to implement two-way text chat over Bluetooth between two Android devices, using all the fundamental Bluetooth API capabilities.

Zenitsu-Bluetooth Chat Application This sample shows how to implement two-way text chat over Bluetooth between two Android devices, using all the fund

Jan 16, 2022

Compiler that compiles our language flowg to g-code (4th semester project)

flowg FlowG is a language that greatly simplifies manual g-code programming. It is a high-level language that supports functions, for loops, if statem

Jun 15, 2022

This repository contains source code examples to support my course Spring Data JPA and Hibernate Beginner to Guru

Spring Data JPA - Spring Data JPA This repository contains source code examples to support my course Spring Data JPA and Hibernate Beginner to Guru Co

Aug 24, 2022

CobaltStrike and Google Auth twice

CobaltStrike and Google Auth twice

GoogleAuth 基于cobaltStrike的谷歌二次验证 语言:Java 目的:实现在多人联合中的安全验证 原文:https://my.oschina.net/u/4587690/blog/5161930 开发原因:很简单,原文没有源代码。也没有jar包,不能“拿来吧你”。所以就自己写算了。

Dec 20, 2022

Web-based restaurant management system with spring boot and rest API for school final-year project.

Restaurant Management System Developing this for my school as first final year project. It shall be able to handle most of the challanges that encount

Mar 10, 2022

Movie,actor & director RESTful API. Sample app with jpa, flyway and testcontainers

Movie,actor & director RESTful API. Sample app with jpa, flyway and testcontainers

spring-restful-jpa-flyway Movie,actor & director RESTful API. Sample app with jpa, flyway and testcontainers.

Dec 10, 2022

This is simple project to show how to create a basic API using Java 11 + Maven + Spring Boot + PostgrSQL + Flyway.

This is simple project to show how to create a basic API using Java 11 + Maven + Spring Boot + PostgrSQL + Flyway.

Dec 10, 2022

Spring Boot JdbcTemplate example with SQL Server: CRUD Rest API using Spring Data JDBC, Spring Web MVC

Spring Boot JdbcTemplate example with SQL Server: Build CRUD Rest API Build a Spring Boot CRUD Rest API example that uses Spring Data Jdbc to make CRU

Dec 20, 2022

An examples of creating test records in the database with Spring Boot + Spring Data + JPA usage.

Spring Boot + JPA — Clear Tests An examples of creating test records in the database with Spring Boot + Spring Data + JPA usage. Check out the article

Nov 24, 2022
Owner
Hafizullah Samim
Hafizullah Samim
Dynamically filters JPA entities with a simple query syntax. Provides JPA/Hibernate predicates and Spring Data specifications.

Spring Filter You need a way to dynamically filter entities without any effort? Just add me to your pom.xml. Your API will gain a full featured search

Turkraft 142 Dec 13, 2022
In this project, we will implement two Spring Boot Java Web application called, streamer-data-jpa and streamer-data-r2dbc.

In this project, we will implement two Spring Boot Java Web application called, streamer-data-jpa and streamer-data-r2dbc. They both will fetch 1 million of customer's data from MySQL and stream them to Kafka. The main goal is to compare the application's performance and resource utilization.

Ivan Franchin 6 Nov 2, 2022
Text to Speech Project for Spring Boot and Kotlin, Auth Server, Python with Fast API (gTTS)

TTS-App Text to Speech Project for Spring Boot Module (etc Resource, Auth Server, Python with Fast API (gTTS)) Python의 gTTS lib를 활용하여 텍스트를 음성으로 변환하는 서

Seokhyun 7 Dec 21, 2021
Spring JPA Many To Many example with Hibernate and Spring Boot CRUD Rest API - ManyToMany annotation

Spring JPA Many To Many example with Hibernate and Spring Boot CRUD Rest API - ManyToMany annotation

null 17 Dec 28, 2022
How to configure Replica Set with Embedded Mongo using Spring Boot and Flapdoodle for unit testing code that uses mongodb transactions

Spring Boot Embedded Mongo with Replica Set This project defines a basic rest service that allows users to update records of a Person (name and email)

Divyansh Shekhar Gaur 4 Nov 1, 2022
This project demonstrates reference implementation of Auth API Client

Client Application to test Auth API for OTP verification. Introduction Aadhaar “authentication” means the process wherein Aadhaar Number or Virtual ID

UIDAI 3 Oct 27, 2021
Automatic creation of simple CRUD API of Spring boot and JPA project.

fast-crud Automatic creation of simple CRUD API of Spring boot and JPA project.

JinHwanKim 18 Oct 23, 2022
Cosmic Ink is a transcript application which was built with the help of Symbl AI and At Sign platform for back-end to store our data and authenticate

Cosmic-Ink Cosmic Ink is a transcript application which was built with the help of Symbl AI and At Sign platform for back-end to store our data and au

Venu Sai Madisetti 4 Dec 1, 2022
SecureDB is an extension for Ai2 Appinventor and its distros which stores the data in the form of key and value just like TinyDB but in a more secure manner.

SecureDB SecureDB is an extension for Ai2 Appinventor and its distros which stores data for your app in a secure format locally on user's device. Expl

Akshat Developer 3 Sep 24, 2022
Served as the team leader to collaborate with another intern for a project and drew up our working schedule

Served as the team leader to collaborate with another intern for a project and drew up our working schedule, risk management plan, quality control plan, configuration management plan, etc Compiled codes for the front end and back end, composed the development documentation and explored SSM structure and MyBatis structure.

bright spark 2 Jan 17, 2022