Skip to content

[BUG] 使用Spring Security时,实体类实现UserDetails时使用@JSONField(serialize = false)不序列化authorities无效 #1465

@TsukasaHwan

Description

@TsukasaHwan

使用Spring Security时,实体类实现UserDetails时使用@JSONField(serialize = false)不序列化authorities无效

public static void main(String[] args) {
    TestUser testUser = new TestUser();
    testUser.setUsername("test");
    testUser.setPassword("123456");
    testUser.setAuthorities(Sets.newHashSet());
    String s = JSON.toJSONString(testUser);
    System.out.println(s);
}

@Data
public static class TestUser implements UserDetails {
    private String username;

    private String password;

    @JSONField(serialize = false)
    private Set<GrantedAuthority> authorities;

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    @JSONField(serialize = false)
    public boolean isEnabled() {
        return true;
    }
}

输出结果为{"authorities":[],"password":"123456","username":"test"}
期望为{"password":"123456","username":"test"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions