r/SpringBoot • • Oct 18 '21

My understanding of Spring Security - Someone clarify any misunderstandings?

Hi. I've spent a few days reading about Spring Security & practicing it in a simple Spring Boot app. I wanted to just write out my understanding of it, and see if I am on the right track, and receive feedback and any clarifications.

So all Spring Security is is a framework that adds a filter, specifically the DelegatingFilterProxy. Spring Security will "filter" incoming requests made to your servlet, in other words, any route that is defined & implemented by your Rest Controller.

So adding Spring Security will immediately protect all routes, e.g: /api/users/.

Spring Security has several interfaces aka "contracts" and each "contract" is responsible for one thing only.

We have the AuthenticationProvider, which can be set by overriding

@Override
    protected void configure(AuthenticationManagerBuilder authenticationManagerBuilder) {
        authenticationManagerBuilder.authenticationProvider(authenticationProvider);
    }

AuthenticationProvider is the "contract" that is responsible for implementing authentication logic. An example would be validating the password sent with the hashed password for the user.

Inside the AuthenticationProvider, you use UserDetailsService implementation to retrieve the user. Since the contract of UserDetailsService is to retrieve UserDetails. So I can autowire my own UserDetailsService, where I had created a class, implemented the interface, and overridded the loadUserByUsername method. Right?

The loadUserByUsername method needs to communicate with a Repository, or a DAO, to get the actual user. In my case, I use Spring Data JPA, so I can implement a UserDao,

package com.example.springsecurityapp.dao;

import com.example.springsecurityapp.entities.User;
import org.springframework.data.repository.CrudRepository;

public interface UserDao extends CrudRepository<User, Long> {

    User findById(long id);
    User findByUsername(String username);
}

And this Dao would find a user by username, and return a UserDetails, which I can create my own, like this:

package com.example.springsecurityapp.configuration;

import com.example.springsecurityapp.entities.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

public class AuthenticatedUser implements UserDetails {
    private final User user;

    public AuthenticatedUser(User user) {
        this.user = user;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return List.of(() -> "READ");
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

UserDetails is just responsible for getting details of the user, such as password, username, enabled, etc.

Am I missing anything, or is my understanding on the right track?

34 Upvotes

6 comments sorted by

5

u/Sheldor5 Oct 18 '21

that's basically it

2

u/sdiamante13 Oct 19 '21

You explained it better than I could. Great Job!

1

u/ann321go Oct 19 '21

Yes you are right.

1

u/TheOldMancunian Oct 19 '21

Good explanation. Your choice of database can vary (I actually use neo4j as my DB backend).

We assign roles to users and map these roles to URL routes in the WebSecurityConfigurerAdapter class. So, only people with ROLE_ADMIN can access any routes in the /api/admin/... path.

All we needed was a UserDetailsService class and the WebSecurityConfigurerAdapter class. The user details are in a Person class with a PersonRepository interface.

And it just works!

1

u/Zaysetsu Oct 19 '21

You can also use a very basic auth with no need of a dao and user details (can be used in a prop file or constant), but this obviously is not recommended on a prod env.

1

u/UptownCoder23 Oct 22 '21

I think you got it all. Nice work