configure JDBCRealm JAAS for mysql and tomcat 7 with form based authentication

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all,

In this tutorial we are going to configure JDBCRealm JAAS for tomcat 7 and mysql database server.

Let us first understand what exactly these terminologies mean.

JAAS : Java Authentication and Authorization Service is used for user authentication. This provides separation of concerns for user authentication so that they are managed independently

JDBCRealm: We can say this is used to look for users in provided relational database. All the user credentials will be retrieved by tomcat using JDBCRealm.

Form based authentication: This is a mechanism by which security is provided for web resources. If the user is authenticated, then resource will be served, otherwise it will lead to a login page where user can fill in login credentials and after successful login, the resource will be served.

We will follow these steps for configuration

1. Prepare database for user credentials and roles

2. Configure tomcat 7 server for JDBCRealm with our database

3. Create a web application in eclipse

4. Configure security for the resources which we want to protect in our web application

5. Run example

1. Prepare database:

Copy paste the following sql script and run from mysql command prompt

This will create the database and add data into it.

2. Configure tomcat 7 server.xml for JDBCRealm

Add a realm tag in tomcat_home/conf/server.xml file. Place mysql-connector-java.jar in tomcat_home/lib

3. Create a dynamic web project in eclipse.

Click File -> New -> Dynamic Web Project. Name it Tomcat7FormBasedJAAS. Also place mysql-connector-java.jar in  WEB-INF/lib

Directory structure after creating project
Directory structure after creating project

4. Configure security for web application

Paste following content in web.xml

Create protected.jsp in Webcontent/protected folder and paste following code in it.

Create login.jsp as follows

Note that the names for username, password and action must be j_username, j_password and j_security_check. It means we are using JAAS

Create a page if user authentication fails. Name it login_failure.jsp

5. Run and test the application

Start tomcat 7 server and hit

Now that we are trying to access a protected file based on url pattern, the container will take us to the login page as per our configuration in web.xml

The following page should be displayed when you hit above url.

Login Page
Login Page

Now enter wrong credentials. say I enter username as prasadkharkar and password as 1234.

Now container will check whether these match the credentials specified in the database. If they don’t match it will redirect you to error page as follows

Login Error
Login Error

When you enter correct credentials..i.e. username as “prasadkharkar” and password as “password”. Then you will be successfully redirected to the protected resource that you are trying to access because now you are a authenticated user.

Successful Login
Successful Login

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

96 thoughts on “configure JDBCRealm JAAS for mysql and tomcat 7 with form based authentication

  • July 8, 2013 at 5:00 pm
    Permalink

    So simply put, the realm tag is used to connect to our DB right.?

    Reply
    • July 8, 2013 at 11:14 pm
      Permalink

      Thanks for reading the tutorial Preeti, hope you liked it.
      Simply saying, Real can be thought of as a system in which usernames and their credentials are stored. We can have a jdbc realm that uses database, a ldap or even simple file system.

      Reply
      • November 26, 2014 at 12:27 pm
        Permalink

        I am a bit confused. We set the relam in the server.xml and in the app web.xml we dont say anything about that relam. Then how will the tomcat know for this app i should contact this relam. When i access the protected page, because the cookie is not present it will get redirected to login page. once i entered the credentials, and press login, can you please elaborate what happens, who checks the data. How will that guy know the database. I know this blog is a bit old, if anyone can let me know the things i would be grateful. Thanks

        Reply
        • November 29, 2014 at 11:57 pm
          Permalink

          Hi Arjun, as far as I know, it applies to all web applications deployed under tomcat. I will try to find out more information about it and discuss. Meanwhile you can study how the process of authentication works here

          Reply
      • April 26, 2017 at 8:45 pm
        Permalink

        Hi,Nice article.
        I want to get the user name on my login error page (login_failure.jsp is your example) so that I can implement features like user lockout after consecutive failed login attempts.

        Reply
  • August 31, 2013 at 12:53 pm
    Permalink

    Here are some links to web sites that we link to for the reason that we feel they’re really worth visiting.

    Reply
  • September 11, 2013 at 9:15 am
    Permalink

    Just beneath, are many completely not connected websites to ours, having said that, they’re certainly really worth going over.

    Reply
  • Pingback:configure JAAS for jboss 7.1 and mysql | theJavaGeek

  • September 20, 2013 at 4:50 am
    Permalink

    Usually I do not learn article on blogs, but I wish to say that this write-up very forced me to take a look at and do so! Your writing style has been amazed me. Thanks, quite great article.

    Reply
  • September 30, 2013 at 11:41 am
    Permalink

    what you have done that i understood….but i have some confusion here…ie in ur login page u have given action as j_security_check,how it knows that it should forward to protected.jsp only…if i have one more page welcom.jsp and if user is authenticated means i wan forward to welcome.jsp….

    Reply
    • December 25, 2013 at 6:28 pm
      Permalink

      Its not that j_security_check will know how to go to welcome.jsp. The process goes like this.
      1. A request for welcome.jsp is made.
      2. If the user is not authenticated, he will be redirected to the login form. Here, server know the authentication is for welcome.jsp
      3. When user is authenticated using username and password, then he is redirected to welcome.jsp

      Reply
  • October 29, 2013 at 11:43 am
    Permalink

    Good article! would be nice if you add how to logout.

    thanks!

    Reply
  • December 25, 2013 at 5:34 pm
    Permalink

    Hi, i have followed this tutorial but can’t make login part to work. Actually, if i enter wrong password for valid username, i am getting redirected to login_failure.jsp. But when i enter correct username and password i am again being redirected to login_failure.jsp, and in Eclipse console i am getting message:

    Dec 25, 2013 12:49:53 PM org.apache.catalina.realm.LockOutRealm authenticate
    WARNING: An attempt was made to authenticate the locked user “prasadkharkar”

    Reply
    • December 25, 2013 at 6:29 pm
      Permalink

      It seems there is something wrong with server configuration. Can you please post your code here?

      Reply
        • December 26, 2013 at 3:33 am
          Permalink

          My apologies. I was editing wrong ‘server.xml’ file. My Tomcat server location setting is set to use Tomcat installation. So, I thought I need to edit define Realm in ‘[tomcat]/conf/server.xml’. Thats why this example wasn’t working. When I put Realm definition in ‘workspace/Servers/Tomcat/server.xml’ everything works fine.

          Reply
          • December 26, 2013 at 8:11 am
            Permalink

            I’m glad you could solve the problem 🙂

  • January 3, 2014 at 5:40 pm
    Permalink

    Very well described,got to know how to configure jaas for tomcat 7 & mysql.

    Reply
  • January 6, 2014 at 10:25 am
    Permalink

    ****************Very VEry Urgent******************************
    Am using Struts 2 as my MVC framework. My requirement is to authenticate using jdbc realm & form authentication .And am not able to redirect my successful authentication to main page.
    Am facing an error stating : HTTP Status 400 – Invalid direct reference to form login page .description The request sent by the client was syntactically incorrect (Invalid direct reference to form login page).

    Kindly provide some steps to make my application up and running.If possible could you please mail me the possible solution @ shashankfrost@gmail.com.

    Thanks&Regards
    Shanks

    Reply
  • January 28, 2014 at 3:35 pm
    Permalink

    The article posted was very informative and useful. You people are doing a great job. Keep going.

    Reply
  • January 30, 2014 at 6:06 pm
    Permalink

    Thanks for every other informative web site. The place else may just I am getting that type of information written in such an ideal method? I have a project that I’m just now running on, and I’ve been on the glance out for such info.

    Reply
  • February 4, 2014 at 8:15 am
    Permalink

    I’d like to thank you for the efforts you’ve put in writing this blog. I am hoping to see the same high-grade blog posts by you later on as well. In truth, your creative writing abilities has inspired me to get my own blog now 😉

    Reply
  • February 8, 2014 at 9:27 am
    Permalink

    I’m still learning from you, but I’m improving myself. I certainly liked reading everything that is written on your blog.Keep the posts coming. I loved it!

    Reply
  • February 11, 2014 at 12:47 pm
    Permalink

    Keep functioning ,impressive job!

    Reply
  • February 13, 2014 at 1:41 pm
    Permalink

    Peculiar article, exactly what I needed.

    Reply
  • February 20, 2014 at 5:59 pm
    Permalink

    If i use PostgreSql instead of Mysql database, what are the changes to make ?
    Thanks in advance

    Reply
    • February 20, 2014 at 10:08 pm
      Permalink

      Hello Sarra,
      Thanks for reading this tutorial. You will have postgres specific database url and you will have to create database module similar to

      This is not needed. By mistake I thought you are using jBoss and not Tomcat 7. You do not need to create database module.

      Reply
      • February 21, 2014 at 2:32 am
        Permalink

        Thanks for your reply 🙂
        I do this:

        But, it doesn’t work 🙁

        Reply
        • February 21, 2014 at 2:33 am
          Permalink

          Thanks for your reply 🙂
          I do this:

          But, it doesn’t work 🙁

          Reply
      • February 21, 2014 at 1:27 pm
        Permalink

        Hello Prasad,
        the problem is that it does not accede to the data from the database

        Reply
        • February 23, 2014 at 6:49 pm
          Permalink

          Hi Sarra, Can you please check whether you can access database from some other application? What do you see in stacktrace ?

          Reply
  • February 22, 2014 at 11:08 pm
    Permalink

    Hello, I want to think you about this tutoriel 🙂
    I have followed this tutorial with a dataBase Postgresql, but it doesn’t work, if i enter a wrong password for a valid user name, the login_failure.jsp is displayed, and if i enter a correct password for a valid user also the login_failure.jsp is displayed, i don’t know what’s the problem? According to you, what is the problem?
    I’m stuck, i followed like you present
    Thank you in advance 🙂

    Reply
    • February 23, 2014 at 6:48 pm
      Permalink

      Hello, it seems that your web application security configuration is fine but it cannot access data from database. Do you see anything in the stacktrace on console? Can you access your database from other web application? Can you please check it?

      Reply
      • February 23, 2014 at 9:19 pm
        Permalink

        Hello,
        I am a beginner, the first web application that i used is yours.
        I can put what the console display :
        java.lang.UnsatisfiedLinkError: C:\Tomcat7\apache-tomcat-7.0.50\bin\tcnative-1.dll: Can’t load AMD 64-bit .dll on a IA 32-bit platform
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at org.apache.tomcat.jni.Library.(Library.java:42)
        at org.apache.tomcat.jni.Library.initialize(Library.java:174)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.core.AprLifecycleListener.init(AprLifecycleListener.java:180)
        at org.apache.catalina.core.AprLifecycleListener.isAprAvailable(AprLifecycleListener.java:85)
        at org.apache.catalina.connector.Connector.setProtocol(Connector.java:595)
        at org.apache.catalina.connector.Connector.(Connector.java:69)
        at org.apache.catalina.startup.ConnectorCreateRule.begin(ConnectorCreateRule.java:62)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1288)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:616)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:664)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
        java.lang.UnsatisfiedLinkError: C:\Tomcat7\apache-tomcat-7.0.50\bin\tcnative-1.dll: Can’t load AMD 64-bit .dll on a IA 32-bit platform
        at java.lang.ClassLoader$NativeLibrary.load(Native Method)
        at java.lang.ClassLoader.loadLibrary0(Unknown Source)
        at java.lang.ClassLoader.loadLibrary(Unknown Source)
        at java.lang.Runtime.loadLibrary0(Unknown Source)
        at java.lang.System.loadLibrary(Unknown Source)
        at org.apache.tomcat.jni.Library.(Library.java:42)
        at org.apache.tomcat.jni.Library.initialize(Library.java:174)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.core.AprLifecycleListener.init(AprLifecycleListener.java:180)
        at org.apache.catalina.core.AprLifecycleListener.isAprAvailable(AprLifecycleListener.java:85)
        at org.apache.catalina.connector.Connector.setProtocol(Connector.java:595)
        at org.apache.catalina.connector.Connector.(Connector.java:69)
        at org.apache.catalina.startup.ConnectorCreateRule.begin(ConnectorCreateRule.java:62)
        at org.apache.tomcat.util.digester.Digester.startElement(Digester.java:1288)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
        at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
        at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:616)
        at org.apache.catalina.startup.Catalina.load(Catalina.java:664)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
        23 févr. 2014 14:57:26 org.apache.catalina.core.AprLifecycleListener init
        INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files (x86)\Java\jre1.6.0_03\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files (x86)/Java/jre1.6.0_03/bin/client;C:/Program Files (x86)/Java/jre1.6.0_03/bin;C:/Program Files (x86)/Java/jre1.6.0_03/lib/i386;C:\Program Files\Dell\DW WLAN Card;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2008a\bin;C:\Program Files\MATLAB\R2008a\bin\win64;C:\Program Files\Java\jdk1.7.0\bin;C:\Tomcat7\apache-tomcat-7.0.50\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Tcl\bin;C:\Program Files\Dell\DW WLAN Card;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MATLAB\R2008a\bin;C:\Program Files\MATLAB\R2008a\bin\win64;C:\Program Files\Java\jdk1.7.0\bin;C:\Tomcat7\apache-tomcat-7.0.50\bin;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files\Java\jdk1.7.0\bin;C:\Users\Mohamed\Desktop\Utilitaires\apache-ant-1.8.1\bin;;D:\Maha\3éme anneé\eclipse_j2ee;
        23 févr. 2014 14:57:26 org.apache.tomcat.util.digester.SetPropertiesRule begin
        ATTENTION: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:Testweb’ did not find a matching property.
        23 févr. 2014 14:57:26 org.apache.tomcat.util.digester.SetPropertiesRule begin
        ATTENTION: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:Tomcat7FormBasedJAAS’ did not find a matching property.
        23 févr. 2014 14:57:26 org.apache.coyote.AbstractProtocol init
        INFO: Initializing ProtocolHandler [“http-bio-8080”]
        23 févr. 2014 14:57:26 org.apache.coyote.AbstractProtocol init
        INFO: Initializing ProtocolHandler [“ajp-bio-8009″]
        23 févr. 2014 14:57:26 org.apache.catalina.startup.Catalina load
        INFO: Initialization processed in 481 ms
        23 févr. 2014 14:57:26 org.apache.catalina.core.StandardService startInternal
        INFO: Démarrage du service Catalina
        23 févr. 2014 14:57:26 org.apache.catalina.core.StandardEngine startInternal
        INFO: Starting Servlet Engine: Apache Tomcat/7.0.50
        23 févr. 2014 14:57:26 org.apache.catalina.realm.JDBCRealm startInternal
        GRAVE: Exception lors de l”ouverture de la base de données
        java.sql.SQLException: org/postgresql/Driver : Unsupported major.minor version 51.0
        at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:694)
        at org.apache.catalina.realm.JDBCRealm.startInternal(JDBCRealm.java:782)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1109)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:690)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456)
        Caused by: java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:690)
        … 16 more
        23 févr. 2014 14:57:27 org.apache.tomcat.websocket.server.WsSci onStartup
        INFO: JSR 356 WebSocket (Java WebSocket 1.0) support is not available when running on Java 6. To suppress this message, run Tomcat on Java 7, remove the WebSocket JARs from $CATALINA_HOME/lib or add the WebSocketJARs to the tomcat.util.scan.DefaultJarScanner.jarsToSkip property in $CATALINA_BASE/conf/catalina.properties. Note that the deprecated Tomcat 7 WebSocket API will be available.
        23 févr. 2014 14:57:28 org.apache.coyote.AbstractProtocol start
        INFO: Starting ProtocolHandler [“http-bio-8080”]
        23 févr. 2014 14:57:28 org.apache.coyote.AbstractProtocol start
        INFO: Starting ProtocolHandler [“ajp-bio-8009″]
        23 févr. 2014 14:57:28 org.apache.catalina.startup.Catalina start
        INFO: Server startup in 1812 ms
        23 févr. 2014 14:57:34 org.apache.catalina.realm.JDBCRealm authenticate
        GRAVE: Exception pendant le traitement de l”authentification
        java.sql.SQLException: org/postgresql/Driver : Unsupported major.minor version 51.0
        at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:694)
        at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:352)
        at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:296)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:450)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
        Caused by: java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:690)
        … 14 more
        23 févr. 2014 14:57:34 org.apache.catalina.realm.JDBCRealm authenticate
        GRAVE: Exception pendant le traitement de l”authentification
        java.sql.SQLException: org/postgresql/Driver : Unsupported major.minor version 51.0
        at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:694)
        at org.apache.catalina.realm.JDBCRealm.authenticate(JDBCRealm.java:352)
        at org.apache.catalina.authenticator.FormAuthenticator.authenticate(FormAuthenticator.java:296)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:450)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
        Caused by: java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.apache.catalina.realm.JDBCRealm.open(JDBCRealm.java:690)
        … 14 more

        Reply
        • February 24, 2014 at 10:28 am
          Permalink

          Hello Lawra, It seems that there is mismatch between java versions of database driver and your code. Did you rebuild your application before deploying?
          According to stacktrace, I could find these links. They may be helpful to you.
          http://stackoverflow.com/questions/12028166/java-lang-unsupportedclassversionerror-unsupported-major-minor-version-51-0-un
          http://stackoverflow.com/questions/16019586/unsupported-major-minor-version-51-0-unable-to-load-class-org-postgresql-driver

          Reply
          • February 24, 2014 at 2:13 pm
            Permalink

            Hello Prasad,
            So, what’s the solution please, i should change the version of my driver or what ?
            here’s my driver : “postgresql-9.3-1101.jdbc3”
            and i use a jdk 1.7.
            what should i do ? i’m stuck realy 🙁

          • February 24, 2014 at 3:16 pm
            Permalink

            Hi Lawra,
            I would have created a simple jdbc program using jdk 1.7 to check whether the driver really works or not. Could you please do that, so that we can know where the problem is.

        • February 24, 2014 at 10:37 am
          Permalink

          You do not have to create a database module for this tutorial. By mistake, I thought you are using jBoss 7.1. Apologies as I misdirected you a little.

          Reply
          • February 24, 2014 at 4:09 pm
            Permalink

            Hi Parsad,
            I have just a question, the file web.xml is in Tomcat or in the project ??

          • February 24, 2014 at 4:55 pm
            Permalink

            web.xml is deployment descriptor and should be present in your application under WEB-INF folder. Realm configuration is done in Tomcat’s server.xml file.

  • February 24, 2014 at 5:03 pm
    Permalink

    Hi Parsad,
    When i use a database Mysql it works very well
    But, what is the problem for postgresql ?? :'(

    Reply
    • February 24, 2014 at 5:07 pm
      Permalink

      Here’s the problem written in console :
      “java.lang.UnsatisfiedLinkError: C:\Tomcat7\apache-tomcat-7.0.50\bin\tcnative-1.dll: Can’t load AMD 64-bit .dll on a IA 32-bit platform”

      Reply
        • February 24, 2014 at 6:56 pm
          Permalink

          How could I do with jdbc jar file?
          I do not know how exactly, would you please give me an idea ?
          thank you in advance

          Reply
          • February 24, 2014 at 7:31 pm
            Permalink

            This is my jdbc jar file which i use in my application
            postgresql-9.3-1101.jdbc41

          • February 25, 2014 at 4:04 pm
            Permalink

            Hello Lawra, as I am not very sure about postgres jdbc jar file, please go through the links I’ve provided. They will surely help you.

        • February 25, 2014 at 11:39 pm
          Permalink

          Thank you Prasad, i will see that 🙂

          Reply
          • February 26, 2014 at 3:45 am
            Permalink

            I don’t know what’s the role of classpath, i think that not need to add it.
            I check the JAVA_HOME as the following: C:\Program Files\Java\jdk1.7.0
            And the jre in my eclipse is version 7.

        • February 27, 2014 at 11:20 pm
          Permalink

          Hi Parsad,
          Thank you for this tuto, it’s work now 😉
          The problem is i enter a correct user with a correct password but he doesn’t contain a role user

          Reply
          • February 28, 2014 at 6:49 pm
            Permalink

            I am glad this was helpful 🙂 Happy learning.

  • March 8, 2014 at 3:12 pm
    Permalink

    Hi Parsad,
    I want to change the interface of Login.jsp and the protected.jsp.
    I rename the folder protected to authentification and protected.jsp to home.jsp:
    here’s the code for Login.jsp:

    login

    Authentication

    Login:

    Password:

    My problem is when i put this url “http://localhost:8080/Tomcat7FormBasedJAAS/authentication/home.jsp”, the page display with no style, and when i enter the correct user name and password, an error page display “http://localhost:8080/Gestion_de_stock/authentication/inc/style.css” and i don’t know where it comes from !!!!

    Reply
  • March 8, 2014 at 3:13 pm
    Permalink

    login

    Authentication

    Login:

    Password:

    Reply
  • March 8, 2014 at 3:14 pm
    Permalink

    Authentication

    Login:

    Password:

    Reply
  • Pingback:JAAS-reaml-tomcat7 | mauroprogram's Blog

  • Pingback:glassfish-jdbc-realm-and-form-based | mauroprogram's Blog

  • March 19, 2014 at 3:00 am
    Permalink

    Hi Parsad,
    I change the contents of protected.jsp, it contains a Logout button.
    I want to know how can logout to authentication page?
    thank you in advance 🙂

    Reply
    • March 19, 2014 at 8:54 am
      Permalink

      Hi Lawra,
      you can create a LogoutServlet which will invalidate your session and then redirect to your authentication servlet.
      You will need to call LogoutServlet on your Logout button form action.

      Reply
      • March 19, 2014 at 1:36 pm
        Permalink

        Hi Prasad,
        Thank you for your reply.
        I haven’t a authentication servlet but a jsp page.
        How can i redirect LogoutServlet to a authentication jsp page?

        Reply
          • March 19, 2014 at 2:43 pm
            Permalink

            Hi Parsad,
            I have created a LogoutServlet as you told me, which invalidate the session as follows:
            request.getSession().invalidate(); then i redirect it as follows: response.sendRedirect(“/Mywebproject/protected/protected.jsp”);
            in a doGet method.
            And it’s work, thank you 🙂

          • March 19, 2014 at 3:53 pm
            Permalink

            I am glad I could help 🙂 Happy learning.

          • March 26, 2014 at 1:50 pm
            Permalink

            Hi Prasad,
            After logout the user should not be able to see the previous pages, how can i expire the session ?
            thank you in advance 🙂

          • March 26, 2014 at 3:09 pm
            Permalink

            You can call a servlet on Logout link and call session.invalidate(); method. It will invalidate your session.

  • March 24, 2014 at 3:27 pm
    Permalink

    Hi Prasad,

    I followed your example.., you have explained in simple and elegant way, but i am not able to reproduce the same in my Spring MVC application.

    Thank you in advance 🙂

    Reply
    • March 24, 2014 at 8:04 pm
      Permalink

      Hi Siddaram, Thank you for reading the article. I haven’t done it with spring but you can post the problem you are getting and I will try to look into it. I will learn something from you 🙂

      Reply
  • March 24, 2014 at 7:53 pm
    Permalink

    Hi Parsad,
    i want to do this :
    if i enter a valid name and a valid password but it’s not a user, i want to redirect to another page .jsp for example
    which show this message :
    “you don’t have the right to access to this Service!!”
    How can i do this, thank you in advance 🙂

    Reply
    • March 24, 2014 at 11:54 pm
      Permalink

      Hi Lawra,
      Just a gentle correction; my name is Prasad and not Parsad 🙂
      For your requirement, you will have to look into Java Authentication and Authorization Services. You can google JAAS example and it will find you oracle official documentation.
      In this article also, roles are defined, another example can be found here.
      http://www.thejavageek.com/2013/09/18/configure-jaas-jboss-7-1-mysql/

      Reply
      • March 25, 2014 at 2:13 am
        Permalink

        Oh sorry Prasad 🙂
        ok, thank you
        i will see it

        Reply
  • March 25, 2014 at 3:56 pm
    Permalink

    Hi Prasad,
    How can i delete a user from this database?
    Thank you in advance

    Reply
  • March 25, 2014 at 5:49 pm
    Permalink

    Hi Prasad,
    Can i merge the three tables into a single table named users?
    if yes, what changes are needed

    Reply
    • March 26, 2014 at 3:13 pm
      Permalink

      As far as I know, all three tables have specific meaning when they are used in tomcat xml file. So they should not be merged.

      Reply
      • March 26, 2014 at 9:30 pm
        Permalink

        Prasad, How can i update or delete username from database ?
        it seems difficult 🙁

        Reply
        • March 26, 2014 at 11:04 pm
          Permalink

          Hi Lawra, your requirement is not very clear to me. Do you want to delete the user directly from database or from web application? From database, you can simply execute SQL for deletion.

          Reply
          • March 27, 2014 at 12:30 am
            Permalink

            Hi Prasad,
            From both : database and web application.
            I want to know the query for delete and update.

  • April 30, 2014 at 12:43 am
    Permalink

    Good article. I am dealing with a few of these issues as well..|

    Reply
  • April 30, 2014 at 2:32 pm
    Permalink

    Thank you: great tutorial!! I followed it trough and it worked perfectly.

    I still have a problem, and I don’t seem able to find a solution:
    it is possible to have a list of all the user logged in?

    Thank you,
    Maja

    Reply
    • April 30, 2014 at 5:24 pm
      Permalink

      Hello java, thank you for reading and positive feedback. For your problem, my approach would be to listen to every sessionCreated and sessionDestroyed event and store the user information from those sessions into an object that is stored in servlet context.

      Reply
      • April 30, 2014 at 8:05 pm
        Permalink

        Thank you!!!!
        I’ll start trying immediatly.
        🙂

        Reply
  • July 31, 2014 at 4:29 am
    Permalink

    Thank alot for your explanation, its just perfect.

    But i have a small issue, if I want to make the login.jsp page my home page for every visitor
    and when the user enter correct username and password, he then should be redirect to a specific
    page (ex: Welcome.page). How can I do that? Can you please tell me.

    Reply
    • July 31, 2014 at 9:51 am
      Permalink

      Hi Khalid, I am glad you found this article helpful. Do you want to say that every time login page should be displayed and based on user role, it should be redirected to user role specific pages?

      Reply
      • August 2, 2014 at 5:32 am
        Permalink

        Yes, exactly as you said.

        How can I do that?

        Reply
        • August 4, 2014 at 7:07 am
          Permalink

          Hi Khalid,
          You can call a Servlet, say PageForwarder from your login page. This servlet should then retrieves the login credentials and the role of the logged in user. Based on the roles, you can write if else conditions to forward to corresponding pages.

          Reply
  • September 25, 2014 at 12:57 am
    Permalink

    hello Prasad

    i did all the steps above and it doesnt work with me
    even if i put a valid username and password it redirect me to login_failure.jsp

    what should i do 🙁

    Reply
    • September 25, 2014 at 8:33 am
      Permalink

      Hi Marouane, one reason could be that your application was not able to connect to database hence could not read username and password from table. Please check whether you have put mysql connector in classpath. If it isn’t so, please post stacktrace here.

      Reply
  • November 14, 2015 at 1:37 am
    Permalink

    I followed this tutorial exactly. But I am getting this error.

    HTTP Status 404 – /Tomcat7FormBasedJAAS/
    The requested resource is nit available.

    Any idea why?

    Reply
    • November 15, 2015 at 1:52 pm
      Permalink

      are you getting any stacktrace ? Can you check whether you server is running by visiting localhost:8080?

      Reply
  • July 30, 2016 at 8:52 pm
    Permalink

    This really answered my problem, thank you!

    Reply
  • September 14, 2017 at 1:41 pm
    Permalink

    Hello Prasad Kharkar,

    I have tried it, My IDE was netbeans 8.0.2 and apache-tomcat server, and in mysql database db

    username=root, db password=””, when i sent a request of the protected.jsp page it is giving me

    login.jsp page, when I am giving credentials correctly but it is not displaying protected.jsp page,

    it is displaying login_failure page, why? i don’t know ,can you help me please..

    Reply
  • September 14, 2017 at 11:06 pm
    Permalink

    Hello Prasad,

    i have done all the steps above using netbeans 8.0.2 + apache tomcat 8 and it doesn’t work with me
    even if i put a valid username and password it redirect me to login_failure.jsp page

    now what should i do?

    Reply
    • September 19, 2017 at 8:59 am
      Permalink

      are there any errors on console? It seems database is not being read properly.

      Reply
      • October 4, 2017 at 10:14 am
        Permalink

        it is giving an error as like this :

        Caused by : java.lang.CassNotFoundException : com.mysql.jdbc.Driver

        but in the netbeans lib folder I have already added mysql-connector-java-5.1.23-bin.jar

        then also it shows this error. so, now what can i do? please..

        Reply
        • October 5, 2017 at 3:56 pm
          Permalink

          Its definitely not getting the jar. Is the jar available in WEB-INF/lib folder ? It should be picked from there. If not, then please try cleaning the project

          Reply
  • February 21, 2018 at 10:59 pm
    Permalink

    hi, in tomee, the server does not start.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *