AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



web-service-security: How can you apply security to RESTful services?

Please explain with an example How can you apply security to RESTful services.

web-service x 19
security x 9
Posted On : 2016-03-26 22:07:48.0
profile Mantu Kumar - anyforum.in Mantu Kumar
8200
up-rate
3
down-rate

Answers


You can secure your RESTful Web services using one of the following methods to support authentication, authorization, or encryption:

1. Updating the web.xml deployment descriptor to define security configuration. See Securing RESTful Web Services Using web.xml.

2. Using the javax.ws.rs.core.SecurityContext interface to implement security programmatically. See Securing RESTful Web Services Using SecurityContext.

3. Applying annotations to your JAX-RS classes. See Securing RESTful Web Services Using Annotations..

4. Using Jersey OAuth libraries to sign and verify requests. For more information about using and installing the OAuth libraries, see the Jersey and OAuth wiki at: https://wikis.oracle.com/display/Jersey/OAuth



Securing RESTful Web Services Using web.xml:
-----------------------------------------------------------------------------------------
You secure RESTful Web services using the web.xml deployment descriptor as you would for other Java EE Web applications.

For example, to secure your RESTful Web service using basic authentication, perform the following steps:

* Define a <security-constraint> for each set of RESTful resources (URIs) that you plan to protect.

* Use the <login-config> element to define the type of authentication you want to use and the security realm to which the security constraints will be applied.

* Define one or more security roles using the <security-role> tag and map them to the security constraints defined in step 1. For more information, see "security-role" in Programming Security for Oracle WebLogic Server.

* To enable encryption, add the <user-data-constraint> element and set the <transport-guarantee> subelement to CONFIDENTIAL. For more information, see "user-data-constraint" in Programming Security for Oracle WebLogic Server.

Example: Securing RESTful Web Services Using Basic Authentication
------------------------------------------------------------------------------------------------------------------------
<web-app>
<servlet>
<servlet-name>RestServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Orders</web-resource-name>
<url-pattern>/orders</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>default</realm-name>
</login-config>
<security-role>
<role-name>admin</role-name>
</security-role>
</web-app>


Securing RESTful Web Services Using SecurityContext
---------------------------------------------------------------------------------------------------------------
The javax.ws.rs.core.SecurityContext interface provides access to security-related information for a request. The SecurityContext provides functionality similar to javax.servlet.http.HttpServletRequest, enabling you to access the following security-related information:

* java.security.Principal object containing the name of the user making the request.

* Authentication type used to secure the resource, such as BASIC_AUTH, FORM_AUTH, and CLIENT_CERT_AUTH.

* Whether the authenticated user is included in a particular role.

* Whether the request was made using a secure channel, such as HTTPS.

* You access the SecurityContext by injecting an instance into a class field, setter method, or method parameter using the javax.ws.rs.core.Context annotation.

Following example shows how to inject an instance of SecurityContext into the sc method parameter using the @Context annotation, and check whether the authorized user is included in the admin role before returning the response.


Example: Securing RESTful Web Service Using SecurityContext
-----------------------------------------------------------------------------------------------------------------------

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.Context;

...

@Path("/stateless")
@Stateless(name = "JaxRSStatelessEJB")
public class StlsEJBApp {
...
@GET
@Produces("text/plain;charset=UTF-8")
@Path("/hello")
public String sayHello(@Context SecurityContext sc) {
if (sc.isUserInRole("admin")) return "Hello World!";
throw new SecurityException("User is unauthorized.");
}



Securing RESTful Web Services Using Annotations:
--------------------------------------------------------------------------------------------------
The javax.annotation.security package provides annotations, that you can use to secure your RESTful Web services.


@DeclareRoles: Declares roles.

@DenyAll: Specifies that no security roles are allowed to invoke the specified methods.

@PermitAll: Specifies that all security roles are allowed to invoke the specified methods.

@RolesAllowed: Specifies the list of security roles that are allowed to invoke the methods in the application.

@RunAs: Defines the identity of the application during execution in a J2EE container.


Following example shows how to define the security roles that are allowed, by default, to access the methods defined in the helloWorld class. The sayHello method is annotated with the @RolesAllows annotation to override the default and only allow users that belong to the ADMIN security role.

Example: Securing RESTful Web Service Using SecurityContext
---------------------------------------------------------------------------------------------------------------------
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.annotation.Security.RolesAllowed;


@Path("/helloworld")
@RolesAllowed({"ADMIN", "ORG1"})
public class helloWorld {

@GET
@Path("sayHello")
@Produces("text/plain")
@RolesAllows("ADMIN")
public String sayHello() {
return "Hello World!";
}
}

Reference: Oracle Docs

Posted On : 2016-03-27 00:30:29
Satisfied : 3 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250050
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in