From bb8c9a5b8115d05c09b3a8034fc5a7de4b3416de Mon Sep 17 00:00:00 2001 From: John Blum Date: Fri, 7 Aug 2020 15:02:13 -0700 Subject: [PATCH] Polish Security Sample Code. Resolves gh-81. --- .../BootGeodeSecurityClientApplication.java | 54 ++++++++++++------- .../client/controller/SecurityController.java | 13 ++--- .../BootGeodeSecurityServerApplication.java | 47 ++++++++-------- .../src/main/resources/application.properties | 5 +- .../src/main/resources/shiro.properties | 4 +- 5 files changed, 72 insertions(+), 51 deletions(-) diff --git a/spring-geode-samples/boot/security/src/main/java/example/app/security/client/BootGeodeSecurityClientApplication.java b/spring-geode-samples/boot/security/src/main/java/example/app/security/client/BootGeodeSecurityClientApplication.java index c4639d78..ef62621f 100644 --- a/spring-geode-samples/boot/security/src/main/java/example/app/security/client/BootGeodeSecurityClientApplication.java +++ b/spring-geode-samples/boot/security/src/main/java/example/app/security/client/BootGeodeSecurityClientApplication.java @@ -15,55 +15,71 @@ */ package example.app.security.client; -import example.app.security.client.model.Customer; -import org.apache.geode.cache.Region; import org.apache.geode.cache.client.ClientCache; + import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.data.gemfire.config.annotation.EnableClusterConfiguration; +import org.springframework.data.gemfire.GemfireTemplate; import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions; +import org.springframework.geode.config.annotation.EnableClusterAware; + +import example.app.security.client.model.Customer; /** - * The {@link BootGeodeSecurityClientApplication} class is a Spring Boot, Apache Geode {@link ClientCache} - * application that configures security. + * A Spring Boot, Apache Geode {@link ClientCache} application that configures security. * * @author Patrick Johnson + * @author John Blum * @see org.apache.geode.cache.client.ClientCache - * @see org.springframework.boot.SpringApplication + * @see org.springframework.boot.ApplicationRunner * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.builder.SpringApplicationBuilder * @see org.springframework.context.annotation.Bean * @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication - * @since 1.3.0 + * @see org.springframework.geode.config.annotation.EnableClusterAware + * @since 1.4.0 */ // tag::class[] @SpringBootApplication -@EnableClusterConfiguration -@EnableEntityDefinedRegions +@EnableClusterAware +@EnableEntityDefinedRegions(basePackageClasses = Customer.class) public class BootGeodeSecurityClientApplication { + public static void main(String[] args) { + new SpringApplicationBuilder(BootGeodeSecurityClientApplication.class) - .web(WebApplicationType.SERVLET) - .build() - .run(args); + .web(WebApplicationType.SERVLET) + .build() + .run(args); } + // tag::runner[] @Bean - ApplicationRunner runner(@Qualifier("Customers") Region customers) { + ApplicationRunner runner(@Qualifier("customersTemplate") GemfireTemplate customersTemplate) { + return args -> { - customers.put(2L, Customer.newCustomer(2L, "William Evans")); - System.out.println(String.format("Successfully wrote data to region %s", customers.getName())); + + Customer williamEvans = Customer.newCustomer(2L, "William Evans"); + + customersTemplate.put(williamEvans.getId(), williamEvans); + + System.err.printf("Successfully put [%1$s] in Region [%2$s]%n", + williamEvans, customersTemplate.getRegion().getName()); try { - System.out.println(String.format("Attempting to read data from region %s", customers.getName())); - customers.get(2L); - } catch (Exception e) { - System.out.println(String.format("Read failed because \"%s\"", e.getCause().getMessage())); + System.err.printf("Attempting to read from Region [%s]...%n", customersTemplate.getRegion().getName()); + customersTemplate.get(2L); + } + catch (Exception cause) { + System.err.println(String.format("Read failed because \"%s\"", + cause.getCause().getCause().getMessage())); } }; } + // end::runner[] } // end::class[] diff --git a/spring-geode-samples/boot/security/src/main/java/example/app/security/client/controller/SecurityController.java b/spring-geode-samples/boot/security/src/main/java/example/app/security/client/controller/SecurityController.java index 2017cef3..f6ad2d6e 100644 --- a/spring-geode-samples/boot/security/src/main/java/example/app/security/client/controller/SecurityController.java +++ b/spring-geode-samples/boot/security/src/main/java/example/app/security/client/controller/SecurityController.java @@ -13,32 +13,33 @@ * or implied. See the License for the specific language governing * permissions and limitations under the License. */ - package example.app.security.client.controller; -import example.app.security.client.BootGeodeSecurityClientApplication; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; +import example.app.security.client.BootGeodeSecurityClientApplication; + /** - * The {@link SecurityController} class is a RestController used by {@link BootGeodeSecurityClientApplication} + * A Spring {@link RestController} used by {@link BootGeodeSecurityClientApplication}. * * @author Patrick Johnson * @see org.springframework.web.bind.annotation.RestController - * @since 1.3.0 + * @since 1.4.0 */ // tag::class[] @RestController public class SecurityController { @Autowired - Environment env; + private Environment environment; @GetMapping("/message") public String getMessage() { - return "I'm using SSL with this keystore: " + env.getProperty("spring.data.gemfire.security.ssl.keystore"); + return String.format("I'm using SSL with this Keystore: %s", + this.environment.getProperty("spring.data.gemfire.security.ssl.keystore")); } } // end::class[] diff --git a/spring-geode-samples/boot/security/src/main/java/example/app/security/server/BootGeodeSecurityServerApplication.java b/spring-geode-samples/boot/security/src/main/java/example/app/security/server/BootGeodeSecurityServerApplication.java index af2d6ada..4badcc20 100644 --- a/spring-geode-samples/boot/security/src/main/java/example/app/security/server/BootGeodeSecurityServerApplication.java +++ b/spring-geode-samples/boot/security/src/main/java/example/app/security/server/BootGeodeSecurityServerApplication.java @@ -17,7 +17,9 @@ package example.app.security.server; import org.apache.geode.cache.server.CacheServer; import org.apache.geode.internal.security.shiro.GeodePermissionResolver; + import org.apache.shiro.realm.text.PropertiesRealm; + import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -26,48 +28,49 @@ import org.springframework.data.gemfire.config.annotation.CacheServerApplication import org.springframework.data.gemfire.config.annotation.EnableSecurity; /** - * The {@link BootGeodeSecurityServerApplication} class is a Spring Boot, Apache Geode {@literal peer} - * {@link CacheServer} application serving cache clients. + * A Spring Boot, Apache Geode {@literal peer} {@link CacheServer} application serving cache clients. * - * This Apache Geode peer member server configures security. + * This Apache Geode {@link CacheServer} and {@literal peer member} configures Apache Geode Security + * using Apache Shiro. * * @author Patrick Johnson + * @author John Blum * @see org.apache.geode.cache.Cache - * @see org.apache.geode.cache.Region * @see org.apache.geode.cache.server.CacheServer + * @see org.apache.shiro.realm.text.PropertiesRealm * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @see org.springframework.boot.builder.SpringApplicationBuilder * @see org.springframework.context.annotation.Bean * @see org.springframework.data.gemfire.config.annotation.CacheServerApplication - * @since 1.3.0 + * @see org.springframework.data.gemfire.config.annotation.EnableSecurity + * @see Apache Shiro + * @since 1.4.0 */ // tag::class[] @SpringBootApplication +@CacheServerApplication +@EnableSecurity public class BootGeodeSecurityServerApplication { public static void main(String[] args) { + new SpringApplicationBuilder(BootGeodeSecurityServerApplication.class) - .web(WebApplicationType.NONE) - .build() - .run(args); + .web(WebApplicationType.NONE) + .build() + .run(args); } - @CacheServerApplication - @EnableSecurity - static class GeodeServerConfig { + // tag::realm[] + @Bean + PropertiesRealm shiroRealm() { - // tag::realm[] - @Bean - PropertiesRealm shiroRealm() { + PropertiesRealm propertiesRealm = new PropertiesRealm(); - PropertiesRealm propertiesRealm = new PropertiesRealm(); + propertiesRealm.setResourcePath("classpath:shiro.properties"); + propertiesRealm.setPermissionResolver(new GeodePermissionResolver()); - propertiesRealm.setResourcePath("classpath:shiro.properties"); - propertiesRealm.setPermissionResolver(new GeodePermissionResolver()); - - return propertiesRealm; - } - // end::realm[] + return propertiesRealm; } + // end::realm[] } // end::class[] - diff --git a/spring-geode-samples/boot/security/src/main/resources/application.properties b/spring-geode-samples/boot/security/src/main/resources/application.properties index 37911852..40cf9f13 100644 --- a/spring-geode-samples/boot/security/src/main/resources/application.properties +++ b/spring-geode-samples/boot/security/src/main/resources/application.properties @@ -1,5 +1,6 @@ # Spring Boot client application.properties -spring.data.gemfire.security.ssl.keystore.password=s3cr3t +spring.data.gemfire.management.use-http=false spring.data.gemfire.security.username = jdoe -spring.data.gemfire.security.password = p@55w0rd \ No newline at end of file +spring.data.gemfire.security.password = p@55w0rd +spring.data.gemfire.security.ssl.keystore.password=s3cr3t diff --git a/spring-geode-samples/boot/security/src/main/resources/shiro.properties b/spring-geode-samples/boot/security/src/main/resources/shiro.properties index 356746a2..83bb3b15 100644 --- a/spring-geode-samples/boot/security/src/main/resources/shiro.properties +++ b/spring-geode-samples/boot/security/src/main/resources/shiro.properties @@ -1,3 +1,3 @@ -# assign user jdoe the role fo viewer and give him DATA:WRITE permissions +# Assign user 'jdoe' the role of 'viewer' having 'DATA:WRITE' permissions. user.jdoe = p@55w0rd, viewer -role.viewer = DATA:WRITE \ No newline at end of file +role.viewer = DATA:WRITE