Springboot integration with AAF 70/103870/3
authorDavid Stilwell <stilwelld@att.com>
Wed, 18 Mar 2020 14:02:50 +0000 (10:02 -0400)
committerDan Timoney <dtimoney@att.com>
Thu, 19 Mar 2020 20:14:31 +0000 (16:14 -0400)
Changes made: pom.xml updates, App.java add Beans for realm and filterchain

Issue-ID: CCSDK-2178
Change-Id: I29aa242ceff6a2f840b93a8d18ca5385190ca6d2
Signed-off-by: David Stilwell <stilwelld@att.com>
sliapi/springboot/README.md
sliapi/springboot/pom.xml
sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java
sliapi/springboot/src/main/resources/shiro-users.properties [new file with mode: 0644]
sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/AppTest.java [new file with mode: 0644]

index 3e47f34..38be1c2 100644 (file)
@@ -1,8 +1,12 @@
 This directory contains a demo springboot implementation of the SLI-API healthcheck method.
 
-To start this server, run:
+To start this server with out AAF authentication, run:
 mvn -DserviceLogicDirectory=src/main/resources spring-boot:run
 
+To start this server with AAF authentication, run:
+mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dcadi_prop_files=/opt/onap/sdnc/data/properties/org.onap.sdnc.props -DserviceLogicDirectory=src/main/resources"
+
+
 This will start a servlet on port 8080.  To test to that servlet, post a blank
 message to that port:
 
@@ -26,4 +30,4 @@ An example request
         "mixed": "cAsE"
     }
 }
-```
\ No newline at end of file
+```
index 8e88945..2a3fbcb 100644 (file)
@@ -18,6 +18,8 @@
 
     <properties>
           <start-class>org.onap.ccsdk.sli.core.sliapi.springboot.App</start-class>
+          <shiro.version>1.5.0</shiro.version>
+          <aaf-shiro-bundle.version>2.1.13</aaf-shiro-bundle.version>
     </properties>
 
     <dependencies>
@@ -26,8 +28,9 @@
             <artifactId>swagger-annotations</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-web</artifactId>
+            <groupId>org.apache.shiro</groupId>
+            <artifactId>shiro-spring-boot-web-starter</artifactId>
+            <version>${shiro.version}</version>
                        <exclusions>
                                <exclusion>
                                        <groupId>org.springframework.boot</groupId>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-log4j2</artifactId>
                </dependency>
+        <dependency>
+             <groupId>org.onap.aaf.cadi</groupId>
+             <artifactId>aaf-cadi-shiro</artifactId>
+             <version>${aaf-shiro-bundle.version}</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
index ed3ee04..2892430 100644 (file)
@@ -24,6 +24,13 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;\r
 import org.springframework.context.annotation.ComponentScan;\r
 import springfox.documentation.swagger2.annotations.EnableSwagger2;\r
+import org.apache.shiro.realm.Realm;\r
+import org.apache.shiro.realm.text.PropertiesRealm;\r
+import org.apache.shiro.realm.text.TextConfigurationRealm;\r
+import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;\r
+import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;\r
+import org.springframework.context.annotation.Bean;\r
+import org.onap.aaf.cadi.shiro.AAFRealm;\r
 \r
 @SpringBootApplication\r
 @EnableSwagger2\r
@@ -34,4 +41,34 @@ public class App {
   public static void main(String[] args) throws Exception {\r
     SpringApplication.run(App.class, args);\r
   }\r
+\r
+  @Bean\r
+  public Realm realm() {\r
+\r
+    // If cadi prop files is not defined use local properties realm\r
+    // src/main/resources/shiro-users.properties\r
+    if ("none".equals(System.getProperty("cadi_prop_files", "none"))) {\r
+      PropertiesRealm realm = new PropertiesRealm();\r
+      return realm;\r
+    } else {\r
+      AAFRealm realm = new AAFRealm();\r
+      return realm;\r
+    }\r
+\r
+  }\r
+\r
+  @Bean\r
+  public ShiroFilterChainDefinition shiroFilterChainDefinition() {\r
+    DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();\r
+\r
+    // if cadi prop files is not set disable authentication\r
+    if ("none".equals(System.getProperty("cadi_prop_files", "none"))) {\r
+      chainDefinition.addPathDefinition("/**", "anon");\r
+    } else {\r
+      chainDefinition.addPathDefinition("/**", "authcBasic, rest[org.onap.sdnc:odl-api]");\r
+    }\r
+\r
+    return chainDefinition;\r
+  }\r
+\r
 }\r
diff --git a/sliapi/springboot/src/main/resources/shiro-users.properties b/sliapi/springboot/src/main/resources/shiro-users.properties
new file mode 100644 (file)
index 0000000..df4b1ae
--- /dev/null
@@ -0,0 +1,3 @@
+user.admin = Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U,service
+role.service = odl-api:*
+
diff --git a/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/AppTest.java b/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/AppTest.java
new file mode 100644 (file)
index 0000000..c5f452c
--- /dev/null
@@ -0,0 +1,39 @@
+package org.onap.ccsdk.sli.core.sliapi.springboot;
+
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.realm.text.PropertiesRealm;
+import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.aaf.cadi.shiro.AAFRealm;
+
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+public class AppTest {
+
+    App app;
+
+    @Before
+    public void setUp() throws Exception {
+        app = new App();
+    }
+
+    @Test
+    public void realm() {
+        Realm realm = app.realm();
+        assertTrue(realm instanceof PropertiesRealm);
+
+
+    }
+
+    @Test
+    public void shiroFilterChainDefinition() {
+        ShiroFilterChainDefinition chainDefinition = app.shiroFilterChainDefinition();
+        Map<String, String> chainMap = chainDefinition.getFilterChainMap();
+        assertEquals("anon", chainMap.get("/**"));
+
+
+    }
+}
\ No newline at end of file