[尝试实现JWT安全性时出现Hibernate的StackOverflowError

问题描述 投票:2回答:1

我是这个主题的新手,在尝试进行授权时,我会不断收到“ java.lang.StackOverflowError:null”。我在邮递员中收到内部服务器错误,在控制台中我得到了stackoveflowerror。这是我的AuthenticationController:


   @PostMapping("/signup")
   public ResponseEntity<?> registerUser(@Valid @RequestBody SignUpRequest signUpRequest) {
       if (userService.existsByEmail(signUpRequest.getEmail())) {
           throw new BadRequestException("email already exists");
       }
       User user = UserMapper.INSTANCE.registerRequestoUser(signUpRequest);
       user.setPassword(passwordEncoder.encode(user.getPassword()));
       Optional<Role> optionalRole = roleService.getByName("user");

       if (optionalRole.isPresent()) {
           Role userRole = optionalRole.get();
           user.addRole(userRole);
           Optional<User> optionalUser = userService.create(user);
           if (optionalUser.isPresent()) {
               User result = optionalUser.get();
               URI location = ServletUriComponentsBuilder
                       .fromCurrentContextPath().path("/api/v1/users/email/{email}")
                       .buildAndExpand(result.getEmail()).toUri();

               return ResponseEntity.created(location).body("User registered successfully");
           }

       }
       return (ResponseEntity<?>) ResponseEntity.badRequest();
   }

我的SignUpRequest类:


public class SignUpRequest {
   @NotBlank
   private String firstName;

   @NotBlank
   private String lastName;

   @NotBlank
   @Email
   private String email;

   @NotBlank
   @Size(min = 8, max = 20)
   private String password;

   private Set<String> roles;

我的用户实体和我的角色实体:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User extends BaseEntity {

    private String firstName;

    private String lastName;

    @Email(message = "Email should be valid")
    private String email;

    @Size(min = 3, max = 100, message
            = "password must be between 3 and 50 characters")
    private String password;

    @OneToMany(mappedBy = "user",
            cascade = {CascadeType.PERSIST, CascadeType.MERGE,
                    CascadeType.DETACH, CascadeType.REFRESH})
    private Set<Appointment> appointments;


    @ManyToMany(targetEntity = Role.class,
            cascade = {CascadeType.ALL},
            fetch = FetchType.EAGER)
    @JoinTable(
            name = "user_role",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id")}
    )
    private Set<Role> roles;
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Role extends BaseEntity {

    @NotNull
    private String name;

    @ManyToMany(mappedBy = "roles")
    private Set<User> users;

    public String  getName() {
        return name;
    }

这是我的邮递员要求

{
        "firstName":"name",
        "lastName":"name",
        "email": "[email protected]",
        "password": "thepassword123",
        "roles": ["user"]

}

这里是堆栈跟踪的一部分:

at at com.project.rushhour.entity.User.hashCode(User.java:15) ~[classes/:na]
...
at at com.project.rushhour.entity.Role.hashCode(Role.java:15) ~[classes/:na]
...


at com.project.rushhour.entity.User.hashCode(User.java:15) ~[classes/:na]
...
at com.project.rushhour.entity.Role.hashCode(Role.java:15) ~[classes/:na]
...


at com.project.rushhour.entity.User.hashCode(User.java:15) ~[classes/:na]
...
at com.project.rushhour.entity.Role.hashCode(Role.java:15) ~[classes/:na]
...


我认为程序尝试解析角色时,该错误在某处,但不确定到底是什么问题。 jwt的东西设置正确,只是逻辑错了

java spring hibernate spring-boot lombok
1个回答
0
投票

问题的原因是,您具有从用户角色和从角色到用户的关系。但是您尚未显式定义hashCode方法。您使用@ Data批注。默认情况下,Lombok生成的hashCode方法包括all实体的属性。

hashCode通常如下计算:

h = hashCode(property1);
h = h*31 + hashCode(property2);
h = h*31 + hashCode(property3);
h = h*31 + hashCode(property4);
h = h*31 + hashCode(property5);
...

User上调用hashCode时,如果计算属性firstNamelastName等的哈希码,则还要添加属性roles]的哈希码>。 roles的哈希码的计算包括每个Role元素的哈希码的计算。

但是哈希码Role

的计算包括属性users的哈希码的计算,该属性在每个User元素上调用hashCode()。正如我们在上面看到的,User的哈希码将再次与Role的哈希码冲突。等。直到使用堆栈容量。然后您得到StackOverflowError

如何避免?从hashCode()

方法中排除递归。要么手动实现hashCode()。或告诉Lombok从hashCode()中排除基于关系的属性。

我希望龙目岛仍然可以为我产生一切。因此,我会告诉Lombok在生成hashCode()equals( )

。因此您的代码如下所示:public class User extends BaseEntity { private String firstName; private String lastName; ... @EqualsAndHashCode.Exclude private Set<Role> roles; ... } public class Role extends BaseEntity { ... private String name; ... @EqualsAndHashCode.Exclude private Set<User> users; ... } 为什么注释称为EqualsAndHashCode?因为hashCode()
equals()
应该始终进行[[synchronously更改,以实现所谓的

hashCode-equals

合同。
© www.soinside.com 2019 - 2024. All rights reserved.