Skip to content
Advertisement

Gradle Login and signup page giving error in getUserAuthority() in CustomerUserDetailsService.java

The method getUserAuthority(java.util.Set<com.djamware.springsecuritymongodb.domain.Role>) in the type CustomUserDetailsService is not applicable for the arguments (java.util.Set<javax.management.relation.Role>)
Why giving this error?
This Java file is under package com.djamware.springsecuritymongodb.services and User.java is under package com.djamware.springsecuritymongodb.domain

    package com.djamware.springsecuritymongodb.services;

    import com.djamware.springsecuritymongodb.domain.Role;
    import com.djamware.springsecuritymongodb.domain.User;
    import com.djamware.springsecuritymongodb.repositories.RoleRepository;
    import com.djamware.springsecuritymongodb.repositories.UserRepository;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.stereotype.Service;

    @Service
   public class CustomUserDetailsService implements UserDetailsService{
   @Autowired
   private UserRepository userRepository;
   @Autowired
   private RoleRepository roleRepository;
   @Autowired
  private BCryptPasswordEncoder bCryptPasswordEncoder;

  public User findUserByEmail(String email) {
    return userRepository.findByEmail(email);
  }

   public void saveUser(User user) {
    user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
    user.setEnabled(true);
    Role userRole = roleRepository.findByRole("ADMIN");
   // user.setRoles(new HashSet<>(Arrays.asList(userRole)));
    user.setRoles(new HashSet<Role>(Arrays.asList(userRole)));
    userRepository.save(user);
}

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

    User user = userRepository.findByEmail(email);
    if(user != null) {
       List<GrantedAuthority> authorities = getUserAuthority(user.getRoles());
        return buildUserForAuthentication(user, authorities);
    } else {
        throw new UsernameNotFoundException("username not found");
    }
}


private List<GrantedAuthority> getUserAuthority(Set<Role> userRoles) {
    Set<GrantedAuthority> roles = new HashSet<GrantedAuthority>();
    for (Role role : userRoles) {
        roles.add(new SimpleGrantedAuthority(role.getRole()));
    }

    List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(roles);
    return grantedAuthorities;
}

private UserDetails buildUserForAuthentication(User user, List<GrantedAuthority> authorities) {
    return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), authorities);
}

}

I have created User Class attached here.

User.java

     package com.djamware.springsecuritymongodb.domain;
     import java.util.HashSet;
     import java.util.Set;
     import org.springframework.data.annotation.Id;
     import org.springframework.data.mongodb.core.index.IndexDirection;
     import org.springframework.data.mongodb.core.index.Indexed;
     import org.springframework.data.mongodb.core.mapping.DBRef;
     import org.springframework.data.mongodb.core.mapping.Document;
      import javax.management.relation.Role;

    @Document(collection = "user")
   public class User {
   @Id
   private String id;
   @Indexed(unique = true, direction = IndexDirection.DESCENDING, dropDups = true)
   private String email;
   private String password;
   private String fullname;
   private boolean enabled;
   @DBRef
   private Set<Role> roles;
  
   public String getId() {
     return id;
   }

public void setId(String id) {
    this.id = id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getFullname() {
    return fullname;
}

public void setFullname(String fullname) {
    this.fullname = fullname;
}

public boolean isEnabled() {
    return enabled;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}

public Set<Role> getRoles() {
    return roles;
}

public void setRoles(Set<Role> roles) {
    this.roles = roles;
}

public void setRoles(HashSet<com.djamware.springsecuritymongodb.domain.Role> hashSet) {
    // TODO Auto-generated method stub
    
}

}

Advertisement

Answer

The reason is that the Role that CustomUserDetailsServices getUserAuthority(Set<Role> userRoles) is expecting com.djamware.springsecuritymongodb.domain.Role (see its imports). However, User is returning javax.management.relation.Role (see its imports). To fix it, remove import javax.management.relation.Role; from the User class. Since User and Role are in the same package you don’t need to explicitly import anything.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement