Skip to content

Commit ff35749

Browse files
felipe-gdrEugen
authored andcommitted
BAEL-127: @PreFilter and @PostFilter annotations (eugenp#898)
* BAEL-127: simple app with filters * removed data rest dependency, final adjustments * added first live test for the rest api * move filters code to new module * moved to root of module, create service layer, standard pom
1 parent afa7fed commit ff35749

12 files changed

Lines changed: 467 additions & 0 deletions

File tree

‎spring-security-core/.gitignore‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

‎spring-security-core/README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## @PreFilter and @PostFilter annotations
2+
3+
### Build the Project ###
4+
5+
```
6+
mvn clean install
7+
```

‎spring-security-core/pom.xml‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>spring-security-core</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
8+
<name>spring-security-core</name>
9+
<packaging>war</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>1.4.2.RELEASE</version>
15+
<relativePath />
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-data-jpa</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-devtools</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-security</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.h2database</groupId>
37+
<artifactId>h2</artifactId>
38+
<scope>runtime</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.security</groupId>
47+
<artifactId>spring-security-test</artifactId>
48+
</dependency>
49+
</dependencies>
50+
51+
52+
<build>
53+
<finalName>spring-security-core</finalName>
54+
<resources>
55+
<resource>
56+
<directory>src/main/resources</directory>
57+
<filtering>true</filtering>
58+
</resource>
59+
</resources>
60+
61+
<plugins>
62+
63+
<plugin>
64+
<groupId>org.apache.maven.plugins</groupId>
65+
<artifactId>maven-compiler-plugin</artifactId>
66+
<configuration>
67+
<source>1.8</source>
68+
<target>1.8</target>
69+
<debug>true</debug>
70+
<debuglevel>source</debuglevel>
71+
</configuration>
72+
</plugin>
73+
74+
<plugin>
75+
<groupId>org.apache.maven.plugins</groupId>
76+
<artifactId>maven-war-plugin</artifactId>
77+
</plugin>
78+
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-surefire-plugin</artifactId>
82+
<configuration>
83+
<excludes>
84+
<exclude>**/*LiveTest.java</exclude>
85+
<exclude>**/*ManualTest.java</exclude>
86+
</excludes>
87+
<systemPropertyVariables>
88+
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
89+
</systemPropertyVariables>
90+
</configuration>
91+
</plugin>
92+
93+
</plugins>
94+
95+
</build>
96+
97+
<profiles>
98+
<profile>
99+
<id>live</id>
100+
<build>
101+
<plugins>
102+
103+
<plugin>
104+
<groupId>org.apache.maven.plugins</groupId>
105+
<artifactId>maven-surefire-plugin</artifactId>
106+
<executions>
107+
<execution>
108+
<phase>integration-test</phase>
109+
<goals>
110+
<goal>test</goal>
111+
</goals>
112+
<configuration>
113+
<excludes>
114+
<exclude>none</exclude>
115+
</excludes>
116+
<includes>
117+
<include>**/*LiveTest.java</include>
118+
</includes>
119+
<systemPropertyVariables>
120+
<webTarget>cargo</webTarget>
121+
</systemPropertyVariables>
122+
</configuration>
123+
</execution>
124+
</executions>
125+
</plugin>
126+
127+
</plugins>
128+
</build>
129+
</profile>
130+
</profiles>
131+
<properties>
132+
<!-- Spring -->
133+
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
134+
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
135+
136+
<!-- http -->
137+
<httpcore.version>4.4.5</httpcore.version>
138+
<httpclient.version>4.5.2</httpclient.version>
139+
140+
<!-- logging -->
141+
<org.slf4j.version>1.7.21</org.slf4j.version>
142+
<logback.version>1.1.7</logback.version> <!-- do not upgrade - see http://jira.qos.ch/browse/LOGBACK-851 -->
143+
144+
<!-- various -->
145+
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
146+
<jstl.version>1.2</jstl.version>
147+
<javax.servlet.version>3.1.0</javax.servlet.version>
148+
<jackson.version>2.8.5</jackson.version>
149+
150+
<!-- util -->
151+
<guava.version>19.0</guava.version>
152+
<commons-lang3.version>3.5</commons-lang3.version>
153+
154+
<!-- testing -->
155+
<org.hamcrest.version>1.3</org.hamcrest.version>
156+
<junit.version>4.12</junit.version>
157+
<mockito.version>1.10.19</mockito.version>
158+
159+
<rest-assured.version>2.9.0</rest-assured.version>
160+
161+
<!-- Maven plugins -->
162+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
163+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
164+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
165+
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
166+
167+
</properties>
168+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.app;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
9+
@SpringBootApplication
10+
@EnableJpaRepositories("org.baeldung.repository")
11+
@ComponentScan("org.baeldung")
12+
@EntityScan("org.baeldung.entity")
13+
public class App {
14+
public static void main(String[] args) {
15+
SpringApplication.run(App.class, args);
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.config;
2+
3+
import org.baeldung.entity.Task;
4+
import org.baeldung.repository.TaskRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.CommandLineRunner;
7+
import org.springframework.stereotype.Component;
8+
9+
@Component
10+
public class DatabaseLoader implements CommandLineRunner {
11+
12+
@Autowired
13+
private TaskRepository taskRepository;
14+
15+
@Override
16+
public void run(String... strings) throws Exception {
17+
this.taskRepository.save(new Task("Send a fax", "pam"));
18+
this.taskRepository.save(new Task("Print a document", "pam"));
19+
this.taskRepository.save(new Task("Answer the phone", "pam"));
20+
this.taskRepository.save(new Task("Call a client", "jim"));
21+
this.taskRepository.save(new Task("Organize a meeting", "michael"));
22+
}
23+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
6+
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
7+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10+
11+
@Configuration
12+
@EnableWebSecurity
13+
@EnableGlobalMethodSecurity(prePostEnabled = true)
14+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
15+
16+
@Override
17+
protected void configure(HttpSecurity http) throws Exception {
18+
http
19+
.authorizeRequests()
20+
.antMatchers("/css/**", "/js/**", "/loggedout").permitAll()
21+
.anyRequest().authenticated()
22+
.and()
23+
.httpBasic()
24+
.and()
25+
.logout().disable()
26+
.csrf().disable();
27+
}
28+
29+
@Autowired
30+
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
31+
auth
32+
.inMemoryAuthentication()
33+
.withUser("jim").password("jim").roles("USER")
34+
.and()
35+
.withUser("pam").password("pam").roles("USER")
36+
.and()
37+
.withUser("michael").password("michael").roles("MANAGER");
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.controller;
2+
3+
import org.baeldung.entity.Task;
4+
import org.baeldung.service.TaskService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
12+
@Controller
13+
@RequestMapping("api/tasks")
14+
public class TaskController {
15+
16+
@Autowired
17+
private TaskService taskService;
18+
19+
@RequestMapping(method = RequestMethod.GET)
20+
public ResponseEntity<Iterable<Task>> findAllTasks() {
21+
Iterable<Task> tasks = taskService.findAll();
22+
23+
return ResponseEntity.ok().body(tasks);
24+
}
25+
26+
@RequestMapping(method = RequestMethod.POST, consumes = "application/json")
27+
public ResponseEntity<Iterable<Task>> addTasks(@RequestBody Iterable<Task> newTasks) {
28+
Iterable<Task> tasks = taskService.save(newTasks);
29+
30+
return ResponseEntity.ok().body(tasks);
31+
}
32+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.baeldung.entity;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
@Entity
8+
public class Task {
9+
private @Id @GeneratedValue Long id;
10+
private String description;
11+
12+
private String assignee;
13+
14+
public Task() {
15+
}
16+
17+
public Task(String description, String assignee) {
18+
this.description = description;
19+
this.assignee = assignee;
20+
}
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getDescription() {
31+
return description;
32+
}
33+
34+
public void setDescription(String description) {
35+
this.description = description;
36+
}
37+
38+
public String getAssignee() {
39+
return assignee;
40+
}
41+
42+
public void setAssignee(String assignee) {
43+
this.assignee = assignee;
44+
}
45+
46+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.repository;
2+
3+
import org.baeldung.entity.Task;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
public interface TaskRepository extends CrudRepository<Task, Long> {
7+
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.service;
2+
3+
import org.baeldung.entity.Task;
4+
import org.baeldung.repository.TaskRepository;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.security.access.prepost.PostFilter;
7+
import org.springframework.security.access.prepost.PreFilter;
8+
import org.springframework.stereotype.Service;
9+
10+
@Service
11+
public class TaskService {
12+
13+
@Autowired
14+
private TaskRepository taskRepository;
15+
16+
@PostFilter("hasRole('MANAGER') or filterObject.assignee == authentication.name")
17+
public Iterable<Task> findAll() {
18+
return taskRepository.findAll();
19+
}
20+
21+
@PreFilter("hasRole('MANAGER') or filterObject.assignee == authentication.name")
22+
public Iterable<Task> save(Iterable<Task> entities) {
23+
return taskRepository.save(entities);
24+
}
25+
26+
}

0 commit comments

Comments
 (0)