延迟加载时,Spring Boot登录错误

时间:2018-08-13 17:15:49

标签: spring-boot spring-security spring-data-jpa

我已经向Spring Boot用户添加了一些员工数据,如下所示:

import org.springframework.security.core.userdetails.User;

public class UserFull extends User {
    private final Employee employee;

    public UserFull (String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, 
                    boolean accountNonLocked, Collection authorities, Employee employee) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);

        this.employee = employee;
    }

    public Employee getEmployee() {
        return employee;
    }
}

然后在UserDetailsServiceImpl中引用它,例如:

public class UserDetailsServiceImpl implements UserDetailsService {    
    @Autowired
    private ErpUserRepository userRepository;

    @Override
    @Transactional(readOnly=true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        ErpUser erpUser = userRepository.findByUserName(username);

        UserFull userFull = null;
        if (erpUser!=null) {
            Set<GrantedAuthority> grantedAuthorities = new HashSet();
            erpUser.getRoles().forEach((role) -> {
                grantedAuthorities.add(new SimpleGrantedAuthority(role.getRoleName()));
            });

            userFull = new UserFull(username, erpUser.getPassword(), true, true, true, true, grantedAuthorities, erpUser.getEmployee());
        } else {
            throw new UsernameNotFoundException("User not found.");
        }

        return userFull;
    }
}

当所有Employee数据都急切加载时,此方法非常有用。但是,如果我将Employee属性之一更改为具有`fetch = FetchType.LAZY',那么就会出现一些问题。我在日志中看到,即使我不希望它仍在获取属性。登录仍然有效,但响应未完成。

这是Employee中的属性:

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="fk_employeetypeid")
@JsonView({View.SimpleEmployeeView.class,View.EmployeeBaseView.class})
public EmployeeType getEmployeeType() {
    return employeeType;
}

以下是回复:

  

{“ password”:空,“ username”:“ tim”,“ authorities”:[{“ authority”:“ Administrator”},{“ authority”:“ Developer”},{“ authority”:“ Engineering “},{” authority“:”购买“},{” authority“:”质量“}]],” accountNonExpired“:true,” accountNonLocked“:true,” credentialsNonExpired“:true,” enabled“:true,”员工“:{” employeeID“:31,” lastName“:” Last“,” firstName“:” Tim“,” nameSuffix“:null,” initials“:” TL“,” employeeType“:{” employeeTypeID“:1, “ employeeTypeName”:“完整   时间”,“处理程序”

如您所见,响应以一些“ handler”结束。即使验证有效,日志中也会出现此错误:

  

com.fasterxml.jackson.databind.exc.InvalidDefinitionException:否   找到用于类的序列化器   org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer,无   发现的用于创建BeanSerializer的属性(为避免出现异常,   禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用   链:   com.company.utilities.UserFull [“ employee”]-> com.company.humanresources.domain.Employee [“ employeeType”]-> com.company.humanresources.domain.EmployeeType _ $$ _ jvste76_8cd [“ handler”])

但是,在日志错误之前的一行中,存在toString的完整UserFull,其中包含EmployeeType和其他所有内容。

问题:

  • 为什么延迟加载不起作用? (为什么它仍在获取EmployeeType数据?)
  • 为什么我会看到“处理程序”错误? (这是什么意思,如何解决?)

0 个答案:

没有答案