No tengo mucho que decir o explicar, el problema al que me enfrento está en cierta parte de mi programa, no puede reconocer el método getter, pero he creado un método getter y también es público.

Clase de usuario:

public class User {

     @Id
        @Column(name="id")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        private String firstname;
        private String lastname;
        private String email;
        private String password;
        @ManyToMany(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
        @JoinTable(
                name="users_roles",
                joinColumns = @JoinColumn(
                        name= "user_id",referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(
                        name= "role_id",referencedColumnName = "rollid"))
        private Set<Role> roles;
    
        /*Creating cart*/
        @OneToMany(fetch = FetchType.EAGER)
        @JoinTable(
                name="user_purchase",
                joinColumns = @JoinColumn(
                        name="user_id",referencedColumnName = "id"),
                inverseJoinColumns = @JoinColumn(
                        name="product_id",referencedColumnName = "productId"))
        private Set<Products> purchased;
    
        /*All-arg constructor and No-arg Constructor*/
        public User() {}
    
        public User(String firstname, String lastname,
                    String email, String password, Set<Role> roles, Set<Products> purchased) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.email = email;
            this.password = password;
            this.roles = roles;
            this.purchased = purchased;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getFirstname() {
            return firstname;
        }
    
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
    
        public String getLastname() {
            return lastname;
        }
    
        public void setLastname(String lastname) {
            this.lastname = lastname;
        }
    
        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 Set<Role> getRoles() {
            return roles;
        }
    
        public void setRoles(Set<Role> roles) {
            this.roles = roles;
        }
    
        public Set<Products> getPurchased() {
            return purchased;
        }
    
        public void setPurchased(Set<Products> purchased) {
            this.purchased = purchased;
        }
    }

A continuación, publico la clase CustomUserDetails: *

package com.ecommerce.demo.services;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.stream.Collectors;

public class CustomUserDetails implements UserDetails {

    private User user;
    /*Getter and Setters for user variable*/

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    /*Overrided Methods Of User Details Class*/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return user.getRoles().stream().
                map(role-> new SimpleGrantedAuthority("ROLE_"+role)).collection(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}

En el código anterior en la declaración de devolución, IntelliJ me muestra getRoles () no está definido.

1
deep 30 ago. 2020 a las 08:13

1 respuesta

La mejor respuesta

El problema está en la importación de usuarios. Debe importar la clase de usuario correcta en la clase CustomUserDetails. Eliminar import org.springframework.security.core.userdetails.User;

Y reemplácelo con su clase de usuario.

        import org.springframework.security.core.GrantedAuthority;
        import org.springframework.security.core.authority.SimpleGrantedAuthority;
        //Remove Below import 
        import org.springframework.security.core.userdetails.User;
        
       //Add your User class Here
       import YOURPACKAGE.User;

        import org.springframework.security.core.userdetails.UserDetails;
        
        import java.util.Collection;
        import java.util.stream.Collectors;
1
Mark Rotteveel 30 ago. 2020 a las 07:34