【问题标题】:No visible WebSecurityExpressionHandler instance could be found in the applicationContext在 applicationContext 中找不到可见的 WebSecurityExpressionHandler 实例
【发布时间】:2012-09-28 23:18:08
【问题描述】:

我正在尝试使用 Spring3.1 实现部分级别的安全性。使用 Thymeleaf2.0 作为我的视图部分。这是我为此所做的配置,

使用的罐子## - 所有 spring3.1 罐子thymeleaf-extras-springsecurity3.jar(版本 1.0.0.beta-1)

SpringWebFlow-Servlet.xml

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
     ...
     <property name="additionalDialects">
     <set>
         <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
     </set>
     </property>
     ...
</bean>
<bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" />

spring-security.xml

 <http auto-config="true" use-expressions="true">
     <intercept-url pattern="/productSelection" access="hasRole('ROLE_ADMIN')"/>
    .....
 </http>

xxx.html

<div sec:authorize="hasRole('ROLE_ADMIN')">
    This only be seen if authenticated user has role ROLE_ADMIN.
</div>

问题

获取异常说明: 在 applicationContext 中找不到可见的 WebSecurityExpressionHandler 实例

在 Spring3.1 中,DefaultWebSecurityExpressionHandler 没有实现 WebSecurityExpressionHandler,该接口已被弃用。请让我知道解决方法,因为 Thymeleaf 正在尝试搜索 ApplicationContext 中不可用的 WebSecurityExpressionHandler 实例。

【问题讨论】:

  • 您使用的是最新版本吗? DefaultWebSecurityExpressionHandler 确实实现了 WebSecurityExpressionHandler。您可以查看the source 进行检查。看thymeleaf项目,只提到了Spring Security 3.0.x,所以可能不兼容3.1。
  • @Luke,我使用的是 3.1.0,现在我已将其替换为 3.1.2。这并没有解决我的问题,但至少该异常已经消失。看起来是兼容性问题。谢谢!

标签: spring-security thymeleaf


【解决方案1】:

您需要在应用程序(根)上下文而不是 servlet 上下文中定义 bean。

<bean id="webSecurityExpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>

我有同样的问题将它从 servlet-context.xml 移动到 root-context.xml 并且 bean 被拾取。

根据您的 spring 设置,它可以在另一个 xml 文件中,因此请检查您的 web.xml 中的 Spring 上下文根目录。

在你的情况下,如果 spring-security.xml 被导入到你的 Spring 根上下文中,你可以在那里添加它。

【讨论】:

    【解决方案2】:

    这个问题已经很老了,但是对于任何在这里找到方法来补充 Daniel 的答案的人,您可以通过更改以下内容来更新您的 pom.xml 中的依赖项。

    <properties>
        ...
        <!-- <spring-security.version>3.1.0.RELEASE</spring-security.version> -->
        <spring-security.version>3.1.1.RELEASE</spring-security.version>
    </properties>
    

    如果有人感兴趣,我也会在博客中提供更多信息

    【讨论】:

      【解决方案3】:

      Spring Security 3.1.0 中的DefaultWebSecurityExpressionHandler 类没有实现WebSecurityExpressionHandler 接口,这可能是一个遗漏。

      这已在 Spring Security 3.1.1 和更新版本中解决,因此您只需更新您的 Spring Security 依赖项。

      【讨论】:

      • 丹尼尔,感谢您的回复。我已经这样做了,并且不再遇到该异常,但不知何故我的 sec:authorize 仍然无法正常工作(请参阅我的最后评论)。
      猜你喜欢
      • 2012-07-20
      • 2015-06-24
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      • 2015-12-07
      • 2020-11-20
      相关资源
      最近更新 更多