Spring security 如何创建实现Spring安全特定用户详细信息的数据类

Spring security 如何创建实现Spring安全特定用户详细信息的数据类,spring-security,kotlin,spring-webflux,Spring Security,Kotlin,Spring Webflux,我正在尝试将一些springwebflux示例代码迁移到kotlin 目前我想将我的样本转换为kotlin。有一个用户,原始数据Mongo版本看起来: @Data @ToString @Builder @NoArgsConstructor @AllArgsConstructor @Document class User implements UserDetails { @Id private String id; private String username;

我正在尝试将一些
springwebflux
示例代码迁移到kotlin

目前我想将我的样本转换为kotlin。有一个
用户
,原始数据Mongo版本看起来:

@Data
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Document
class User implements UserDetails {

    @Id
    private String id;
    private String username;
    private String password;

    @Builder.Default()
    private boolean active = true;

    @Builder.Default()
    private List<String> roles = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.createAuthorityList(roles.toArray(new String[roles.size()]));
    }

    @Override
    public boolean isAccountNonExpired() {
        return active;
    }

    @Override
    public boolean isAccountNonLocked() {
        return active;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return active;
    }

    @Override
    public boolean isEnabled() {
        return active;
    }

}
更新:如何向其添加
UserDetails
界面

新的问题是当使用销毁时,它不起作用

(id, username, password, active, roles) = <a user>
(id、用户名、密码、活动、角色)=。目前我注释掉了实现的版本
UserDetails
。如果我使用注释代码(User:UserDetails),则
Beans.kt中的
destruction
代码将在IDE中报告错误。错误发生在
用户名
密码
破坏用户类型的声明初始值设定项!必须具有component2函数

数据类用户(
data class User(
        // inherits from UserDetails
        private val username: String,
        private val password: String,
        private val isEnabled: Boolean, //Disabled account can not log in
        private val isCredentialsNonExpired: Boolean, //credential can be expired,eg. Change the password every three months
        private val isAccountNonExpired: Boolean, //eg. Demo account(guest) can only be online  24 hours
        private val isAccountNonLocked: Boolean, //eg. Users who malicious attack system,lock their account for one year
        private val authorities: Set<GrantedAuthority>, 

        // inherits from Tree.If the user is not a tree structure, can be ignored
        val id: Long,
        val parentId: Long? = null, 
        val lft: Int? = null, // preorder tree traversal algorithm
        val rgt: Int? = null, // preorder tree traversal algorithm
        val depth: Int? = null, // or levels
        val isLeaf: Boolean? = null, 
        val teamTotal: Int? = null, // The number of all subordinates
        val childrenTotal: Int? = null, //The number of all directly under the subordinate

        //Custom attributes
        val tenantId: Long, //We are the platform + tenant model
        val role: Set<Role>, // TENANT, SUBACCOUNT, DEFAULT_GROUP, MEMBER, GUEST
        val platform: Platform, // PC, IOS, ANDROID, WAP, INTERNAL
        val deviceToken: String? = null, //or machine code
        val ip: InetAddress //Inet4Address or Inet6Address
) : UserDetails, Tree(id, parentId) {
    override fun getUsername(): String = username
    override fun getPassword(): String = password
    override fun isEnabled(): Boolean = isEnabled
    override fun isCredentialsNonExpired(): Boolean = isCredentialsNonExpired
    override fun isAccountNonExpired(): Boolean = isAccountNonExpired
    override fun isAccountNonLocked(): Boolean = isAccountNonLocked
    override fun getAuthorities(): Set<out GrantedAuthority> = authorities
}
//从UserDetails继承 私有val用户名:String, 私有val密码:字符串, private val isEnabled:布尔值,//禁用的帐户无法登录 private val isCredentialsNonExpired:Boolean,//凭证可以过期,例如每三个月更改一次密码 private val isAccountNoneExpired:Boolean,//例如,演示帐户(来宾)只能24小时在线 private val isAccountNonLocked:Boolean,//例如,恶意攻击系统的用户将其帐户锁定一年 私人val机构:集, //从树继承。如果用户不是树结构,则可以忽略 val id:Long, val parentId:Long?=null, val lft:Int?=null,//前序树遍历算法 val rgt:Int?=null,//前序树遍历算法 val深度:Int?=null、//或级别 val isLeaf:布尔?=null, val teamTotal:Int?=null,//所有下属的数量 val childrenTotal:Int?=null,//下级直接下的全部的个数 //自定义属性 val tenantId:Long,//我们是平台+租户模型 val角色:Set、//租户、子帐户、默认组、成员、来宾 val平台:平台,//PC、IOS、ANDROID、WAP、内部 val deviceToken:字符串?=null,//或机器代码 val ip:InetAddress//Inet4Address或Inet6Address ):UserDetails,树(id,parentId){ 重写fun getUsername():String=username 重写fun getPassword():字符串=密码 override fun isEnabled():Boolean=isEnabled override fun isCredentialsNonExpired():Boolean=isCredentialsNonExpired 覆盖乐趣IsAccountNoneExpired():Boolean=IsAccountNoneExpired 覆盖乐趣isAccountNonLocked():Boolean=isAccountNonLocked 覆盖权限():Set=权限 }
数据类用户(
//从UserDetails继承
私有val用户名:String,
私有val密码:字符串,
private val isEnabled:布尔值,//禁用的帐户无法登录
private val isCredentialsNonExpired:Boolean,//凭证可以过期,例如每三个月更改一次密码
private val isAccountNoneExpired:Boolean,//例如,演示帐户(来宾)只能24小时在线
private val isAccountNonLocked:Boolean,//例如,恶意攻击系统的用户将其帐户锁定一年
私人val机构:集,
//从树继承。如果用户不是树结构,则可以忽略
val id:Long,
val parentId:Long?=null,
val lft:Int?=null,//前序树遍历算法
val rgt:Int?=null,//前序树遍历算法
val深度:Int?=null、//或级别
val isLeaf:布尔?=null,
val teamTotal:Int?=null,//所有下属的数量
val childrenTotal:Int?=null,//下级直接下的全部的个数
//自定义属性
val tenantId:Long,//我们是平台+租户模型
val角色:Set、//租户、子帐户、默认组、成员、来宾
val平台:平台,//PC、IOS、ANDROID、WAP、内部
val deviceToken:字符串?=null,//或机器代码
val ip:InetAddress//Inet4Address或Inet6Address
):UserDetails,树(id,parentId){
重写fun getUsername():String=username
重写fun getPassword():字符串=密码
override fun isEnabled():Boolean=isEnabled
override fun isCredentialsNonExpired():Boolean=isCredentialsNonExpired
覆盖乐趣IsAccountNoneExpired():Boolean=IsAccountNoneExpired
覆盖乐趣isAccountNonLocked():Boolean=isAccountNonLocked
覆盖权限():Set=权限
}

您的问题是解构声明,对吗?你能提供一个完整的代码示例吗?我看不出有错误there@s1m0nw1更新并添加了当前用户类链接。您的问题是解构声明,对吗?你能提供一个完整的代码示例吗?我看不出有错误there@s1m0nw1更新并添加了当前用户类链接。
data class User(
        // inherits from UserDetails
        private val username: String,
        private val password: String,
        private val isEnabled: Boolean, //Disabled account can not log in
        private val isCredentialsNonExpired: Boolean, //credential can be expired,eg. Change the password every three months
        private val isAccountNonExpired: Boolean, //eg. Demo account(guest) can only be online  24 hours
        private val isAccountNonLocked: Boolean, //eg. Users who malicious attack system,lock their account for one year
        private val authorities: Set<GrantedAuthority>, 

        // inherits from Tree.If the user is not a tree structure, can be ignored
        val id: Long,
        val parentId: Long? = null, 
        val lft: Int? = null, // preorder tree traversal algorithm
        val rgt: Int? = null, // preorder tree traversal algorithm
        val depth: Int? = null, // or levels
        val isLeaf: Boolean? = null, 
        val teamTotal: Int? = null, // The number of all subordinates
        val childrenTotal: Int? = null, //The number of all directly under the subordinate

        //Custom attributes
        val tenantId: Long, //We are the platform + tenant model
        val role: Set<Role>, // TENANT, SUBACCOUNT, DEFAULT_GROUP, MEMBER, GUEST
        val platform: Platform, // PC, IOS, ANDROID, WAP, INTERNAL
        val deviceToken: String? = null, //or machine code
        val ip: InetAddress //Inet4Address or Inet6Address
) : UserDetails, Tree(id, parentId) {
    override fun getUsername(): String = username
    override fun getPassword(): String = password
    override fun isEnabled(): Boolean = isEnabled
    override fun isCredentialsNonExpired(): Boolean = isCredentialsNonExpired
    override fun isAccountNonExpired(): Boolean = isAccountNonExpired
    override fun isAccountNonLocked(): Boolean = isAccountNonLocked
    override fun getAuthorities(): Set<out GrantedAuthority> = authorities
}