Skip to content

Commit 80ae7f1

Browse files
committed
Added Spring Security and Spring Session to showcase basic security.
We're using a HTTP header based strategy to generate authentication tokens on authorization requests. We use the already used HSQLDB as temporary session store.
1 parent fc7aebf commit 80ae7f1

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

server/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/ma
167167
<scope>runtime</scope>
168168
</dependency>
169169

170+
<!-- Security -->
171+
172+
<dependency>
173+
<groupId>org.springframework.boot</groupId>
174+
<artifactId>spring-boot-starter-security</artifactId>
175+
</dependency>
176+
177+
<dependency>
178+
<groupId>org.springframework.session</groupId>
179+
<artifactId>spring-session-jdbc</artifactId>
180+
</dependency>
181+
170182
<!-- Misc -->
171183

172184
<dependency>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2015-2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.odrotbohm.restbucks;
17+
18+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
22+
import org.springframework.security.web.SecurityFilterChain;
23+
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
24+
25+
/**
26+
* Web security configuration enabling:
27+
* <ol>
28+
* <li>HTTP Basic authentication with Spring Security</li>
29+
* <li>Configuring Spring Session to expose the session using an HTTP header.</li>
30+
* </ol>
31+
*
32+
* @author Oliver Drotbohm
33+
*/
34+
@Configuration
35+
@ConditionalOnWebApplication
36+
class SecurityConfiguration {
37+
38+
/**
39+
* Configures a the HTTP session to be exposed via an HTTP header.
40+
*/
41+
@Bean
42+
public HeaderHttpSessionIdResolver sessionStrategy() {
43+
return HeaderHttpSessionIdResolver.xAuthToken();
44+
}
45+
46+
@Bean
47+
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
48+
49+
return http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
50+
.httpBasic(it -> it.realmName("Spring RESTBucks"))
51+
.build();
52+
}
53+
}

server/src/main/resources/application.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ management.otlp.metrics.export.step=5s
2020

2121
spring.threads.virtual.enabled=true
2222
spring.aot.repositories.enabled=true
23+
24+
# Security
25+
spring.security.user.password=password
26+
spring.session.jdbc.initialize-schema=embedded
27+
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-hsqldb.sql
28+
spring.session.jdbc.table-name=SPRING_SESSION

0 commit comments

Comments
 (0)