Posts

Showing posts with the label Spring

Secure Spring Boot REST API using Basic Authentication

This is the third post of my Spring Boot Blog post series. In the very first post, I talked about my experience with creating RESTFul Services using Spring Boot. Then I have expanded the sample to integrate with Swagger documentation. In this post, I am going to expand above sample with security aspect.

What is API Security

API Security is a wide area with many different definitions, meanings, and solutions. The main key terms in API security are Authorization, Authentication, Encryption, Federation, and Delegation. However, I am not going to talk about each of them here.

What is Authentication

Authentication is used to reliably determine the identity of an end user and give access to the resources based on the correctly identified user.

What is Basic Authentication

Basic Authentication is the simplest way to enforce access controling to resources. Here, the HTTP user agent provides the username and the password when making a request. The string containing the username and password separ…

Integrating Swagger with Spring Boot REST API

Image
In the last post, I talked about my experience with creating RESTFul Services using Spring Boot. When creating a REST API, proper documentation is a mandatory part of it.

What is Swagger?

Swagger(Swagger 2) is a specification for describing and documenting a REST API. It specifies the format of the REST web services including URL, Resources, methods, etc. Swagger will generate documentation from the application code and handle the rendering part as well.

In this post, I am going to integrate Swagger 2 documentation into a Spring Boot based REST web service. So I am going to use Springfox implementation to generate the swagger documentation. If you want to know how to run/build Spring Boot project, please refer my previous post.

Springfox provides two dependencies to generate API Doc and Swagger UI. If you are not expecting to integrate Swagger UI into your API level, no need to add  Swagger UI dependency.

<dependency>
    <groupId>io.springfox</groupId>
    <artifac…

Building a RESTFul Service using Spring Boot

Image
Everyone is talking about Microservices such as WSO2 Microservice Framework, Spring Boot, etc. Since I haven't worked on any Spring related project since a very long time, I thought to implement a simple RESTFul service using Spring Boot.

So I started with Spring documentation. It is straightforward.  You can create the structure of your project using "Spring Initializr". This is an online tool where you can add all the desired dependencies to your project POM file. Since I am a big fan of Maven, I am generating a maven project.

In the Spring Initializr UI, you can choose the Language, Spring Boot Version, Project Group ID, artifact name, etc. Please refer below screenshot for information I have provided while generating the project.



When clicking on "Generate Project", it will download zipped maven project into your computer. Unzip it and import into an IDE. The initial project structure is like below.



In my HelloWorld REST service implementation, it accepts u…

Google Cloud SQL with Struts, Spring and Hibernate

With Google App engine SDK 1.5.5, Google have introduced Google Cloud SQL for GAE (Currently we can't connect any other external application with Google Cloud SQL. This can be only with Google App Engine Apps).

It's faster than connecting with Amazon RDS and other Cloud based Data solutions. No need to worry about load balancing as well. Cloud SQL supports 5 QPS read/write rate, so as long as you don't go over that, you can use cloud SQL. If you are going over this read/write rate limit, datastore might become a much more effective approach to you.If you are storing large blobs data(images, attchments and sound files, it's better storing them in the datastore or blobstore.

When Google App Engine became populer in late 2009 many people asked about Hibernate support. But with their datasore people were unable to develop Hibernate support applications with GAE. But with Google Cloud SQL developers can use Hibernate supported applications as well.

Spring-Hibernate Maven project- Part 3

Image
Hibernate Configuration and Project Deployment


In the previous post I created the hibernate mapping files for the Person-Address domain classes. In here we are going to create the hibernate configuration file which creates the database connection according to the defined URL, credentials and other properties.

On the deplyment of the project, the hibernate configuration file is executed through the commands in pom.xml.

We have to put the hibernate configuration file inside the src/main/resources directory. For that right cick on the directory, select new, select other. From there expand the hibernate icon and select Hibernate Configuration file (cfg.xml).

Click Next

Keep the default name "hibernate.cfg.xml" same and click Next
Give the values in the appeared window give the values as follows.

Make sure to verify your mysql username and password, and enter the corret one according to your mysql configuration.
hibernate.cfg.xml
<hibernate-configuration> <session-fa…

Spring-Hibernate Maven project- Part 2

Image
Hibernate Mapping

Here the two domain classes have one-to-many relationship. One Person can have many Addresses while one Adress can have only one person. So there is a one-to-many relationship between Person and Address.

In hibernate we have to create the mapping file for each domain class. The mapping file represents the database table which is mapped with the domain class. The table will be created according to things indicated inside mapping classes.

Mapping file for Person.java: - Person.hbm.xml
Mapping file for Address.java: - Address.hbm.xml

Mapping files are located inside src/main/resources. If you have configured Hibernate to be used inside Eclipse you can simply use it. Otherwise let's configure eclpse for hibrenate.

Hibernate-Eclipse Configuration 

Download Hibernate tools from here.
Extract the HibernateTools-3.X.zip file and put all the files inside the features folder into the features folder of the eclipse installation directory and put all the files inside the plu…

Spring-Hibernate Maven project- Part 1

Image
Here the maven project created in last post, will be extended to add new Persons in to system who has multiple Addresses.

Add the Spring Hibernate dependencies into pom.xml file inside project tags, after the version tag.
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.test.testapp</groupId> <artifactId>testapp</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.3.1.GA</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>${springVer…