Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

spring hibernate encrypted password

In this section, you will learn about encrypted password in spring hibernate.


How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code

spring hibernate encrypted password

In this section, you will learn about encrypted password in spring hibernate.

In the below example, we will save the password in the encrypted format(md5).


The project structure is given below :

The jar files used are given below :

The SQL query used to create user table is given below :

CREATE TABLE `user` ( 
	`user_id` bigint(11) NOT NULL auto_increment, 
	`user_name` varchar(100) default NULL, 
	`user_email` varchar(100) default NULL, 
	`password` varchar(100) NOT NULL, 
	`gender` varchar(1) default NULL, 
	`phone_no` varchar(30) default NULL, 
	`address` text, 
	PRIMARY KEY (`user_id`) 



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""

<context:property-placeholder location="" />
<context:component-scan base-package="net.roseindia" />

<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

<bean id="jspViewResolver"
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />

<bean id="dataSource"
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />

<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<property name="hibernateProperties">
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>

<bean id="hibernateTransactionManager"
<property name="sessionFactory" ref="sessionFactory" />


package net.roseindia.Utils;

import java.math.BigInteger;

public class Encryption {
public static String encrypt(String source) {
String md5 = null;
try {
MessageDigest mdEnc = MessageDigest.getInstance("MD5"); // Encryption algorithm
mdEnc.update(source.getBytes(), 0, source.length());
md5 = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
} catch (Exception ex) {
return null;
return md5;

package net.roseindia.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "user")
public class User {

@Column(name = "user_id")
private Long userId;

@Column(name = "user_name")
private String userName;

@Column(name = "user_email")
private String userEmail;

@Column(name = "password")
private String password;

@Column(name = "gender")
private String userGender;

@Column(name = "phone_no")
private String phoneNo;

@Column(name = "address")
private String address;

public void setUserId(Long userId) {
this.userId = userId;

public Long getUserId() {
return userId;

public void setUserEmail(String userEmail) {
this.userEmail = userEmail;

public String getUserEmail() {
return userEmail;

public void setUserName(String userName) {
this.userName = userName;

public String getUserName() {
return userName;

public void setPassword(String password) {
this.password = password;

public String getPassword() {
return password;

public void setUserGender(String userGender) {
this.userGender = userGender;

public String getUserGender() {
return userGender;

public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;

public String getPhoneNo() {
return phoneNo;

public void setAddress(String address) {
this.address = address;

public String getAddress() {
return address;

package net.roseindia.dao;

import java.util.List;

import net.roseindia.model.User;

public interface LoginDao {

public List<User> validateLogin(String user, String password);
public void saveUser(User user);

package net.roseindia.dao;

import java.util.List;

import net.roseindia.model.User;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

public class LoginDaoImpl implements LoginDao{
private SessionFactory sessionFactory;

@Transactional(readOnly = true)
public List<User> validateLogin(String user, String password) {
return sessionFactory.getCurrentSession().createQuery(
"from User where userName=:user and password=:password")
.setString("user", user).setString("password",password).list();

public void saveUser(User user) {

package net.roseindia.service;

import java.util.List;

import net.roseindia.model.User;

public interface LoginService {
public List<User> validateLogin(String user,String password);
public void saveUser(User user);

package net.roseindia.service;

import java.util.List;

import net.roseindia.dao.LoginDao;
import net.roseindia.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

public class LoginServiceImpl implements LoginService{

private LoginDao loginDao;

public List<User> validateLogin(String user,String password){
return loginDao.validateLogin(user, password);

public void saveUser(User user) {


package net.roseindia.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.roseindia.Utils.Encryption;
import net.roseindia.model.User;
import net.roseindia.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

public class LoginController {
LoginService loginService;

public String ShowLogin(){
return "login";

public String LoginAuthentication(HttpServletRequest req){
String username=req.getParameter("user");
String password=req.getParameter("pass");
List<User> loginlist=loginService.validateLogin(username, Encryption.encrypt(password));
return "loginsuccess";
return "login";

public String Logout(HttpSession session){
return "login";


public String registrationform(){
return "registration";

public String registration(HttpServletRequest req){
String username=req.getParameter("UserName");
String email=req.getParameter("Email");
String password=req.getParameter("Password");
String gender=req.getParameter("gender");
String phoneNumber=req.getParameter("PhoneNumber");
String address=req.getParameter("Address");
// set userForm value to user object.
User user = new User();

return "login";



First, the welcome page will be as follows :

When you click on REGISTRATION, you will get the following page :

When click on the LOGIN, you will get the following page :

If login credential are correct, you will get the following page :

The encrypted password stored in the user table as follows :


Download Source Code


Share on Google+Share on Google+

spring hibernate encrypted password

Posted on: May 17, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: spring hibernate encrypted password  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image