Olá Arthur.
Fiz um mapeamento ManyToMany para o Spring Security junto com o oAuth2 e ao buscar um Principal através de um token, ele retorna um encadeamento infinito, conforme exemplo JSON abaixo:
{
"authorities": [
{
"id": {
"userId": 1,
"roleId": 1
},
"user": {
"id": 1,
"name": "Bruno",
"email": "mail@gmail.com",
"password": "$2a$10$hIOcJxKMHBKxRM2LqPuwsi1B6wdXjwQggxdy9",
"active": true,
"enabled": true,
"authorities": [
{
"id": {
"userId": 1,
"roleId": 1
},
"user": {
"id": 1,
"name": "Bruno",
"email": "mail@gmail.com",
"password": "$2a$10$hIOcJxKMHBKxRM2LqPuwsi1B6wdXjwQggxdy9.",
"active": true,
"enabled": true,
"authorities": [
(...)
Mapeamentos:
Classe User:
@Entity
public class User implements UserDetails {
(...)
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
private Set<UserRole> roles = new HashSet<>();
}
Classe Role:
@Entity
public class Role {
(...)
@OneToMany(mappedBy = "role", fetch = FetchType.LAZY)
private Set<UserRole> users = new HashSet<>();
Classe Associativa:
@Entity
@Table(schema = DBConfigurations.DB_SCHEMA, name = "tb_user_role")
public class UserRole implements GrantedAuthority {
private static final long serialVersionUID = 1L;
@EmbeddedId
private UserRoleKey id;
@ManyToOne
@MapsId("userId")
@JoinColumn(name = "id_user")
private User user;
@ManyToOne
@MapsId("roleId")
@JoinColumn(name = "id_role")
private Role role;
@Column(name = "dt_begin")
private LocalDateTime dtBegin;
@Column(name = "dt_end")
private LocalDateTime dtEnd;
@Column(name = "in_active")
private Boolean isActive;
(...)
Por que traz esse mapeamento infinito? Como resolver? Muito obrigado!