Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用<;第二节:授权访问=";hasRole(“角色”)&燃气轮机;用于检查多个角色?_Java_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Java 如何使用<;第二节:授权访问=";hasRole(“角色”)&燃气轮机;用于检查多个角色?

Java 如何使用<;第二节:授权访问=";hasRole(“角色”)&燃气轮机;用于检查多个角色?,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我想使用SpringSecurityJSPTaglibs根据角色有条件地显示一些内容。 但是在SpringSecurity3.1.x中只检查一个角色 我可以使用,但如果allgrated已弃用 有什么帮助吗?在spring security中有一个特殊的安全表达式: hasAnyRole(角色列表)-如果用户已被授予以下任何权限,则为true 指定的角色(以逗号分隔的字符串列表形式提供) 我从来没用过,但我想这正是你想要的 用法示例: <security:authorize access=

我想使用SpringSecurityJSPTaglibs根据角色有条件地显示一些内容。 但是在SpringSecurity3.1.x中只检查一个角色

我可以使用,但如果allgrated已弃用


有什么帮助吗?

在spring security中有一个特殊的安全表达式:

hasAnyRole(角色列表)-如果用户已被授予以下任何权限,则为true 指定的角色(以逗号分隔的字符串列表形式提供)

我从来没用过,但我想这正是你想要的

用法示例:

<security:authorize access="hasAnyRole('ADMIN', 'DEVELOPER')">
    ...
</security:authorize>

...
下面是一个示例,其中描述了标准的spring安全表达式。另外,在这里,我描述了如何在需要时创建自定义表达式。

我使用了
hasAnyRole('ROLE\u ADMIN','ROLE\u USER')
,但我在下面得到了bean创建错误

Error creating bean with name     'org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0': Cannot create inner bean '(inner bean)' of type [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource] while setting bean property 'securityMetadataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#2': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.security.web.access.expression.ExpressionBasedFilterInvocationSecurityMetadataSource]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Expected a single expression attribute for [/user/*]
然后我试着

access=“hasRole('ROLE\u ADMIN')或hasRole('ROLE\u USER')”
对我来说效果很好

因为我的一个用户是管理员和用户

为此,您需要添加
使用expressions=“true”auto-config=“true”
,后跟http标记

<http use-expressions="true" auto-config="true" >.....</http>
。。。。。

@dimas的答案在逻辑上与您的问题不一致
ifallgrated
不能直接替换为
hasAnyRole

从:

旧的:


必须具有角色\管理员和角色\用户

新增(SPeL):


必须具有角色\管理员和角色\用户

ifallgrated
直接替换为
hasAnyRole
,将导致spring使用
而不是
对语句进行求值。也就是说,如果经过身份验证的主体至少包含一个指定的角色,则
hasAnyRole
将返回
true
,而如果经过身份验证的主体包含所有指定的角色,则Spring的(现在在Spring Security 4中已弃用)ifallgrated
方法仅返回
true


TL;DR:要使用Spring Security Taglib的新身份验证表达式语言复制
ifallgrated
的行为,需要使用
hasRole('ROLE_1')和hasRole('ROLE_2')
模式。

如果您使用的是thymeleaf,您可以用这种方式尝试

sec:authorize="hasAnyRole(T(com.orsbv.hcs.model.SystemRole).ADMIN.getName(),
               T(com.orsbv.hcs.model.SystemRole).SUPER_USER.getName(),'ROLE_MANAGEMENT')"
如果用户具有上述角色,则返回true,否则返回false

请注意,您必须在html声明标记中使用sec标记,如下所示

<html xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

在Spring Boot 2.4中

sec:authorize="hasAnyRole('ROLE_ADMIN')
确保你有

thymeleaf-extras-springsecurity5

在您的依赖项中。还要确保包含名称空间

xmlns:sec="http://www.thymeleaf.org/extras/spring-security"

在你的html中…

它起作用了:-)(用户/管理员)但是有没有办法检查hasAllRoles(role1,role2)?关于你的最后一个问题:“但是有没有办法检查hasAllRoles(role1,role2)?”-我认为这是不可能的。但您可以自定义表达式(我提供了一个链接),并在代码中检查您想要的任何内容。在第二个问题上,您应该能够使用“hasRole”和“and”操作符,如hasRole('ADMIN')和hasRole('DEVELOPER')来实现此功能。如果您想为拥有role1和role2的人提供内容,我想您可以只嵌套两个块。
hasAnyRole
无法在逻辑上替换
ifallgrated
;请看下面我的回答,非常感谢。同样,它也适用于特权。此文本仅对同时具有“创建组”和“更改密码”权限的用户可见。
sec:authorize="hasAnyRole('ROLE_ADMIN')
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"