<div dir="ltr" style="text-align: justify;">
<p>In Spring Security tutorial, we will discuss about Password Hashing or Encoding through SHA hashing algorithm. <b><a href="https://dineshonjava.com/spring-security-fetch-logged-in-username/" target="_blank" rel="noopener">In last Spring Security form login example</a></b>, the password is stored in clear-text, it is vulnerable to attack. In practice, recommend to hash your password before storing them. Here we will see how to use SHA hashing algorithm to hash password, and use the hashed password to perform the login authentication in Spring Security.</p>
<div id="ads-id" align="center"></div>
<p>Spring Security supports following hashing algorithms :</p>
<p> ;</p>
<ul>
<li>plaintext</li>
<li>sha</li>
<li>sha-256</li>
<li>md5</li>
<li>md4</li>
</ul>
<p>Here we will perform password hashing through <b>SHA </b>hashing algorithm. We will use this hashed password to accomplish the login authentication in Spring Security.<br />
<b><br />
</b> <b>Required Tools used for this Application:</b></p>
<ul>
<li>Spring MVC 3.0.1</li>
<li>Spring Security 3.1.0</li>
<li>STS 2.8.1.RELEASE</li>
<li>Tomcat 7</li>
<li>Jdk 1.7</li>
<li>Jacksum 1.7.0</li>
</ul>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p><b>Password Hashing:</b><br />
For password hashing, we are incorporating <i><b>Jacksum 1.7.0</b></i>, you can download it from <b><a href="http://www.jonelo.de/java/jacksum/index.html" target="_blank" rel="noopener">here</a></b>.</p>
<p>After downloading it, execute the below CMD command to generate hash value of the plain text/password,<b> </b>by using the same folder path where you download it ,as follows :</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/hashing.png" border="0" /></div>
<p>In above my password is &#8220;<b>sweetu</b>&#8221; after hashing we will get as &#8220;<b>22c27ff8a5be6260871523871ee37d0768eb02fc</b>&#8221;<br />
when it is &#8220;<b>sweety</b>&#8221; then we will get as &#8220;<b>15eabb8159c574ddb45fea23e853e18bc599ce87</b>&#8220;.</p>
<p>In original example, password is stored in clear text. As follows.</p>
<pre class="highlight"><;security:authentication-manager>; 
 <;security:authentication-provider>; 
 <;security:password-encoder hash="sha"/>; 
 <;security:user-service>; 
 <;security:user name="dineshonjava" password="sweety" authorities="ROLE_USER" />; 
 <;/security:user-service>; 
 <;/security:authentication-provider>; 
<;/security:authentication-manager>; 
</pre>
<p>Now, use &#8220;<b>jacksum</b>&#8221; to hash the password &#8220;<b>sweety</b>&#8221; with <b>SHA </b>algorithm is &#8220;<b>15eabb8159c574ddb45fea23e853e18bc599ce87</b>&#8220;.</p>
<pre class="highlight"><;security:authentication-manager>; 
 <;security:authentication-provider>; 
 <;security:password-encoder hash="sha"/>; 
 <;security:user-service>; 
 <;security:user name="dineshonjava" 
 password="15eabb8159c574ddb45fea23e853e18bc599ce87" 
 authorities="ROLE_USER" />; 
 <;/security:user-service>; 
 <;/security:authentication-provider>; 
<;/security:authentication-manager>; 
</pre>
<p>Use this hashed password in <b>sdnext-security.xml</b> as follows :</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>;<br />
<;beans xmlns="http://www.springframework.org/schema/beans"<br />
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />
 xmlns:><br />
Now rest of the code is same as the<b> <a href="https://dineshonjava.com/spring-security-custom-error-message/" target="_blank" rel="noopener">previous example</a></b> no need to discuss every file just look and run the application.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/hashing1.png" border="0" /></div>
<p><b>Running the example</b></p>
<p>Export the example as war and deploy it Tomcat 7 server. While browsing the project you will get the following screen for loging:</p>
<p><b>Access URL "<i>http://localhost:8080/sdnext/index</i>", Spring will redirect to your custom login form.</b><br />
<i><b>URL : http://localhost:8080/sdnext/login</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/custom5-1.png" width="600" height="210" border="0" /></div>
<p>If username/password is correct, then<br />
URL : <i><b>http://localhost:8080/sdnext/index</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/02/custom4-2.png" width="600" height="154" border="0" /></div>
<p><b>Download Source Code-</b><br />
<b><a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringSecurityPasswordHashing.zip?attredirects=0&;d=1" target="_blank" rel="noopener">SpringSecurityPasswordHashing.zip</a></b></p>
<p><b><b>References-</b><br style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><a style="color: #888888; text-decoration: initial;" href="https://dineshonjava.com/spring-security-form-based-login-example/" target="_blank" rel="noopener">https://dineshonjava.com/spring-security-form-based-login-example/</a></b><br style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><a style="color: #888888; text-decoration: initial;" href="http://static.springsource.org/spring-security/site/" target="_blank" rel="noopener">Spring Security</a></b><br style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;" /><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><a style="color: #888888; text-decoration: initial;" href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity.html" target="_blank" rel="noopener">Spring Security documentation</a></b></b><br />
<b><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><a href="http://en.wikipedia.org/wiki/SHA-1" target="_blank" rel="noopener">SHA-1 hashing algorithm</a> </b></b><br />
<b><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><a href="http://www.jonelo.de/java/jacksum/index.html" target="_blank" rel="noopener">Jacksum Java library</a> </b></b></p>
<p><b><b style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: #222222; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"> </b> </b></p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring Security Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-security-interview-questions-and-answers/"><span style="color: red;">Spring Security Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-java-based-configuration-with-example/"><span style="color: red;">Spring Security Java Based Configuration with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-xml-namespace-configuration-example/"><span style="color: red;">Spring Security XML Namespace Configuration Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-hello-world-example/"><span style="color: red;">Spring Security XML Based Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-form-based-login-example/"><span style="color: red;">Spring Security form-based login example </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-login-form-using/"><span style="color: red;">Spring Security Login Form Based Example Using Database</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-http-basic/"><span style="color: red;">Spring Security Authentication Example Using HTTP Basic </span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-authorized-access/"><span style="color: red;"> Spring Security Authorized Access Control Example </span></a></b></li>
<li><b><a href="https://dineshonjava.com/customize-http-403-access-denied-page/"><span style="color: red;">Spring Security Customized Access Denied Page</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-custom-error-message/"><span style="color: red;">Spring Security Custom Error Message</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-logout-example/"><span style="color: red;"> Spring Security Logout Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-fetch-logged-in-username/"><span style="color: red;">Spring Security Fetch Logged in Username</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-password-hashing/"><span style="color: red;">Spring Security Password Hashing</span></a></b></li>
</ul>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: #ff99cc;"> <b> <;<;<a href="https://dineshonjava.com/spring-security-fetch-logged-in-username/">previous</a><;<; || <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">index </a>|| >;>;next>;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/customize-http-403-access-denied-page/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-security-custom-error-message/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '573'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…