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);
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
@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 –
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