如何创建一个数据类实现Spring Secuirty特定的UserDetails
我正在尝试将一些spring-webflux
示例代码迁移到kotlin。
目前我想将我的Spring Data Mongo示例转换为kotlin。 有一个User
,原Data 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 roles = new ArrayList(); @Override public Collection 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
接口包括一些getXXX和isXXX方法,如何添加override
到kotlin的username
和password
箴言?
顺便说一句:目前我删除了kotlin版本的UserDetails
, 请点击这里 :
@Document data class User( @Id var id: String? = null, var username: String? = null, var password: String? = null, var active: Boolean = true, var roles: List = ArrayList() )
更新 : 如何将 , 这个问题很有帮助 。 UserDetails
接口添加到它?
新的问题是使用破坏时,它不起作用。
(id, username, password, active, roles) =
User
位于https://github.com/hantsy/spring-reactive-sample/blob/master/kotlin-gradle/src/main/kotlin/com/example/demo/User.kt 。 目前我注释了实现UserDetails
的版本。 如果使用注释代码(User:UserDetails),则desstruction
中的desstruction
代码将在IDE中报告错误。 username
和password
发生错误: 破坏User!types的声明初始值设定项。 必须具有component2function 。
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, // 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, // 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 = authorities }