#107 - Polishing.

Switched to Asciidoc for README. Renamed ApplicationConfiguration to Application. Switched to a more simple package name. Some polishing in the test cases (removed unnecessary injections).

Original pull request: #108.
This commit is contained in:
Oliver Gierke
2015-07-16 08:20:53 +02:00
parent d95ed268b2
commit 8d77a9a37e
7 changed files with 27 additions and 41 deletions

View File

@@ -1,18 +1,18 @@
# Spring Data MongoDB - Spring Security integration.
= Spring Data MongoDB - Spring Security integration.
This project contains samples of the Spring Security integration in Spring Data (MongoDB).
## Support for SpEL Expression based filtering
== Support for SpEL expression based filtering
```java
[source, java]
----
public interface PersonRepository extends CrudRepository<Person, String> {
@Override
List<Person> findAll();
//Custom Query method with filtering based on Spring Security context information
// Custom query method with filtering based on Spring Security context information
@Query("{id: ?#{ hasRole('ROLE_ADMIN') ? {$exists:true} : principal.id}}")
List<Person> findAllForCurrentUserById();
}
```
----

View File

@@ -11,7 +11,12 @@
<artifactId>spring-data-mongodb-security</artifactId>
<name>Spring Data MongoDB - Spring Security Integration</name>
<properties>
<spring-data-releasetrain.version>Gosling-BUILD-SNAPSHOT</spring-data-releasetrain.version>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>spring-data-mongodb-utils</artifactId>
@@ -19,21 +24,16 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.8.0.DATAMONGO-1244-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-data</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.people;
package example.people;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@@ -28,12 +28,8 @@ import org.springframework.security.data.repository.query.SecurityEvaluationCont
* @author Thomas Darimont
*/
@SpringBootApplication
class ApplicationConfiguration {
class Application {
public @Bean LoggingEventListener mongoEventListener() {
return new LoggingEventListener();
}
@Bean
public EvaluationContextExtension securityExtension() {
return new SecurityEvaluationContextExtension();

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.people;
package example.people;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.data.annotation.Id;
/**
* An entity to represent a Person.
* An entity to represent a {@link Person}.
*
* @author Thomas Darimont
*/

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.people;
package example.people;
import java.util.List;
@@ -25,9 +25,7 @@ import org.springframework.data.repository.CrudRepository;
*
* @author Thomas Darimont
*/
public interface PersonRepository extends CrudRepository<Person, String> {
List<Person> findAll();
interface PersonRepository extends CrudRepository<Person, String> {
@Query("{id: ?#{ hasRole('ROLE_ADMIN') ? {$exists:true} : principal.id}}")
List<Person> findAllForCurrentUserById();

View File

@@ -13,11 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.mongodb.people;
package example.people;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import example.springdata.mongodb.util.RequiresMongoDB;
import java.util.Collections;
import java.util.List;
@@ -28,14 +29,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import example.springdata.mongodb.util.RequiresMongoDB;
/**
* Integration test for {@link PersonRepository}.
*
@@ -43,13 +41,12 @@ import example.springdata.mongodb.util.RequiresMongoDB;
* @authot Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
@SpringApplicationConfiguration(classes = Application.class)
public class PersonRepositoryIntegrationTest {
@ClassRule public static RequiresMongoDB mongodbAvailable = RequiresMongoDB.anyVersion();
@Autowired PersonRepository repository;
@Autowired MongoOperations operations;
Person dave, oliver, carter, admin;
@@ -66,11 +63,11 @@ public class PersonRepositoryIntegrationTest {
@Test
public void nonAdminCallingShouldReturnOnlyItSelfAsPerson() throws Exception {
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(dave, "x"));
List<Person> persons = repository.findAllForCurrentUserById();
assertThat(persons, hasSize(1));
assertThat(persons, contains(dave));
}
@@ -83,7 +80,7 @@ public class PersonRepositoryIntegrationTest {
SecurityContextHolder.getContext().setAuthentication(auth);
List<Person> persons = repository.findAllForCurrentUserById();
assertThat(persons, hasSize(4));
assertThat(persons, containsInAnyOrder(admin, dave, carter, oliver));
}

View File

@@ -1,5 +0,0 @@
/**
* Package showing usage of Spring Data MongoDB Repositories with Java 8.
*/
package example.springdata.mongodb.people;