对于通过 http 命名空间定义为拦截 URL 的 Web 安全表达式,会考虑角色层次结构,但在使用 JSP 授权标签库的表达式中则不会。
我已经读了很多东西了...ref1 ref2 ref3 ref4 ref5 ref6
****编辑:****Ref1 和 Ref6 提到了关于 jsp 中不可用的过滤器顺序和安全上下文问题的内容...(顺便说一句,我正在使用 jsf2)也许有一些东西需要挖掘...
EDIT 2:JSF 是否处理安全标记库?我读this并尝试that没有成功
EDIT 3 ::我尝试通过 Maven spring-faces 2.3.2 和 spring-security-taglibs 安装...什么也没有发生...一些tuto说创建一个自定义 taglib.xml 但它也不起作用,我认为它是为了旧版本...
我测试了 facesContext.externalContext.isUserInRole('ROLE') 和 sec:authorize access="hasRole('Role')... 第一个仅在相同 ROLE 时才有效,但没有考虑层次结构...并且sec: autorize 简单地什么都不做,所有的角色都被打印出来。
请参阅上面的示例,其中用户具有 ROLE ROLE_ADMIN_PROFILER_NGS :
<h:outputText rendered="#{facesContext.externalContext.isUserInRole('ROLE_ADMIN_PROFILER_NGS')}" value ="ROLE_ADMIN_PROFILER_NGS"></h:outputText> // WORKS <br></br>
<h:outputText rendered="#{facesContext.externalContext.isUserInRole('ROLE_GUEST')}" value ="ROLE_GUEST"></h:outputText> // SHOULD APPEAR BUT NOTHING HAPPENS<br></br>
<h:outputText rendered="#{facesContext.externalContext.isUserInRole('ROLE_ADMIN')}" value ="ROLE_ADMIN"> // SHOULD NOT APPEAR AND THAT'S THE CASE</h:outputText><br></br>
/* ALL THE THREE NEXT ARE DISPLAYED WHITHOUT CONTROL AUTORIZATION.*/
<sec:authorize access="hasRole('ROLE_ADMIN_PROFILER_NGS')">ROLE_ADMIN_PROFILER_NGS<br></br></sec:authorize>
<sec:authorize access="hasRole('ROLE_GUEST')">ROLE_GUEST <br></br></sec:authorize>
<sec:authorize access="hasRole('ROLE_ADMIN')">ROLE_ADMIN <br></br></sec:authorize>
这是我尝试访问以使用角色层次结构进行测试的内容:
当用户仅带有角色 GUEST 时...所有标签都会显示...它们不会出现,只有 GUEST (参见底部的定义)应该出现:
<sec:authentication property="username" />
<sec:authorize access="hasRole('ROLE_BABAB')">BABA</sec:authorize>
<sec:authorize access="hasRole('ROLE_GUEST')">GUEST</sec:authorize>
<sec:authorize access="hasRole('ROLE_ADMIN')">ADMIN</sec:authorize>
这是我的安全配置:
<security:http auto-config="true" access-decision-manager-ref="accessDecisionManager" use-expressions="true" disable-url-rewriting="true">
<security:intercept-url pattern="/Participant/New/*" access="hasRole('ROLE_ADMIN')" />
<security:intercept-url pattern="/Home" access="hasRole('ROLE_GUEST')" />
<security:intercept-url pattern="/Login" access="hasRole('ROLE_ANONYMOUS')" />
<security:intercept-url pattern="/Login/Error" access="hasRole('ROLE_ANONYMOUS')" />
<security:form-login login-page="/Login" login-processing-url="/j_spring_security_check" authentication-failure-url="/Login/Error" default-target-url="/Home" />
<security:logout logout-url="/j_spring_security_logout" logout-success-url="/Home" delete-cookies="JSESSIONID" invalidate-session="true"/>
<security:anonymous/>
<security:expression-handler ref="defaultWebSecurityExpressionHandler" />
<security:session-management invalid-session-url="/Login" >
<security:concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</security:session-management>
<security:port-mappings>
<security:port-mapping http="8086" https="8443"/>
</security:port-mappings>
</security:http>
<beans:bean id="defaultWebSecurityExpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler">
<beans:property name="roleHierarchy" ref="roleHierarchy"/>
</beans:bean>
<beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
<beans:property name="decisionVoters">
<beans:list>
<beans:ref bean="roleVoter" />
<beans:bean class="org.springframework.security.web.access.expression.WebExpressionVoter">
<beans:property name="expressionHandler" ref="defaultWebSecurityExpressionHandler"/>
</beans:bean>
<beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
</beans:list>
</beans:property>
</beans:bean>
<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleHierarchyVoter">
<beans:constructor-arg ref="roleHierarchy" />
</beans:bean>
<beans:bean id="roleHierarchy" class="org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl">
<beans:property name="hierarchy">
<beans:value>
ROLE_ADMIN > ROLE_ADMIN_PROFILER_NGS
ROLE_ADMIN_PROFILER_NGS > ROLE_GUEST
ROLE_GUEST > ROLE_GUEST_PROFILER_NGS
ROLE_ADMIN > ROLE_ADMIN_PROFILER_CGH
ROLE_ADMIN_PROFILER_CGH > ROLE_GUEST
ROLE_GUEST > ROLE_GUEST_PROFILER_CGH
</beans:value>
</beans:property>
</beans:bean>
非常感谢,如果您有其他想法可以测试,欢迎您...
EDIT 3:
为什么 RoleHierarchyVoter 返回 0,WebExpressionVoter 返回 1...这正常吗?
14:48:32,861 调试 FilterSecurityInterceptor:194 - 安全对象:FilterInspiration:URL:/Home;属性:[hasRole('ROLE_GUEST')]
14:48:32,861 调试 FilterSecurityInterceptor:310 - 先前已验证:org.springframework.security.authentication.UsernamePasswordAuthenticationToken@43a64f5f:主体:com.clb.genomic.lyon.model.User@d46:用户名:jp;密码保护];启用:真;帐户未过期:true;凭证未过期:true;帐户非锁定:true;授予权限:ROLE_ADMIN_PROFILER_NGS;凭证:[受保护];已验证:真实;详细信息:org.springframework.security.web.authentication.WebAuthenticationDetails@0:RemoteIpAddress:192.168.154.18;会话 ID:084939D4E097F41ACA6A1F24CD8390BE;授予权限:ROLE_ADMIN_PROFILER_NGS
14:48:32,861 DEBUG RoleHierarchyImpl:117 - getReachableGrantedAuthorities() - 从角色 [ROLE_ADMIN_PROFILER_NGS] 可以通过零个或多个步骤到达 [ROLE_GUEST_PROFILER_NGS、ROLE_GUEST_PROFILER_CGH、ROLE_ADMIN_PROFILER_NGS、ROLE_GUEST]。
14:48:32,861 调试 AffirmativeBased:65 - 投票者:org.springframework.security.access.vote.RoleHierarchyVoter@6ff43d69,返回:0
14:48:32,862 DEBUG RoleHierarchyImpl:117 - getReachableGrantedAuthorities() - 从角色 [ROLE_ADMIN_PROFILER_NGS] 可以通过零个或多个步骤到达 [ROLE_GUEST_PROFILER_NGS、ROLE_GUEST_PROFILER_CGH、ROLE_ADMIN_PROFILER_NGS、ROLE_GUEST]。
14:48:32,862 调试 AffirmativeBased:65 - 投票者:org.springframework.security.web.access.expression.WebExpressionVoter@3fe932d5,返回:1
14:48:32,862 调试 FilterSecurityInterceptor:215 - 授权成功
EDIT 5 :
<beans:bean id="login" class="com.clb.genomic.lyon.beans.LoginBean" scope ="session">
<beans:property name="authenticationManager" ref="authenticationManager" />
</beans:bean>
> <security:authentication-manager alias="authenticationManager">
> <security:authentication-provider user-service-ref="userBo" >
> <security:password-encoder ref="standardPasswordEncoder"/>
> </security:authentication-provider>
> </security:authentication-manager>