Question: I am trying to implement role based authorization on my rest apis.
I have been able to successfully implement and run the project but there are some things that I am confused about.

CustomeUserDetailsService.java:

```
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Optional<UsersAuth> optionalUsers = usersAuthRepository.findByName(username);

    optionalUsers
            .orElseThrow(() -> new UsernameNotFoundException("Username not found"));
    return optionalUsers
            .map(CustomUserDetails::new).get();
}

```

CustomUserDeatils.java:

```
 @Override
public Collection<? extends GrantedAuthority> getAuthorities() {

    return getRoles()
            .stream()
            .map(role -> new SimpleGrantedAuthority("ROLE_" + role.getRole()))
            .collect(Collectors.toList());
}

```

I want to know what the above two code snippets are actually doing. can someone explain to me? I have searched alot but did not understand. Specially what is the use of Granted authority? I know for a fact that CustomUserDetailsService is taking a username and if it does not exist, it throws an exception. If anyone could elaborate in detail? It would help me alot.

Answer: 
In my case there was an error about the POSTGRES\_PASSWORD (in docker-compose.yml) and the DATABASE settings for PASSWORD in the project\_level settings.py file.

After providing a password in docker-compose.yml

```
  db:   # For the PostgreSQL database
    image: postgres:11
    environment:
      - POSTGRES_PASSWORD=example

```

It is necessary to provide the same password to enable access to the POSTGRES database between the two containers (in my case the web application and the database it depended on). In the project\_level settings.py:

```
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'example',
        'HOST': 'db',
        'PORT': 5432
    }
}

```
Adding on ArifMustafa's answer, This worked for me.

```
  postgres:
    image:  'postgres:12-alpine'
    environment:
        POSTGRES_PASSWORD: mypassword
    expose:
      - 5432
    volumes: 
      - postgres_data:/var/lib/postgres/data/

volumes:
   postgres_data:

```