Skip to content

Commit

Permalink
Polish Security Sample Code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jxblum committed Aug 7, 2020
1 parent 7ab2d2d commit 6635a8f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 51 deletions.
Expand Up @@ -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<Long, Customer> 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[]
Expand Up @@ -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[]
Expand Up @@ -17,7 +17,9 @@

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;
Expand All @@ -26,48 +28,49 @@
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 <a href="http://shiro.apache.org/">Apache Shiro</a>
* @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[]

@@ -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
spring.data.gemfire.security.password = p@55w0rd
spring.data.gemfire.security.ssl.keystore.password=s3cr3t
@@ -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
role.viewer = DATA:WRITE

0 comments on commit 6635a8f

Please sign in to comment.