added authorized method

This commit is contained in:
lucio 2024-02-22 09:56:36 +01:00
parent 6d47bbfbf4
commit f5d8522e47
4 changed files with 69 additions and 3 deletions

40
pom.xml
View File

@ -11,9 +11,11 @@
<name>Smartgears HelloWorld Service</name>
<packaging>war</packaging>
<properties>
<java.version>11</java.version>
<aspectj-plugin.version>1.14.0</aspectj-plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<scm>
<connection>
@ -39,6 +41,14 @@
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-control-library</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears-app</artifactId>
@ -74,5 +84,31 @@
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- add this plugin if you want to use gcube authorization control funzionalities -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-plugin.version}</version>
<configuration>
<complianceLevel>11</complianceLevel>
<source>11</source>
<target>11</target>
<aspectLibraries>
<aspectLibrary>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-control-library</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -6,6 +6,7 @@ import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.gcube.service.helloworld.services.AuthorizedMethods;
import org.gcube.service.helloworld.services.HelloService;
@ -16,6 +17,7 @@ public class HelloWorld extends Application {
final Set<Class<?>> classes = new HashSet<Class<?>>();
// register resources classes implementing Servlets
classes.add(HelloService.class);
classes.add(AuthorizedMethods.class);
return classes;
}
}

View File

@ -0,0 +1,26 @@
package org.gcube.service.helloworld.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.gcube.common.authorization.control.annotations.AuthorizationControl;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.utils.InnerMethodName;
@Path("auth")
public class AuthorizedMethods {
private static final String ALLOWED_ROLE = "myRole";
@AuthorizationControl(allowedRoles={ALLOWED_ROLE})
@GET
public String authorized() {
InnerMethodName.set("auth");
Secret secret = SecretManagerProvider.get();
String userId = secret.getOwner().getId();
String context = secret.getContext();
return String.format("User %s in context %s is authorized to execute this method because he has the correct role", userId,context);
}
}

View File

@ -7,6 +7,7 @@ import javax.ws.rs.core.MediaType;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.utils.InnerMethodName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -18,7 +19,8 @@ public class HelloService {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
Secret secret = SecretManagerProvider.instance.get();
InnerMethodName.set("hello");
Secret secret = SecretManagerProvider.get();
String userId = secret.getOwner().getId();
String context = secret.getContext();
logger.info("caller id is {}",userId);