Spring启动认证中的堆栈溢出

时间:2017-03-12 12:34:28

标签: spring hibernate spring-boot spring-security

我正在尝试使用Spring Boot中的角色创建一个简单的登录。

我有一个User类:

package com.codecool.domain;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import java.util.Set;

@Data
@Entity
@Table(name="users")
@Slf4j
public class User {

@Id
@Column(name="user_id", unique = true)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

@Column(name="email")
private String email;

@Column(name="password")
private String password;

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="user_role", joinColumns =         @JoinColumn(name="user_id"), inverseJoinColumns = @JoinColumn(name="role_id"))
private Set<Role> roles;

private User(){}

public User(String email, String password, Set<Role> roles) {
    this.email = email;
    this.password = password;
    this.roles = roles;
}

}

...和角色类:

package com.codecool.domain;


import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.*;
import java.util.Set;

@Data
@Entity
@Table(name="role")
@Slf4j
public class Role {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToMany(mappedBy = "roles", fetch=FetchType.EAGER)
    private Set<User> users;

    private Role(){}

    public Role(String name){
        this.name = name;
    }
}

每当我尝试通过login.html登录时,都会遇到以下错误消息进入无限循环:

2017-03-12 13:26:17.872 INFO 8794 --- [io-8080-exec-10] o.h.h.i.QueryTranslatorFactoryInitiator:HHH000397:使用ASTQueryTranslatorFactory Hibernate:用户user0_选择user0_.user_id为user_id1_2_,user0_.email为email2_2_,user0_.password为password3_2_,其中user0_.email =? Hibernate:选择roles0_.user_id为user_id1_1_0_,roles0_.role_id为role_id2_1_0_,role1_.id为id1_0_1_,role1_.name为name2_0_1_来自user_role roles0_ inner join role role1_ on roles0_.role_id = role1_.id其中roles0_.user_id =? Hibernate:选择users0_.role_id为role_id2_1_0_,users0_.user_id为user_id1_1_0_,user1_.user_id为user_id1_2_1_,user1_.email为email2_2_1_,user1_.password为password3_2_1_ from user_role users0_ inner join users user1_ on users0_.user_id = user1_.user_id where users0_。 ROLE_ID =? Hibernate:选择roles0_.user_id为user_id1_1_0_,roles0_.role_id为role_id2_1_0_,role1_.id为id1_0_1_,role1_.name为name2_0_1_来自user_role roles0_ inner join role role1_ on roles0_.role_id = role1_.id其中roles0_.user_id =? Hibernate:选择users0_.role_id为role_id2_1_0_,users0_.user_id为user_id1_1_0_,user1_.user_id为user_id1_2_1_,user1_.email为email2_2_1_,user1_.password为password3_2_1_ from user_role users0_ inner join users user1_ on users0_.user_id = user1_.user_id where users0_。 ROLE_ID =? Hibernate:选择roles0_.user_id为user_id1_1_0_,roles0_.role_id为role_id2_1_0_,role1_.id为id1_0_1_,role1_.name为name2_0_1_来自user_role roles0_ inner join role role1_ on roles0_.role_id = role1_.id其中roles0_.user_id =? Hibernate:选择users0_.role_id为role_id2_1_0_,users0_.user_id为user_id1_1_0_,user1_.user_id为user_id1_2_1_,user1_.email为email2_2_1_,user1_.password为password3_2_1_ from user_role users0_ inner join users user1_ on users0_.user_id = user1_.user_id where users0_。 ROLE_ID =? Hibernate:选择roles0_.user_id为user_id1_1_0_,roles0_.role_id为role_id2_1_0_,role1_.id为id1_0_1_,role1_.name为name2_0_1_来自user_role roles0_ inner join role role1_ on roles0_.role_id = role1_.id其中roles0_.user_id =? Hibernate:选择users0_.role_id为role_id2_1_0_,users0_.user_id为user_id1_1_0_,user1_.user_id为user_id1_2_1_,user1_.email为email2_2_1_,user1_.password为password3_2_1_ from user_role users0_ inner join users user1_ on users0_.user_id = user1_.user_id where users0_。 ROLE_ID =?

我缺少什么想法?

2 个答案:

答案 0 :(得分:2)

当你想在RestController中返回对象时,对象序列化为json .Spring使用jackson来序列化。为了解决这个问题你想要jsonignore用户或角色例如忽略Role.java中的用户

@Data
@Entity
@Table(name="role")
@Slf4j
public class Role {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToMany(mappedBy = "roles", fetch=FetchType.EAGER)
    @JsonIgnore
    private Set<User> users;

    private Role(){}

    public Role(String name){
        this.name = name;
    }
}

答案 1 :(得分:0)

使用延迟加载

@ManyToMany(mappedBy = "roles", fetch=FetchType.LAZY)
private Set<User> users;

users中有Role不是个好主意。最好删除此字段。