转换列表<权限>至 Collection在Spring安全中 权限>
我的情况是,我有一个接受 Collection 的方法,并且我将值作为列表 当我尝试将列表值添加到集合参数中时它不起作用,所以我应该转换它吗?
更新: 当我尝试创建:
new org.springframework.security.core.userdetails.User(
user.getEmail(), user.getPassword(), true, true, true, true,
user.getAuthorities());
其中 user.getAuthorities() 返回我定义的 Authority 对象的数组列表时,
上面的代码给出了构造函数未定义的错误,当我添加集合强制转换它时工作正常。
org.springframework.security.core.userdetails
类用户构造函数
User(java.lang.String username, java.lang.String password,
boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
java.util.Collection<? extends GrantedAuthority> authorities)
My case is that i have a method that takes Collection and i have the value as a list
when i tried to add the list value into collection parameter it doesn't work, so should i cast it ?
UPDATE:
when i tried to create:
new org.springframework.security.core.userdetails.User(
user.getEmail(), user.getPassword(), true, true, true, true,
user.getAuthorities());
where user.getAuthorities() returns an arraylist of my defined Authority object
above code gives error that the constructor is undefined, when i add collection casting it works fine.
org.springframework.security.core.userdetails
Class UserConstructor
User(java.lang.String username, java.lang.String password,
boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
java.util.Collection<? extends GrantedAuthority> authorities)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
User 构造函数的 javadoc 告诉您最后一个参数必须是
Collection
。因此,如果 ArrayList 是ArrayList
,其中Something
是GrantedAuthority
或实现GrantedAuthority< 的类,则您的 ArrayList 将被接受/代码>。
您还没有告诉我们
user.getAuthorities()
方法返回的确切类型是什么。你了解泛型吗?您读过有关泛型的 Java 教程吗?The javadoc of the User constructor tells you that the last argument must be a
Collection<? extends GrantedAuthority>
. So, you ArrayList will be accepted if it's anArrayList<Something>
, whereSomething
isGrantedAuthority
or a class which implementsGrantedAuthority
.You still haven't told us what was the exact type returned by the method
user.getAuthorities()
. Do you understand Generics? Have you read the Java tutorial about generics?List
已经是一个Collection
,因为它是Collection
的子接口。如果您有一个实现
List
类型的变量(例如ArrayList
),则可以在任何需要Collection
的地方使用它。不需要转换,甚至不需要显式类型转换。A
List
already is aCollection
since it's a sub-interface ofCollection
.If you have a variable of type implementing
List
(e.g.ArrayList
), you can use it anywhere aCollection
is expected. No conversion will be necessary, not even an explicit type cast.不过List已经是集合了,所以实际上不需要转换。
However, List already is collection, so actually no conversion is required.