<Realm className="org.apache.catalina.realm.JNDIRealm"
debug="99"
connectionURL="ldap://ldap.viralpatel.net:389/"
userPattern="{0}" />
Code language: HTML, XML (xml)
Also comment out the entry for <Realm>: <!—
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
debug="0"
resourceName="UserDatabase"/>
-->
Code language: HTML, XML (xml)
Let us see now what are the different configuration parameters that we are using in above code. <security-constraint>
<web-resource-collection>
<web-resource-name>Logging Area</web-resource-name>
<description>
Authentication for registered users.
</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>*</role-name>
</security-role>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Please enter your Username</realm-name>
</login-config>
Code language: HTML, XML (xml)
Note that in above code we have mapped the URL /*. If we want to use authentication in some particular area of our website like some admin module than map particular URL with security-constraint. <!—restricted files under folder “Admin”
<url-pattern>/Admin/*</url-pattern>
<!—restricted file OfficeDocIndex.jsp
[/code]
To fetch the user details in Java, copy following code in your JSP/Java file.
[code="java"]
import java.security.Principal;
……
Principal principal = request.getUserPrincipal();
String userName = principal.getName();
.…
[/code]
Now once you start the Tomcat and visit your website, following popup will be shown.
<img src="//www.viralpatel.net/app/uploads/2008/12/ldap-authentication-popup-tomcat-server.jpg" alt="ldap-authentication-popup-tomcat-server" title="ldap-authentication-popup-tomcat-server" width="326" height="289" class="alignnone size-full wp-image-394" />
<h2>Configurations in JBoss Server</h2>
I assume you have installed JDK and JBoss server and have set few environment variables such as CLASSPATH, PATH, JAVA_HOME etc.
First step is to implement LDAP in JBoss is to modify <strong>login-config.xml</strong>. Open login-config.xml from <strong>conf </strong>directory from your JBoss installation directory and add following tag.
<!-- wp:code {"language": "xml"} --><pre class="wp-block-code"><code></code></pre><!-- /wp:code -->
<application-policy name="website-domain">
<authentication>
<login-module code="org.jboss.security.auth.spi.LdapLoginModule" flag="required">
<module-option name = "debug">true</module-option>
<module-option name = "java.naming.factory.initial">
com.sun.jndi.ldap.LdapCtxFactory
</module-option>
<module-option name = "java.naming.provider.url">
ldap://ldap.viralpatel.net:389/</module-option>
</login-module>
</authentication>
</application-policy>
Code language: HTML, XML (xml)
Next step will be to create jboss-web.xml. Create a file jboss-web.xml copy following code in it. And place this file under WEB-INF directory of your project. <?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<security-domain>java:/jaas/website-domain</security-domain>
</jboss-web>
Code language: HTML, XML (xml)
Note that, the name website-domain is the security domain and we have specified in application policy of login-config.xml. Now open web.xml from WEB-INF directory of your application and add following code in it. <security-constraint>
<web-resource-collection>
<web-resource-name>ADMIN</web-resource-name>
<description>An example security config that only allows users with the
role ADMIN to access the HTTP servlets
</description>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Authentication require</realm-name>
</login-config>
<security-role>
<role-name>*</role-name>
</security-role>
Code language: HTML, XML (xml)
To fetch the user details in Java, copy following code in your JSP/Java file. import java.security.Principal;
// Get principal from the request.
Principal principal = request.getUserPrincipal();
String userName = principal.getName();
// deal with the userName get from principal.
Code language: Java (java)
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
So interesting! I couldn't even guess it might be so exciting!
You can use request.getRemoteUser() instead.
Hi,
One question: what should be the LDAP "structure" for the logins to succeed?
I mean, how can I tell what DN to use, how to find users, and where are the roles defined in LDAP?
I can have for example a DN like this:
uid=jack, ou=people, dc=example, dc=com
So the user will have attributes in LDAP, the password for example, and the roles ... where in LDAP will I define user roles so that JBoss will pick them from the correct attribute?
Thanks
Hi Viral,
Thanks for such a wonderful site.
I tried to implement this, but i had no success.
The authentication window pops up but when i give uid and pwd it doesnot take it.
After 3 attempts i get 404 error.
1. I updated server.xml
2.Update web.xml
3 .Added jars to commons/lib directory too.
Thanks,
Naveen.
Hi
This Tutorial is incomplete and does not help,
Why is it inclomplete what is missing. plz give us details
Viral,
is the info for implementing ldap still valid for JBOSS AS 7
@rd I did not find any login-config.xml in JBoss 7. So I am not sure if this tutorial is applicable for AS 7.
hi i am trying to integrate jack rabbit to the jboss .. if u have any idea just post it.... and mail me also.... and i forgot to tell something ur post s are very help full to me at the time of need
I am new to LDAP , please explain me in detail, how roles are defined in this concept, and we have to use only jboss ? no other servers we have to use please answer this
Hi Viral,
Have implemented the same in apache successfully. Tested too. But how the same can be done for Websphere 7?
Can you please tell us in detail?
Thanks !!!
Hi Tilak,
can you please mail me the procedure how did you implement the above code in the apache successfully.
waiting for your reply.
T&R,
Khaleed K