cloud-native-microservices-security
  • Introduction
  • Introduction
    • Requirements and Setup
    • Demo Application Architecture
  • Hands-On Labs
    • 1.Security via Spring Boot Auto-Configuration
    • 2.Customized Authentication
    • 3.Mutual TLS (MTLS)
    • 4.Authorization
    • 5.Automated Testing
    • 6.Kubernetes Security
      • 6.1.Docker as Root
      • 6.2.Docker as NonRoot
      • 6.3.Kubernetes Deployment
      • 6.4.Kubernetes Pod Security Context
      • 6.5.Kubernetes Pod Security Policies
  • Bonus Labs
    • CSRF Attack Demo
    • Web Authn
Powered by GitBook
On this page
  • Add Dependencies
  • Login
  • Common Security Problems
  • Logout

Was this helpful?

  1. Hands-On Labs

1.Security via Spring Boot Auto-Configuration

PreviousDemo Application ArchitectureNext2.Customized Authentication

Last updated 5 years ago

Was this helpful?

In the first step we start quite easy by just adding the spring boot starter dependency for spring security.

NOTE: Please start into the workshop using the project lab1/library-server.

Add Dependencies

We just need to add the following two dependencies to the build.gradle file of the initial application (lab1/library-server).

dependencies {
    ...
    implementation 'org.springframework.boot:spring-boot-starter-security'
    ...
    testImplementation 'org.springframework.security:spring-security-test'
}

build.gradle

Please start the application by running the class com.example.libraryserver.LibraryServerApplication.

Login

Spring Security 5 added a nicer auto-generated login form (build with bootstrap library).

Autogenerated login formular

If you browse to then you will notice that a login form appears in the browser window.

TIP: But wait - what are the credentials for a user to log in?

With spring security auto-configured by spring boot the credentials are as follows:

  • Username=user

  • Password=

INFO 18465 --- [  restartedMain] UserDetailsServiceAutoConfiguration :
Using default security password: ded10c78-0b2f-4ae8-89fe-c267f9a29e1d

console log

After giving the correct credentials you should see the main screen of the library application.

Auto-generating the password on each application start is not really usable for serious applications. Later we will use the persistent user data for login. But for now Spring Security provides an easy way to set a static password using the application.yml file:

spring:
  application:
    name: library-server
  jpa:
    open-in-view: false
  jackson:
    default-property-inclusion: non_null
  security:
    user:
      password: secret

application.yml

To set the password to the value secret just add the last 3 lines above to the existing entries in the application.yml file.

After restarting the application you can now login using user/secret.

Common Security Problems

Additionally spring security improved the security of the web application automatically for:

  • Session Fixation is an attack that permits an attacker to hijack a valid user session. +

Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Expires: 0
Pragma: no-cache
Referrer-Policy: no-referrer
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1 ; mode=block

default security response headers

Logout

This concludes the first step.

NOTE: You find the completed code in project lab1/library-server-complete.

Now let's proceed to next step and start with customizing the authentication part.

main screen

As you can see, if Spring Security is on the classpath, then the web application is secured by default. auto-configured basic authentication and form based authentication for all web endpoints.

This also applies to all actuator endpoints like . All monitoring web endpoints can now only be accessed with an authenticated user. See for details.

:

If you want to learn more about this please read the

: Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated. + If you want to know what CSRF really is and how to mitigate this attack please consult

: This automatically adds all recommended security response headers to all http responses. You can find more information about this topic in the

You can check the response headers configuration on .

Spring security 5 also added a bit more user friendly logout functionality out of the box. If you direct your browser to you will see the following dialog on the screen.

Autogenerated logout formular

Spring boot
localhost:9090/library/actuator/health
Actuator Security
Session Fixation
Session Fixation page at OWASP
Cross Site Request Forgery (CSRF)
CSRF attack description at OWASP
Default Security Headers
OWASP Secure Headers Project
Security Headers
localhost:9090/library/logout
localhost:9090/library
SecureHeaders
LoginForm
LogoutForm
MainScreen