techdev-solutions / jaxenter-showcase

42 stars 32 forks source link

asList() supposed to be Arrays.asList() #2

Closed kentoj closed 9 years ago

kentoj commented 9 years ago

Is your asList in your getGrantedAuthorities supposed to read Arrays.asList()?

Here is the code of my FakeUserDetailsService working with Arrays.asList():

package demo.core.service;

import java.util.Arrays;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import demo.data.domain.Person;
import demo.data.repository.PersonRepository;

@Service
public class FakeUserDetailsService implements UserDetailsService {
    @Autowired
    private PersonRepository personRepository;

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        Person person = personRepository.findByFirstNameEquals(username);
        if (person == null) {
            throw new UsernameNotFoundException("Username " + username + "not found");
        }
        return new User(username,"password", getGrantedAuthorities(username));
    }
    private Collection<? extends GrantedAuthority> getGrantedAuthorities(String username) {
        Collection<? extends GrantedAuthority> authorities;
        if (username.equals("Justin")) {
            authorities = Arrays.asList(() -> "ROLE_ADMIN", () -> "ROLE_BASIC");
        }
        else {
            authorities = Arrays.asList(() -> "ROLE_BASIC" );
        }
    return authorities;
    }

}
FrontierPsychiatrist commented 9 years ago

Hi! Yes, it's imported statically at line 13.

kentoj commented 9 years ago

I see. Thanks for pointing that out! I must have missed that line the first time around.