Получил ошибку в spring boot , spring security
Помогите, пожалуйста, исправить ошибку:
Ошибка - Description:
Parameter 0 of constructor in sirvices.UserDetailsImpl required a bean of type 'java.lang.Long' that could not be found.
Action:
Consider defining a bean of type 'java.lang.Long' in your configuration.
Мой UserDetailsImpl
:
@Service
public class UserDetailsImpl implements UserDetails {
private Long id;
private String username;
@JsonIgnore
private String password;
private String email;
private Collection<? extends GrantedAuthority> authorities;
public static UserDetailsImpl build(User user){
List<GrantedAuthority> authorities = user.getRoles().stream().map(role -> new SimpleGrantedAuthority(role.getName().name())).collect(Collectors.toList());
return new UserDetailsImpl(
user.getId(),
user.getUsername(),
user.getPassword(),
user.getEmail(),
authorities
);
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return authorities;
}
@Override
public String getPassword() {
return password;
}
public Long getId() {
return id;
}
public String getEmail() {
return email;
}
@Override
public String getUsername() {
return username;
}
public UserDetailsImpl(Long id, String username, String password, String email, Collection<? extends GrantedAuthority> authorities) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.authorities = authorities;
}
}
Мой UserDetailsServiceImpl
:
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
private UserRep rep;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = rep.findByUsername(username).orElseThrow(()-> new UsernameNotFoundException("User not found , username : " + username));
return UserDetailsImpl.build(user);
}
public UserRep getRep() {
return rep;
}
public void setRep(UserRep rep) {
this.rep = rep;
}
}
Ответы (1 шт):
Автор решения: Roman C
→ Ссылка
Проблема в том что вы поставили @Servive
аннотацию на UserDetailsImpl
.
Этого делать было не нужно, потому что Spring не может создать инстанцию этого класса так как вы не определили, как будут подставляться параметры для конструктора, который практически не используется Spring-ом, так как статический метод использует оператор new
для создания объекта этого класса.
Поэтому этот класс должен быть обычным POJO. Если вы уберете аннотацию, то всё должно работать.