2015-03-29 23 views
6

Mam pewne problemy z uwierzytelnianiem wiosennym zabezpieczeń. Wszędzie w mojej aplikacji wszystko działa świetnie (operacje CRUD działają dobrze), ale próba zalogowania się nie udała.Uwierzytelnianie Spring Security przy użyciu UserDetailsService

Oto mój kod (I oznaczone poniżej komentarze gdzie userDAO jest null, który jest przyczyną uwierzytelniania):

@Service 
public class UserServiceImpl implements UserService, UserDetailsService { 

    @Autowired 
    UserDAO userDAO; 

    @Override 
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
     User user = userDAO.getUserByUsername(username); //userDAO == null Causing NPE 
     if (user == null) 
      throw new UsernameNotFoundException("Oops!"); 

     List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(user.getRole())); 

     return new org.springframework.security.core.userdetails 
       .User(user.getLogin(), user.getPassword(), authorities); 
    } 

@Override 
    public List<User> getUsers() { 
     return userDAO.getUsers();//userDAO !=null 
    } 
//rest of code skipped 

My SecurityConfig wygląda to

@Configuration 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

UserServiceImpl userService = new UserServiceImpl(); 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userService); 
    } 
//rest of code skipped 

zaznaczyłem gdzie otrzymuję NPE i nie mam pojęcia, jak rozwiązać ten problem. Cała konfiguracja jest JavaBased i można to sprawdzić tutaj po więcej szczegółów HERE

EDIT: getUsers() jest wywoływana w ten sposób w kontrolerze:

@Controller 
public class LoginController { 
    @Autowired 
    UserService userService; 

@RequestMapping(value = "/dashboard") 
    public ModelAndView userDashboard(){ 
     ModelAndView modelAndView = new ModelAndView("Dashboard"); 
     List<User> userList = userService.getUsers(); 
     modelAndView.addObject("users", userList); 
     return modelAndView; 
    } 

I w tym przypadku (gdy powołując userService.getUsers()) userDAO nie jest null

Próbowałem to naprawić jak Bohuslav Burghardt sugerowane i mam

method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder cannot be applied to given types; 
    required: T 
    found: com.gi.service.UserService 
    reason: inferred type does not conform to upper bound(s) 
    inferred: com.gi.service.UserService 
    upper bound(s): org.springframework.security.core.userdetails.UserDetailsService 

w linii auth.userDetailsService (userService);

Odpowiedz

2

problem rozwiązany z tego kawałka kodu z Bohuslav

public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserService userService; 
} 

również brakuje

@ComponentScan("com.gi") 

przed

public class SecurityConfig extends WebSecurityConfigurerAdapter { 

których brak spowodował

Error:(24, 13) java: method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.Authentic‌​ationManagerBuilder cannot be applied to given types; required: T found: com.gi.service.UserService reason: inferred type does not conform to upper bound(s) inferred: com.gi.service.UserService upper bound(s): org.springframework.security.core.userdetails.UserDetailsService 
6

Jest to część, która jest niepoprawna:

UserServiceImpl userService = new UserServiceImpl(); 

Podczas wystąpienia usługę samemu, jego autowired Zależności zawsze będzie null. Musisz pozwolić pojemnik Wiosna instancję (już zrobione poprzez zaznaczenie usługi @Service), a następnie wprowadzić go w swojej klasie konfiguracji zabezpieczeń takich jak to:

public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private UserDetailsService userService; 
} 
+0

Kiedy próbowałem go naprawić swój sposób pokazał Błąd: (24, 13) Java: metoda userDetailsService w klasie org.springframework.security.config.annotation. authentication.builders.AuthenticationManagerBuilder nie można zastosować do podanych typów; wymagane: T found: com.gi.service.UserService powód: wywnioskowany typ nie pasuje do górnej granicy wnioskowane: com.gi.service.UserService górne ograniczenie (s): org.springframework.security .core.userdetails.UserDetailsService – dklos

+1

@d__k Możesz to naprawić na jeden z trzech sposobów: 1) Autoworykuj usługę jako prywatną UserDetailsService userService, 2) spraw, aby twój interfejs UserService rozszerzył interfejs UserDetailsService, a następnie poproś, aby twoje klasy implementacji implementowały tylko UserService lub 3) Cast UserService do UserDetailsService w klasie konfiguracji –

+0

Próbowano w obie strony i otrzymałem: ' Nie znaleziono kwalifikującej się fasoli typu [com.gi.service.UserService] dla zależności: oczekiwano co najmniej 1 komponentu bean, który kwalifikuje się jako kandydata na program do tej zależności." – dklos

Powiązane problemy