You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Tobias Gierke <to...@voipfuture.com> on 2013/06/11 17:20:33 UTC

Possible to expose a Tomcat Realm instance through JNDI ?

Hi,

In my web app, I'd like to re-use the (server-wide) Tomcat Realm that is 
already being used for HTTP Basic authentication but couldn't find a way 
how to get hold of the actual Realm instance.

I spent quite some time looking for a solution (complicated by the fact 
that most Google hits actually referred to the LDAP authentication 
realm) but found none. Is  there a "config-file-only" solution or do I 
need to dig into the Tomcat source code and come up with my own JNDI 
ObjectFactory to achieve this ?

Cheers,
Tobias



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by "Tanglin, Oliver" <OL...@saic.com>.
Thanks for the detail.

As a final note and in case you are not already doing this, I have found the <scope>provided</scope> element in <dependency/> useful under similar circumstances when building a war. This prevents bundling of dependencies provided by the container such as catalina, servlet, etc.

Regards,
Oliver



-----Original Message-----
From: users-return-242264-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
Sent: Wed 6/12/2013 5:17 AM
To: users@tomcat.apache.org
Subject: Re: Possible to expose a Tomcat Realm instance through JNDI ?
 
Hi,
> Thanks for posting your results.
>
> But I am thinking now that this may lead to a portability issue. Push to a container other than Tomcat and 'org.apache.catalina.User' will not be available on the classpath.
Yes, this is a portability issue. But to be honest, changing (assuming a 
reasonably big application) to a different Application server is almost 
as unlikely as migrating to a different database vendor. At least in the 
last 15 years I've never seen this happen ... YMMV though ;)
>
> Also, during development you are forced to include catalina as a dependency to compile. Or are you somehow using reflection?
Yes, I'm depending on the Tomcat API. We already had it as a dependency 
in a different sub-module because we're using a custom Realm 
implementation. Since it's a Maven build I only need to update the 
version number in the parent POM.xml when upgrading the Tomcat, so not 
much of a hassle there.

Cheers,
Tobias
>
> Oliver
>
>
>
> -----Original Message-----
> From: users-return-242261-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
> Sent: Wed 6/12/2013 2:12 AM
> To: users@tomcat.apache.org
> Subject: Re: Possible to expose a Tomcat Realm instance through JNDI ?
>   
> Hi Oliver,
>> I think I now understand your issue. I was faced with a similar problem and could not figure out how to get the roles of an authenticated user through the servlet API.
>>
>> It seems to only allow the question 'request.isUserInRole(role)'. But does not seem to provide a way to get a list of roles that the user is in.
>>
>> I used a kludge whereby I defined the valid roles in a context init parameter (bad duplication of effort). Then used 'request.isUserInRole(role)'. I did not think to cast the 'request.getUserPrincipal()' return value.
>>
>> Please post if your methodology works.
>>
>> Oliver
>>
> I ended up using WebserviceContext#getUserPrincipal() and downcasting it
> to org.apache.catalina.User ; should probably also work with the plain
> HttpServletRequest since WebserviceContext is just a wrapper.
>
> Cheers,
> Tobias
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org



Re: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by Tobias Gierke <to...@voipfuture.com>.
Hi,
> Thanks for posting your results.
>
> But I am thinking now that this may lead to a portability issue. Push to a container other than Tomcat and 'org.apache.catalina.User' will not be available on the classpath.
Yes, this is a portability issue. But to be honest, changing (assuming a 
reasonably big application) to a different Application server is almost 
as unlikely as migrating to a different database vendor. At least in the 
last 15 years I've never seen this happen ... YMMV though ;)
>
> Also, during development you are forced to include catalina as a dependency to compile. Or are you somehow using reflection?
Yes, I'm depending on the Tomcat API. We already had it as a dependency 
in a different sub-module because we're using a custom Realm 
implementation. Since it's a Maven build I only need to update the 
version number in the parent POM.xml when upgrading the Tomcat, so not 
much of a hassle there.

Cheers,
Tobias
>
> Oliver
>
>
>
> -----Original Message-----
> From: users-return-242261-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
> Sent: Wed 6/12/2013 2:12 AM
> To: users@tomcat.apache.org
> Subject: Re: Possible to expose a Tomcat Realm instance through JNDI ?
>   
> Hi Oliver,
>> I think I now understand your issue. I was faced with a similar problem and could not figure out how to get the roles of an authenticated user through the servlet API.
>>
>> It seems to only allow the question 'request.isUserInRole(role)'. But does not seem to provide a way to get a list of roles that the user is in.
>>
>> I used a kludge whereby I defined the valid roles in a context init parameter (bad duplication of effort). Then used 'request.isUserInRole(role)'. I did not think to cast the 'request.getUserPrincipal()' return value.
>>
>> Please post if your methodology works.
>>
>> Oliver
>>
> I ended up using WebserviceContext#getUserPrincipal() and downcasting it
> to org.apache.catalina.User ; should probably also work with the plain
> HttpServletRequest since WebserviceContext is just a wrapper.
>
> Cheers,
> Tobias
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org

RE: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by "Tanglin, Oliver" <OL...@saic.com>.
Thanks for posting your results. 

But I am thinking now that this may lead to a portability issue. Push to a container other than Tomcat and 'org.apache.catalina.User' will not be available on the classpath. 

Also, during development you are forced to include catalina as a dependency to compile. Or are you somehow using reflection?

Oliver



-----Original Message-----
From: users-return-242261-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
Sent: Wed 6/12/2013 2:12 AM
To: users@tomcat.apache.org
Subject: Re: Possible to expose a Tomcat Realm instance through JNDI ?
 
Hi Oliver,
> I think I now understand your issue. I was faced with a similar problem and could not figure out how to get the roles of an authenticated user through the servlet API.
>
> It seems to only allow the question 'request.isUserInRole(role)'. But does not seem to provide a way to get a list of roles that the user is in.
>
> I used a kludge whereby I defined the valid roles in a context init parameter (bad duplication of effort). Then used 'request.isUserInRole(role)'. I did not think to cast the 'request.getUserPrincipal()' return value.
>
> Please post if your methodology works.
>
> Oliver
>
I ended up using WebserviceContext#getUserPrincipal() and downcasting it 
to org.apache.catalina.User ; should probably also work with the plain 
HttpServletRequest since WebserviceContext is just a wrapper.

Cheers,
Tobias


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org




Re: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by Tobias Gierke <to...@voipfuture.com>.
Hi Oliver,
> I think I now understand your issue. I was faced with a similar problem and could not figure out how to get the roles of an authenticated user through the servlet API.
>
> It seems to only allow the question 'request.isUserInRole(role)'. But does not seem to provide a way to get a list of roles that the user is in.
>
> I used a kludge whereby I defined the valid roles in a context init parameter (bad duplication of effort). Then used 'request.isUserInRole(role)'. I did not think to cast the 'request.getUserPrincipal()' return value.
>
> Please post if your methodology works.
>
> Oliver
>
I ended up using WebserviceContext#getUserPrincipal() and downcasting it 
to org.apache.catalina.User ; should probably also work with the plain 
HttpServletRequest since WebserviceContext is just a wrapper.

Cheers,
Tobias


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by "Tanglin, Oliver" <OL...@saic.com>.
I think I now understand your issue. I was faced with a similar problem and could not figure out how to get the roles of an authenticated user through the servlet API.

It seems to only allow the question 'request.isUserInRole(role)'. But does not seem to provide a way to get a list of roles that the user is in.

I used a kludge whereby I defined the valid roles in a context init parameter (bad duplication of effort). Then used 'request.isUserInRole(role)'. I did not think to cast the 'request.getUserPrincipal()' return value.

Please post if your methodology works.

Oliver



-----Original Message-----
From: users-return-242241-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
Sent: Tue 6/11/2013 12:07 PM
To: users@tomcat.apache.org
Subject: Re: Possible to expose a Tomcat Realm instance through JNDI ?
 
Hi Oliver,
> I don't think there is a HTTP Basic authentication realm. The authentication type is declared in the <auth-method> of your web.xml and can be used in conjunction with a number of different realm implementations.
>
> Exactly what do you mean by re-use? Does this mean you are doing authentication from within your web app?
My application is exposing a SOAP service (through Spring-WS servlet) 
for which I want to do method-level access control. Since the service 
endpoint already uses container-based HTTP Basic authentication, I'd 
like to reuse the realm implementation (and configuration, obviously) in 
my own code to get hold of the user's roles.

I just found HttpServletRequest#getUserPrincipal() , maybe I can just 
downcast the result to org.apache.catalina.realm.GenericPrincipal and 
invoke getRoles() on this... ugly, but well... ;)

Cheers,
Tobi

>
> Oliver
>
>
>
> -----Original Message-----
> From: users-return-242237-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
> Sent: Tue 6/11/2013 11:20 AM
> To: users@tomcat.apache.org
> Subject: Possible to expose a Tomcat Realm instance through JNDI ?
>   
> Hi,
>
> In my web app, I'd like to re-use the (server-wide) Tomcat Realm that is
> already being used for HTTP Basic authentication but couldn't find a way
> how to get hold of the actual Realm instance.
>
> I spent quite some time looking for a solution (complicated by the fact
> that most Google hits actually referred to the LDAP authentication
> realm) but found none. Is  there a "config-file-only" solution or do I
> need to dig into the Tomcat source code and come up with my own JNDI
> ObjectFactory to achieve this ?
>
> Cheers,
> Tobias
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


-- 
Tobias Gierke
Development

VOIPFUTURE GmbH   Wendenstraße 4   20097 Hamburg,  Germany
Phone +49 40 688 900 111 Mobile +49 172 323 06 11 Fax +49 40 688 900 199
Email jan.bastian@voipfuture.com   Web http://www.voipfuture.com
  
CEO Jan Bastian
	
Commercial Court AG Hamburg   HRB 109896, VAT ID DE263738086




Re: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by Tobias Gierke <to...@voipfuture.com>.
Hi Oliver,
> I don't think there is a HTTP Basic authentication realm. The authentication type is declared in the <auth-method> of your web.xml and can be used in conjunction with a number of different realm implementations.
>
> Exactly what do you mean by re-use? Does this mean you are doing authentication from within your web app?
My application is exposing a SOAP service (through Spring-WS servlet) 
for which I want to do method-level access control. Since the service 
endpoint already uses container-based HTTP Basic authentication, I'd 
like to reuse the realm implementation (and configuration, obviously) in 
my own code to get hold of the user's roles.

I just found HttpServletRequest#getUserPrincipal() , maybe I can just 
downcast the result to org.apache.catalina.realm.GenericPrincipal and 
invoke getRoles() on this... ugly, but well... ;)

Cheers,
Tobi

>
> Oliver
>
>
>
> -----Original Message-----
> From: users-return-242237-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
> Sent: Tue 6/11/2013 11:20 AM
> To: users@tomcat.apache.org
> Subject: Possible to expose a Tomcat Realm instance through JNDI ?
>   
> Hi,
>
> In my web app, I'd like to re-use the (server-wide) Tomcat Realm that is
> already being used for HTTP Basic authentication but couldn't find a way
> how to get hold of the actual Realm instance.
>
> I spent quite some time looking for a solution (complicated by the fact
> that most Google hits actually referred to the LDAP authentication
> realm) but found none. Is  there a "config-file-only" solution or do I
> need to dig into the Tomcat source code and come up with my own JNDI
> ObjectFactory to achieve this ?
>
> Cheers,
> Tobias
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org


-- 
Tobias Gierke
Development

VOIPFUTURE GmbH   Wendenstraße 4   20097 Hamburg,  Germany
Phone +49 40 688 900 111 Mobile +49 172 323 06 11 Fax +49 40 688 900 199
Email jan.bastian@voipfuture.com   Web http://www.voipfuture.com
  
CEO Jan Bastian
	
Commercial Court AG Hamburg   HRB 109896, VAT ID DE263738086


RE: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by "Tanglin, Oliver" <OL...@saic.com>.
I don't think there is a HTTP Basic authentication realm. The authentication type is declared in the <auth-method> of your web.xml and can be used in conjunction with a number of different realm implementations.

Exactly what do you mean by re-use? Does this mean you are doing authentication from within your web app?

Oliver



-----Original Message-----
From: users-return-242237-OLIVER.TANGLIN=saic.com@tomcat.apache.org on behalf of Tobias Gierke
Sent: Tue 6/11/2013 11:20 AM
To: users@tomcat.apache.org
Subject: Possible to expose a Tomcat Realm instance through JNDI ?
 
Hi,

In my web app, I'd like to re-use the (server-wide) Tomcat Realm that is 
already being used for HTTP Basic authentication but couldn't find a way 
how to get hold of the actual Realm instance.

I spent quite some time looking for a solution (complicated by the fact 
that most Google hits actually referred to the LDAP authentication 
realm) but found none. Is  there a "config-file-only" solution or do I 
need to dig into the Tomcat source code and come up with my own JNDI 
ObjectFactory to achieve this ?

Cheers,
Tobias



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org




Re: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by Tobias Gierke <to...@voipfuture.com>.
Hi,
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> Tobias,
>
> On 6/11/13 11:20 AM, Tobias Gierke wrote:
>> In my web app, I'd like to re-use the (server-wide) Tomcat Realm
>> that is already being used for HTTP Basic authentication but
>> couldn't find a way how to get hold of the actual Realm instance.
>>
>> I spent quite some time looking for a solution (complicated by the
>> fact that most Google hits actually referred to the LDAP
>> authentication realm) but found none. Is  there a
>> "config-file-only" solution or do I need to dig into the Tomcat
>> source code and come up with my own JNDI ObjectFactory to achieve
>> this ?
> What are you actually trying to accomplish? Do you want to
> authenticate a user, or get information about an authenticated user?
I need to retrieve the roles assigned to the current (authenticated) 
user. I now solved this by simply using 
WebServiceContext#getUserPrincipal() (inside a JAX-WS webservice 
published by Spring) and downcasting the result to org.apache.catalina.User.

Cheers,
Tobias


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Possible to expose a Tomcat Realm instance through JNDI ?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Tobias,

On 6/11/13 11:20 AM, Tobias Gierke wrote:
> In my web app, I'd like to re-use the (server-wide) Tomcat Realm
> that is already being used for HTTP Basic authentication but
> couldn't find a way how to get hold of the actual Realm instance.
> 
> I spent quite some time looking for a solution (complicated by the
> fact that most Google hits actually referred to the LDAP
> authentication realm) but found none. Is  there a
> "config-file-only" solution or do I need to dig into the Tomcat
> source code and come up with my own JNDI ObjectFactory to achieve
> this ?

What are you actually trying to accomplish? Do you want to
authenticate a user, or get information about an authenticated user?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJRt4B8AAoJEBzwKT+lPKRYYMsP/0mmfpuoeHfhyBtk4r/FhNj+
ujAyd7Ah4lXSPGwZP+9oSLkQFNyn+KzXv0Mw7QpdbGVw1J0mQ/umwxikMriXEKmH
rmXquK060IfAubj7ptvQBu6zgFis/6A1N8r51RmrbUej4bIWGrujB0nRAcpWhT3x
Ynt+0sQ+jeNcPhL5QBwzttdBMJf6hRsmNdjMlnwk/zV/E0qaQTpX0NeUuAkgSFuF
ThpkE92rla8S+NL0mS4knj54MVMs3/NRXtm3g+WMxAjPxbKEbNRWaWziK9Pt7N3H
OWVOGJGvQ2x4XMtb3AULgnVNgWxtc7C6WLOTIVqHeqEDKuk+4UOyPB/o9C5Tyrz/
rChGpEMfhLstRDjOvF40oGewXJ+0vC6oaYa9VogMbf+NDtwnyDmX8sXPmROFChj6
9dP47LtJC+IOkMRwkiaRGbrQwzyWtDBdOMueYz2bdpAlbaCtUnb2IGOPddCSmIuX
tNgqn084ZnmuzmgJE8x1QKkhBiZwrc2N5tcvP4JqujukM27mx5W+r31iTFXPUObQ
5ggpSdUB2ijy5+YOsKa6aYcMMSE+mjENTg/CqnHPPWm7czDYhxFUwokvT7ndXCsq
/ZfxcGiccmYPkfpQJ5iQ6m9vtKmMtofr6/jMatv2sQjN17phc/Fb3fjDD5+pnYEG
214EIEkBhZ76WvOTUQ0X
=gC6q
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org