Spring Security Migration 6.1.4:在Spring Security XML Configuration中使用hasAnyRole()时面临的问题[duplicate]

sigwle7e  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(134)

此问题在此处已有答案

Spring Security Role Based Authorization is not working(1个答案)
21天前关闭
我目前正在将应用程序迁移到JDK 17,Apache Tomcat 10.1和Spring Framework 6.0。作为此迁移的一部分,Spring Security框架已迁移到6.1.4。在完成必要的代码和配置更改后,Tomcat应用程序服务器的启动成功,并且能够登录到应用程序。但是,当我试图从应用程序中访问特定的网页时,它会给出403的HTTP响应。
下面是进一步分析问题后得到的一些细节。它是一个GET请求,URL如下:

<base_url>/SwiftWeb/pages/client/client_tab.jsf?sml_current_id=51828371

我检查了Spring Security配置文件(applicationContext-security.xml),并观察到对于此网页,以下是安全配置:

  1. <security:intercept-url pattern="/pages/client/**"
  2. access="hasAnyRole('BROKERPERSON, BROKERPERSONVIEW, PROCESSOWNER')" />

字符串
以下是Spring Security配置文件的片段,其中配置了不同页面的角色:

  1. <security:http auto-config="true" use-expressions="true" disable-url-rewriting="true">
  2. <security:form-login login-page="/pages/login.jsf"/>
  3. <security:custom-filter position="PRE_AUTH_FILTER"
  4. ref="preAuthFilter" />
  5. <security:intercept-url pattern="/pages/client/**"
  6. access="hasAnyRole('BROKERPERSON, BROKERPERSONVIEW, PROCESSOWNER')" />
  7. <security:intercept-url pattern="/pages/contacts/**"
  8. access="hasAnyRole('BROKERPERSON, COMPANYADMIN, BRANCHADMIN, PROCESSOWNER, ACCOUNTANT, PREMIUMFUNDINGUSER, SYSTEM')" />
  9. <security:intercept-url pattern="/pages/report/**"
  10. access="permitAll" />
  11. <security:intercept-url pattern="/pages/home/**"
  12. access="permitAll" />
  13. <security:intercept-url pattern="/**/*.jsf" access="permitAll" />
  14. <security:intercept-url pattern="/**/*.gif" access="permitAll" />
  15. <security:intercept-url pattern="/**/*.js" access="permitAll" />
  16. <security:intercept-url pattern="/pages/security/password_update.jsf"
  17. access="hasAnyRole('ROLE_ANONYMOUS, BROKERPERSON, COMPANYADMIN, BRANCHADMIN, PROCESSOWNER, ACCOUNTANT, PREMIUMFUNDINGUSER, SYSTEM')" />
  18. <security:csrf disabled="true"/>
  19. <security:headers >
  20. <security:frame-options policy="SAMEORIGIN" />
  21. <security:hsts disabled="true"/>
  22. <security:content-type-options disabled="true"/>
  23. <security:xss-protection disabled="true"/>
  24. <security:cache-control disabled="true"/>
  25. </security:headers>
  26. </security:http>
  27. <!-- User Context bean defined as session scope using aop scoped proxy -->
  28. <bean id="userContext" class="com.swift.core.security.view.UserContext"
  29. scope="session">
  30. <aop:scoped-proxy proxy-target-class="true" />
  31. </bean>
  32. <!-- List of request handler(s) beans -->
  33. <bean id="verifyUserHandler"
  34. class="com.swift.core.security.common.handlers.VerifyUserRequestHandler" />
  35. <bean id="userAuthHandler"
  36. class="com.swift.core.security.common.handlers.UserAuthenticationRequestHandler" />
  37. <bean id="userAuthDummyHandler"
  38. class="com.swift.core.security.common.handlers.UserAuthenticationRequestDummyHandler" />
  39. <bean id="userParamHandler"
  40. class="com.swift.core.security.common.handlers.UserParameterRequestHandler" />
  41. <bean id="userWebSecurityExpressionHandler"
  42. class="com.swift.core.security.common.handlers.UserWebSecurityExpressionHandler" />
  43. <bean id="preAuthFilter" class="com.swift.core.security.filter.SwiftPreAuthFilter">
  44. <property name="checkForPrincipalChanges">
  45. <value>true</value>
  46. </property>
  47. <property name="handlers">
  48. <!-- List of request handler(s) -->
  49. <list>
  50. <ref bean="verifyUserHandler" />
  51. <ref bean="userAuthHandler" />
  52. <!--
  53. <ref bean="userAuthDummyHandler" />
  54. -->
  55. <ref bean="userParamHandler" />
  56. <ref bean="userWebSecurityExpressionHandler" />
  57. </list>
  58. </property>
  59. <property name="authenticationManager" ref="authenticationManager" />
  60. </bean>
  61. <bean id="preauthAuthProvider"
  62. class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
  63. <property name="preAuthenticatedUserDetailsService">
  64. <bean id="userDetailsServiceWrapper"
  65. class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
  66. <property name="userDetailsService" ref="preAuthUserDetailsService" />
  67. </bean>
  68. </property>
  69. </bean>
  70. <security:authentication-manager alias="authenticationManager">
  71. <security:authentication-provider
  72. ref="preauthAuthProvider">
  73. </security:authentication-provider>
  74. </security:authentication-manager>


在进一步调试java代码时观察到,在尝试访问此页面时,User Context具有BROKERPERSON和BROKERPERSON以及ROLE_BROKERPERSON和ROLE_BROKERSOWNER的角色和权限。因此,根据我的分析,后端代码在尝试访问页面时具有所需的用户角色和权限,仍然观察到403响应代码。此问题仅针对访问提到特定角色的URL模式进行观察。对于所有访问为全部的URL它工作正常。此外,相同的代码在旧版本中也能正常工作,即JDK 8,Apache Tomcat 8.5,Spring Framework 4.2.x和Spring Security 4.0.x。
作为一种变通方法,我将突出显示的配置从hasAnyRole更改为hasRole,如下所示:

  1. <security:intercept-url pattern="/pages/client/**"
  2. access="hasRole('BROKERPERSON')" />
  3. <security:intercept-url pattern="/pages/client/**"
  4. access="hasRole('BROKERPERSONVIEW')" />
  5. <security:intercept-url pattern="/pages/client/**"
  6. access="hasRole('PROCESSOWNER')" />


在此更改后,网页按预期正常工作,并且没有观察到403响应代码。然而,问题仍然是相同的,为什么当使用hasAnyRole时会获得403响应代码。我试图在技术论坛上找到答案,但以前没有看到这种类型的问题报告。请让我知道是否有人以前遇到过类似的问题,并采取措施解决它?

yx2lnoni

yx2lnoni1#

考虑使用hasAnyRole('BROKERPERSON', 'BROKERPERSONVIEW', 'PROCESSOWNER')。确保每个角色用逗号分隔。
有关详细信息,请参阅https://docs.spring.io/spring-security/reference/5.7/servlet/authorization/expression-based.html上的文档。

相关问题