Skip to content

Commit e2d082c

Browse files
committed
added doc how to use CSRF
1 parent 19e8d05 commit e2d082c

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ public class ExampleApplication {
306306

307307
![History](screenshots/history-screen.png)
308308

309+
## Spring Boot CSRF config for the UI
310+
311+
Axios should work with the following spring config out of the box with csrf:
312+
313+
```java
314+
@Bean
315+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
316+
http
317+
.httpBasic(org.springframework.security.config.Customizer.withDefaults())
318+
.csrf(c ->
319+
c.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
320+
.csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler())
321+
);
322+
return http.build();
323+
}
324+
```
325+
309326
# Alternatives
310327

311328
- quartz

example/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# UI login
2+
3+
- url: http://localhost:8080/task-ui
4+
- user: admin
5+
- password: admin

example/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<artifactId>spring-boot-starter-web</artifactId>
3838
</dependency>
3939

40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-security</artifactId>
43+
</dependency>
44+
4045
<dependency>
4146
<groupId>uk.co.jemos.podam</groupId>
4247
<artifactId>podam</artifactId>

example/src/main/java/org/sterl/spring/example_app/ExampleApplication.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
import org.springframework.context.annotation.Bean;
99
import org.springframework.data.web.config.EnableSpringDataWebSupport;
1010
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode;
11+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
12+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
13+
import org.springframework.security.core.userdetails.User;
14+
import org.springframework.security.core.userdetails.UserDetails;
15+
import org.springframework.security.core.userdetails.UserDetailsService;
16+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
17+
import org.springframework.security.web.SecurityFilterChain;
18+
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
19+
import org.springframework.security.web.csrf.CsrfTokenRequestAttributeHandler;
1120
import org.springframework.transaction.support.TransactionTemplate;
1221
import org.sterl.spring.persistent_tasks.EnableSpringPersistentTasks;
1322
import org.sterl.spring.persistent_tasks.scheduler.SchedulerService;
@@ -16,6 +25,7 @@
1625
import org.sterl.spring.persistent_tasks.trigger.TriggerService;
1726
import org.sterl.spring.persistent_tasks_ui.EnableSpringPersistentTasksUI;
1827

28+
@EnableWebSecurity
1929
@SpringBootApplication
2030
@EnableSpringPersistentTasks
2131
@EnableSpringPersistentTasksUI
@@ -53,4 +63,25 @@ SchedulerService schedulerB(
5363
return new SchedulerService("schedulerB", triggerService,
5464
new TaskExecutorComponent(triggerService, 7), editSchedulerStatus, trx);
5565
}
66+
67+
@Bean
68+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
69+
http
70+
.httpBasic(org.springframework.security.config.Customizer.withDefaults())
71+
.csrf(c ->
72+
c.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
73+
.csrfTokenRequestHandler(new CsrfTokenRequestAttributeHandler())
74+
);
75+
return http.build();
76+
}
77+
78+
@Bean
79+
UserDetailsService users() {
80+
UserDetails admin = User.builder()
81+
.username("admin")
82+
.password("admin")
83+
.roles("ADMIN")
84+
.build();
85+
return new InMemoryUserDetailsManager(admin);
86+
}
5687
}

0 commit comments

Comments
 (0)