![]() |
VOOZH | about |
Usually, in a web application, we will log in by using a username(email id/login name) with a password. Securely we can do the same by using an APIKey as well. Let us see what is an APIKey. The API key is a unique identifier that authenticates requests and if several users are there, their username or email id can be joined with the current date and a secure code meant only for that project by using the md5 mechanism, we can create APIKey and can maintain in a database. Let us see the ways of creating APIKey and inserting it into the database.
MySQL table structure:
-- Sample table named users is available
CREATE TABLE `users` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`loginId` varchar(20) DEFAULT NULL,
apiKey varchar(255) DEFAULT NULL,
PRIMARY KEY (`userId`)
);
-- insert 2 records
insert into users (loginId) values ('geeka@gmail.com');
insert into users (loginId) values ('geekb@gmail.com');Now let us see the sample java program for the creation of an API key and updating into the 'users' (MySQL) table
On execution of the above program, we can see below the output We can see the API key updation below in MySQL as well.
Now let us try to login into the sample by using login id and apiKey. Main important thing is apiKey will be changed on daily basis because of the date. In order to make this happen. this java code needs to be placed as a 'CRON' job in Linux servers. We can have a sample JSP page where we should have the provision to key in loginId and apiKey.
Once the information is received it will be stored in the bean and get validated. Let's see the validation one.
LoginWithAPIKeyDao.java