JAX-WS webservice and @rolesAllowed

Can I use the @RolesAllowed annotation on the JAX-WS web service, and if so, how?

I have a webservice on Glassfish 3.1.1 using Basic Authentication, but the restrictions expressed with @RolesAllowed are ignored. Information about the role must be available, as I can access it as follows:

 @Resource WebServiceContext wsContext; if (wsContext.isUserInRole("READ")) log.info("Role: READ"); 

I get the expected role, but all methods are still available, even if @RolesAllowed set to another role. @DenyAll doesn't work either.

If these annotations are not supported, can deployment descriptors be used to control access to web service methods based on user roles?

Edit : This part of the JAVA EE 6 tutorial describes how to use the @RolesAllowed annotation. He reads

For Java EE components, you define security roles using the @DeclareRoles and @RolesAllowed metadata annotations.

Web services are not listed as Java EE components in the first part of the tutorial, so it seems that security annotations are not supported.

Edit2 Following Izan, I repeated this attempt. Here is what I did:

 @Webservice @DeclareRoles(value = {"READ", "UPDATE", "DELETE"}) public class ServiceImpl implements Service { @Override @WebMethod(operationName = "helloWorld") @RolesAllowed({"NONE"}) public String helloWorld() throws Exception { return "Hello World!"; } } 

Using this type of setup, everyone can access the method, no matter what roles are installed. Users receive authentication (this can be seen in audit.log), but authorization is not performed. As stated above, I can access the role from WebServiceContext (in fact, I do manual authorization using this information).

By adding the @Stateless , let me use security annotations. So, @permitAll works as expected. But using roles still does not work, as the user is not receiving authentication now. They appear as ANONYMOUS in the audit log and are denied access.

My web.xml as follows:

 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>OneMore</display-name> <security-constraint> <display-name>WebServiceSecurity</display-name> <web-resource-collection> <web-resource-name>Authorized users only</web-resource-name> <url-pattern>/service</url-pattern> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>READ</role-name> <role-name>UPDATE</role-name> <role-name>DELETE</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC</auth-method> </login-config> <security-role> <role-name>READ</role-name> </security-role> <security-role> <role-name>UPDATE</role-name> </security-role> <security-role> <role-name>DELETE</role-name> </security-role> </web-app> 

Glassfish-web.xml just maps role names to group names, for example:

 <security-role-mapping> <role-name>READ</role-name> <group-name>READ</group-name> </security-role-mapping> 

Edit 3 Thanks to Izan and countless attempts later, I finally got his job.

As mentioned earlier, the highlight was switching from a simple web service to an EJB web service by adding the @Stateless . This allows you to use security annotations.

This change is necessary to change deployment descriptors. Although the initial web service requires Glassfish-web.xml to configure the roles, then glassfish-ejb-jar.xml is required.

+6
source share
2 answers

This may be a pretty silly question, but are your EJB web services? As noted in Annotations and Security Authorization in the GlassFish and Java EE 5 SDK

The @PermitAll, @DenyAll, and @RolesAllowed annotations are defined to specify EJB business method permissions.

I use these annotations with upstream WS from the stagnant EJBs and they work like a charm in JBoss.


EDIT 1 @TPete I will add code to show you more or less what I am doing.

 @Stateless @WebService() @WebContext(contextRoot = WSContextRoot.CTX_ROOT, authMethod = "BASIC") @EndpointConfig(configName = "Standard WSSecurity Endpoint") @SecurityDomain(value = "myDeclaredDomain") @RolesAllowed({ "AUTHORISED" }) @SOAPBinding(style = SOAPBinding.Style.DOCUMENT) public class MyWS implements MyInterface { @Override public void doSomething(){ //impl } } 

As for the interface

 @Remote @WebService public interface MyInterface { @WebMethod(operationName="doSomething") public void doSomething(); } 

WebContext, EndpointConfig and SecurityDomain are JBoss annotations, but I suppose there is something similar for GlassFish or an equivalent way to do this. The security domain is included in the deployment descriptor for jboss and is defined in the login-config.xml file from the JBoss configuration files.


EDIT 2 @TPete

Suppose you need to add some EJB deployment descriptors from the Glassfish file, the sun-ejb-jar.xml file package inside your EAR. Again, from the same article as the answer, there is a chapter called “Using Deployment Descriptors,” which states

For EJB web service @RolesAllowed using @RolesAllowed you need to specify the type of authentication that will be used by specifying the elements in sun-ejb-jar.xml too. To authenticate the username and password, set this item to BASIC, as shown in the following example. This step is required only for EJB web service endpoints and is not required for EJB.

Since you are defining the endpoint of an EJB web service, I think you should put this handle in the EAR. Take a good look at this article, it describes the process you are doing well :-)

+6
source

The original question is old, but I still leave a comment just in case, when someone like me came across it. Starting with EJB 3.1, EJBs can be packaged into a WAR module, but when it comes to providing them, you must use EJB deployment descriptors. What is not clear in the specification is that EJBs cannot be declared as Servlets in web.xml, otherwise the application will not start.

Here's a great article on EJB packaging in WAR modules and differences with packaging in EJB JAR modules: http://pic.dhe.ibm.com/infocenter/wasinfo/v8r0/index.jsp?topic=%2Fcom. ibm.websphere.nd.multiplatform.doc% 2Finfo% 2Fae% 2Fae% 2Fcejb_ejbinwar.html

0
source

Source: https://habr.com/ru/post/907313/


All Articles