Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring security : User class without UserRole (roles). Authentication only no authorization needed

I'm using Spring-security with Spring-boot. My Application is a POC, for the moment there is no need to have Role.

I was wondering if it is possible to have a Custom UserDetailsService which return UserDetails in loadUserByUsername method but without the GrantedAuthority nor UserRole class.

I have googled all the day long for an example but I always get them with the UserRole.

Thanks in advance

like image 663
Master Mind Avatar asked Oct 28 '25 20:10

Master Mind


1 Answers

This should work:

  1. Let UserDetails return an empty collection in getAuthorities():

    public class User implements UserDetails {
        ...
        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {        
            return new HashSet<GrantedAuthority>();
        }
    }
    
  2. Use just authenticated() while configuring security

    http.authorizeRequests()
        ...
        .antMatchers("/foo").authenticated()
        ...                 
    
like image 176
Sanjay Avatar answered Oct 30 '25 12:10

Sanjay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!