I am new to jax rs implementation. I had created RESTFUL application contains the web services using jax rs. But I want to provide the authentication and authorization to some of the web services. I am unable to configure the web.xml for the authentication. Thanks in advance
Asked
Active
Viewed 1,524 times
0
-
What container are you using? Tomcat? Other? – David Brossard Oct 15 '13 at 12:43
-
You didn't mention how you want to achieve the authorization either. You can use JAX-RS handlers that check the payload and make authorization requests to a XACML decision engine. – David Brossard Oct 15 '13 at 12:52
1 Answers
0
You can use container-level authentication. For instance, in Apache Tomcat, you can configure HTTP-based authentication (e.g. BASIC, DIGEST, or even FORM-based though the latter does not make sense for APIs).
To configure HTTP BASIC authentication as an example you need to:
- define users in a user store and connect Tomcat to it. By default Tomcat provides you with a tomcat-users.xml file which you can use to define users. You could also configure Tomcat to use LDAP as a source
- update your web.xml file to add the
security-constraint
element - update your web.xml to add the
login-config
element - update your web.xml to add the
security-role
element
Here's an example snippet:
<security-constraint>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>basic-user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<role-name>basic-user</role-name>
</security-role>
Note that there is a great answer already available here too: Easy way for Authentication and Authorization with JAX-RS Jersey

Community
- 1
- 1

David Brossard
- 13,584
- 6
- 55
- 88
-
Thanks for the response. I am using the tomcat container.But I can provide the API key to the vendors(ROLE_VENDOR) and my super admin((ROLE_SUPERADMIN)) and i can check it with my database.So I want to authenticate to the user by using jaxrs with cxf, and some of my web services require the authorization to Vendors, but my super admin can have all the rights. – Test Oct 16 '13 at 06:04