<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this tutorial , we will discuss about Custom Error Message in Spring Security instead of default message of Spring Security.</p>
<p>Spring Security have predefined error message, which appears when authentication failed. The predefined error message is given below</p>
<div style="background-color: #ff99cc;">
<p>Your login attempt was not successful, try again.</p>
<p>Reason: Bad credentials</p>
</div>
<p>You can change this predefined failed authentication error message. For example you want see the following message instead of the default message as follows.</p>
<div id="ads-id" align="center"></div>
<div style="background-color: #ff99cc;">
<p>Your login attempt was not successful, try again.</p>
<p>Invalid username or password</p>
</div>
</div>
<p>You need to add the following change in the <i><b>sdnext-servlet.xml</b></i> :</p>
<pre class="highlight"><;bean id="messageSource" 
 class="org.springframework.context.support.ResourceBundleMessageSource">; 
 <;property name="basenames">; 
 <;list>; 
 <;value>;message_en<;/value>; 
 <;/list>; 
 <;/property>; 
<;/bean>; 
</pre>
<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>Spring Security stored messages in &#8220;<b>messages.properties</b>&#8221; inside &#8220;<b>spring-security-core.jar</b>&#8220;, see figure below :</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com//wp-content/uploads/2013/02/custom.png" width="600" height="516" border="0" /></div>
</div>
<p>To override it, find which key generate what error message in spring security message.properties file, and redefine it with your own properties file.</p>
<p><b>message_en.properties</b></p>
</div>
<pre class="highlight">AbstractUserDetailsAuthenticationProvider.badCredentials=Invalid username or password 
</pre>
<p>Please look the previous example &#8220;<b><a href="https://dineshonjava.com/spring-security-form-based-login-example/" target="_blank" rel="noopener">Spring Security Login Form Example</a></b>&#8221; for this and go ahead.</p>
<p>In the below example, we will going to change it with custom error message :</p>
<p>The project hierarchy and jar file used is given below :</p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com//wp-content/uploads/2013/02/custom1-1.png" border="0" /></div>
</div>
<p><b>Web.xml</b></p>
</div>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">; 
 
 <;servlet>; 
 <;servlet-name>;sdnext<;/servlet-name>; 
 <;servlet-class>;org.springframework.web.servlet.DispatcherServlet<;/servlet-class>; 
 <;load-on-startup>;1<;/load-on-startup>; 
 <;/servlet>; 
 
 <;servlet-mapping>; 
 <;servlet-name>;sdnext<;/servlet-name>; 
 <;url-pattern>;/<;/url-pattern>; 
 <;/servlet-mapping>; 
 <;listener>; 
 <;listener-class>; 
 org.springframework.web.context.ContextLoaderListener 
 <;/listener-class>; 
 <;/listener>; 
 
 <;context-param>; 
 <;param-name>;contextConfigLocation<;/param-name>; 
 <;param-value>; 
 /WEB-INF/sdnext-*.xml, 
 <;/param-value>; 
 <;/context-param>; 
 
 <;welcome-file-list>; 
 <;welcome-file>;index<;/welcome-file>; 
 <;/welcome-file-list>; 
 
 <;!-- Spring Security -->; 
 <;filter>; 
 <;filter-name>;springSecurityFilterChain<;/filter-name>; 
 <;filter-class>; 
 org.springframework.web.filter.DelegatingFilterProxy 
 <;/filter-class>; 
 <;/filter>; 
 
 <;filter-mapping>; 
 <;filter-name>;springSecurityFilterChain<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 
<;/web-app>; 
</pre>
</div>
<p><b>sdnext-servlet.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:> 
</div> 
<b>sdnext-servlet.xml</b> 
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">; 
 
 <;context:component-scan base-package="com.dineshonjava.security" />; 
 
 <;bean id="jspViewResolver" 
 class="org.springframework.web.servlet.view.InternalResourceViewResolver">; 
 <;property name="viewClass" 
 value="org.springframework.web.servlet.view.JstlView" />; 
 <;property name="prefix" value="/WEB-INF/views/" />; 
 <;property name="suffix" value=".jsp" />; 
 <;/bean>; 
 
 <;bean id="messageSource" 
 class="org.springframework.context.support.ResourceBundleMessageSource">; 
 <;property name="basenames">; 
 <;list>; 
 <;value>;message_en<;/value>; 
 <;/list>; 
 <;/property>; 
 <;/bean>; 
 
<;/beans>; 
</pre>
</div>
<p><b>LoginSecurityController.java</b></p>
<pre class="highlight">package com.dineshonjava.security.controller; 
 
import java.security.Principal; 
 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Controller 
public class LoginSecurityController { 
 
 @RequestMapping(value="/index", method = RequestMethod.GET) 
 public String executeSecurity(ModelMap model, Principal principal ) { 
 
 String name = principal.getName(); 
 model.addAttribute("author", name); 
 model.addAttribute("message", "Welcome To Login Form Based Spring Security Example!!!"); 
 return "welcome"; 
 
 } 
 
 @RequestMapping(value="/login", method = RequestMethod.GET) 
 public String login(ModelMap model) { 
 
 return "login"; 
 
 } 
 
 @RequestMapping(value="/fail2login", method = RequestMethod.GET) 
 public String loginerror(ModelMap model) { 
 
 model.addAttribute("error", "true"); 
 return "login"; 
 
 } 
 
 @RequestMapping(value="/logout", method = RequestMethod.GET) 
 public String logout(ModelMap model) { 
 
 return "login"; 
 
 } 
} 
</pre>
</div>
<p><b>login.jsp</b></p>
<pre class="highlight"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>; 
<;html>; 
<;head>; 
<;title>;Login Page For Security<;/title>; 
<;style>; 
.errorblock { 
 color: #ff0000; 
 background-color: #ffEEEE; 
 border: 3px solid #ff0000; 
 padding: 8px; 
 margin: 16px; 
} 
<;/style>; 
<;/head>; 
<;body onload='document.f.j_username.focus();'>; 
 <;h3>;Login with Username and Password (Custom Login Page with custom error message)<;/h3>; 
 
 <;c:if test="${not empty error}">; 
 <;div class="errorblock">; 
 Your login attempt was not successful, try again.<;br />; Caused : 
 ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message} 
 <;/div>; 
 <;/c:if>; 
 
 <;form name='f' action="<;c:url value='j_spring_security_check' />;" 
 method='POST'>; 
 
 <;table>; 
 <;tr>; 
 <;td>;User:<;/td>; 
 <;td>;<;input type='text' name='j_username' value=''>; 
 <;/td>; 
 <;/tr>; 
 <;tr>; 
 <;td>;Password:<;/td>; 
 <;td>;<;input type='password' name='j_password' />; 
 <;/td>; 
 <;/tr>; 
 <;tr>; 
 <;td colspan='2'>;<;input name="submit" type="submit" 
 value="submit" />; 
 <;/td>; 
 <;/tr>; 
 <;tr>; 
 <;td colspan='2'>;<;input name="reset" type="reset" />; 
 <;/td>; 
 <;/tr>; 
 <;/table>; 
 
 <;/form>; 
<;/body>; 
<;/html>; 
</pre>
</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 "http://localhost:8080/sdnext/index", 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/custom2-1.png" width="600" height="216" border="0" /></div>
<p><b>If username/password is wrong, authentication failed, display custom error messages.</b><br />
<b><br />
</b> <i><b>URL : http://localhost:8080/sdnext/fail2login</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com//wp-content/uploads/2013/02/custom3.png" width="600" height="283" border="0" /></div>
<p><i><b> </b></i></p>
</div>
<p><b>Download Source Code-</b><br />
<a href="https://sites.google.com/site/dinesh9582486434/my-forms/SpringSecurityCustomErrorMsg.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><b>SpringSecurityCustomErrorMsg.zip</b></a></p>
<p><b>References-</b><br />
<b><a 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 />
<b><a href="http://static.springsource.org/spring-security/site/" target="_blank" rel="noopener">Spring Security</a></b><br />
<b><a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/springsecurity.html" target="_blank" rel="noopener">Spring Security documentation</a></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/customize-http-403-access-denied-page/">previous</a><;<; || <a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/">index </a>|| >;>;<a href="https://dineshonjava.com/spring-security-logout-example/">next</a>>;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-security-password-hashing/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-security-logout-example/">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: '572'});
});
</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…