From 35d9348ab67b6dc3c8e90a2a479f75fcd0af9228 Mon Sep 17 00:00:00 2001 From: David Stilwell Date: Wed, 18 Mar 2020 10:02:50 -0400 Subject: Springboot integration with AAF 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 --- sliapi/springboot/README.md | 8 +++-- sliapi/springboot/pom.xml | 13 ++++++-- .../onap/ccsdk/sli/core/sliapi/springboot/App.java | 37 ++++++++++++++++++++ .../src/main/resources/shiro-users.properties | 3 ++ .../ccsdk/sli/core/sliapi/springboot/AppTest.java | 39 ++++++++++++++++++++++ 5 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 sliapi/springboot/src/main/resources/shiro-users.properties create mode 100644 sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/AppTest.java (limited to 'sliapi') diff --git a/sliapi/springboot/README.md b/sliapi/springboot/README.md index 3e47f341..38be1c2f 100644 --- a/sliapi/springboot/README.md +++ b/sliapi/springboot/README.md @@ -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 +``` diff --git a/sliapi/springboot/pom.xml b/sliapi/springboot/pom.xml index 8e889459..2a3fbcb0 100644 --- a/sliapi/springboot/pom.xml +++ b/sliapi/springboot/pom.xml @@ -18,6 +18,8 @@ org.onap.ccsdk.sli.core.sliapi.springboot.App + 1.5.0 + 2.1.13 @@ -26,8 +28,9 @@ swagger-annotations - org.springframework.boot - spring-boot-starter-web + org.apache.shiro + shiro-spring-boot-web-starter + ${shiro.version} org.springframework.boot @@ -39,6 +42,12 @@ org.springframework.boot spring-boot-starter-log4j2 + + org.onap.aaf.cadi + aaf-cadi-shiro + ${aaf-shiro-bundle.version} + + org.springframework.boot spring-boot-starter-test diff --git a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java index ed3ee044..2892430b 100644 --- a/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java +++ b/sliapi/springboot/src/main/java/org/onap/ccsdk/sli/core/sliapi/springboot/App.java @@ -24,6 +24,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import springfox.documentation.swagger2.annotations.EnableSwagger2; +import org.apache.shiro.realm.Realm; +import org.apache.shiro.realm.text.PropertiesRealm; +import org.apache.shiro.realm.text.TextConfigurationRealm; +import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition; +import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition; +import org.springframework.context.annotation.Bean; +import org.onap.aaf.cadi.shiro.AAFRealm; @SpringBootApplication @EnableSwagger2 @@ -34,4 +41,34 @@ public class App { public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } + + @Bean + public Realm realm() { + + // If cadi prop files is not defined use local properties realm + // src/main/resources/shiro-users.properties + if ("none".equals(System.getProperty("cadi_prop_files", "none"))) { + PropertiesRealm realm = new PropertiesRealm(); + return realm; + } else { + AAFRealm realm = new AAFRealm(); + return realm; + } + + } + + @Bean + public ShiroFilterChainDefinition shiroFilterChainDefinition() { + DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition(); + + // if cadi prop files is not set disable authentication + if ("none".equals(System.getProperty("cadi_prop_files", "none"))) { + chainDefinition.addPathDefinition("/**", "anon"); + } else { + chainDefinition.addPathDefinition("/**", "authcBasic, rest[org.onap.sdnc:odl-api]"); + } + + return chainDefinition; + } + } diff --git a/sliapi/springboot/src/main/resources/shiro-users.properties b/sliapi/springboot/src/main/resources/shiro-users.properties new file mode 100644 index 00000000..df4b1ae7 --- /dev/null +++ b/sliapi/springboot/src/main/resources/shiro-users.properties @@ -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 index 00000000..c5f452cb --- /dev/null +++ b/sliapi/springboot/src/test/java/org/onap/ccsdk/sli/core/sliapi/springboot/AppTest.java @@ -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 chainMap = chainDefinition.getFilterChainMap(); + assertEquals("anon", chainMap.get("/**")); + + + } +} \ No newline at end of file -- cgit 1.2.3-korg