From dce2994f1fe4bc9d0dcb25b584910dd764275eb9 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 7 Jul 2014 19:37:45 +0200 Subject: [PATCH] Prepare security integration examples. --- jpa/security/pom.xml | 39 +++++++++++++++ .../springdata/jpa/security/Customer.java | 36 ++++++++++++++ .../jpa/security/CustomerRepository.java | 27 ++++++++++ .../jpa/security/SecurityConfiguration.java | 31 ++++++++++++ .../SecurityEvaluationContextExtension.java | 49 +++++++++++++++++++ .../security/SecurityIntegrationTests.java | 33 +++++++++++++ jpa/security/src/test/resources/logback.xml | 18 +++++++ 7 files changed, 233 insertions(+) create mode 100644 jpa/security/pom.xml create mode 100644 jpa/security/src/main/java/example/springdata/jpa/security/Customer.java create mode 100644 jpa/security/src/main/java/example/springdata/jpa/security/CustomerRepository.java create mode 100644 jpa/security/src/main/java/example/springdata/jpa/security/SecurityConfiguration.java create mode 100644 jpa/security/src/main/java/example/springdata/jpa/security/SecurityEvaluationContextExtension.java create mode 100644 jpa/security/src/test/java/example/springdata/jpa/security/SecurityIntegrationTests.java create mode 100644 jpa/security/src/test/resources/logback.xml diff --git a/jpa/security/pom.xml b/jpa/security/pom.xml new file mode 100644 index 00000000..94e21e16 --- /dev/null +++ b/jpa/security/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + + + org.springframework.data.examples + spring-data-jpa-examples + 1.0.0.BUILD-SNAPSHOT + + + spring-data-jpa-security + Spring Data JPA - Spring Security integration + + + + + + org.springframework.data + spring-data-jpa + 1.7.0.DATAJPA-564-SNAPSHOT + + + + org.springframework.data + spring-data-commons + 1.9.0.DATACMNS-533-SNAPSHOT + + + + + + + + org.springframework.boot + spring-boot-starter-security + + + + \ No newline at end of file diff --git a/jpa/security/src/main/java/example/springdata/jpa/security/Customer.java b/jpa/security/src/main/java/example/springdata/jpa/security/Customer.java new file mode 100644 index 00000000..2639db5d --- /dev/null +++ b/jpa/security/src/main/java/example/springdata/jpa/security/Customer.java @@ -0,0 +1,36 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jpa.security; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Oliver Gierke + */ +@Entity +public class Customer { + + @Id @GeneratedValue Long id; + String firstname, lastname; + + public Customer(String firstname, String lastname) { + + this.firstname = firstname; + this.lastname = lastname; + } +} diff --git a/jpa/security/src/main/java/example/springdata/jpa/security/CustomerRepository.java b/jpa/security/src/main/java/example/springdata/jpa/security/CustomerRepository.java new file mode 100644 index 00000000..8bf14c4e --- /dev/null +++ b/jpa/security/src/main/java/example/springdata/jpa/security/CustomerRepository.java @@ -0,0 +1,27 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jpa.security; + +import org.springframework.data.repository.CrudRepository; + +/** + * Repository to manage {@link Customer} instances. + * + * @author Oliver Gierke + */ +public interface CustomerRepository extends CrudRepository { + +} diff --git a/jpa/security/src/main/java/example/springdata/jpa/security/SecurityConfiguration.java b/jpa/security/src/main/java/example/springdata/jpa/security/SecurityConfiguration.java new file mode 100644 index 00000000..5135db6b --- /dev/null +++ b/jpa/security/src/main/java/example/springdata/jpa/security/SecurityConfiguration.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jpa.security; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport; + +@Configuration +@EnableAutoConfiguration +class SecurityConfiguration { + + @Bean + EvaluationContextExtensionSupport securityExtension() { + return new SecurityEvaluationContextExtension(); + } +} diff --git a/jpa/security/src/main/java/example/springdata/jpa/security/SecurityEvaluationContextExtension.java b/jpa/security/src/main/java/example/springdata/jpa/security/SecurityEvaluationContextExtension.java new file mode 100644 index 00000000..7b2f48e9 --- /dev/null +++ b/jpa/security/src/main/java/example/springdata/jpa/security/SecurityEvaluationContextExtension.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jpa.security; + +import java.util.Collections; +import java.util.Map; + +import org.springframework.data.repository.query.spi.EvaluationContextExtension; +import org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport; +import org.springframework.security.core.context.SecurityContextHolder; + +/** + * {@link EvaluationContextExtension} to expose Spring Security's principal via a SpEL property. + * + * @author Oliver Gierke + */ +class SecurityEvaluationContextExtension extends EvaluationContextExtensionSupport { + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.spi.EvaluationContextExtension#getExtensionId() + */ + @Override + public String getExtensionId() { + return "security"; + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.query.spi.EvaluationContextExtensionSupport#getProperties() + */ + @Override + public Map getProperties() { + return Collections.singletonMap("principal", SecurityContextHolder.getContext().getAuthentication().getPrincipal()); + } +} diff --git a/jpa/security/src/test/java/example/springdata/jpa/security/SecurityIntegrationTests.java b/jpa/security/src/test/java/example/springdata/jpa/security/SecurityIntegrationTests.java new file mode 100644 index 00000000..2ad10155 --- /dev/null +++ b/jpa/security/src/test/java/example/springdata/jpa/security/SecurityIntegrationTests.java @@ -0,0 +1,33 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.springdata.jpa.security; + +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * Integration test to show the usage of Java 8 date time APIs with Spring Data JPA auditing. + * + * @author Oliver Gierke + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SecurityConfiguration.class) +@Transactional +public class SecurityIntegrationTests { + +} diff --git a/jpa/security/src/test/resources/logback.xml b/jpa/security/src/test/resources/logback.xml new file mode 100644 index 00000000..b48bf208 --- /dev/null +++ b/jpa/security/src/test/resources/logback.xml @@ -0,0 +1,18 @@ + + + + + + %d %5p %40.40c:%4L - %m%n + + + + + + + + + + + + \ No newline at end of file