【问题标题】:Spring Security - No visible WebSecurityExpressionHandler instance could be found in the application contextSpring Security - 在应用程序上下文中找不到可见的 WebSecurityExpressionHandler 实例
【发布时间】:2012-07-20 14:25:38
【问题描述】:

仅当用户通过身份验证时,我无法在 JSP 页面中显示注销链接。这是我在 JSP 页面的这一行遇到的异常:

<sec:authorize access="isAuthenticated()">

例外:

Stacktrace:
....

root cause

javax.servlet.jsp.JspException: No visible WebSecurityExpressionHandler instance could be found in the application context. There must be at least one in order to support expressions in JSP 'authorize' tags.
    org.springframework.security.taglibs.authz.AuthorizeTag.getExpressionHandler(AuthorizeTag.java:100)
    org.springframework.security.taglibs.authz.AuthorizeTag.authorizeUsingAccessExpression(AuthorizeTag.java:58)

这是我的 application-context-Security.xml:

<http auto-config='true' >
    <intercept-url pattern="/user/**" access="ROLE_User" />
    <logout logout-success-url="/hello.htm" />
</http>

<beans:bean id="daoAuthenticationProvider"
    class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>

<beans:bean id="authenticationManager"
    class="org.springframework.security.authentication.ProviderManager">
    <beans:property name="providers">
        <beans:list>
            <beans:ref local="daoAuthenticationProvider" />
        </beans:list>
    </beans:property>
</beans:bean>

<authentication-manager>
    <authentication-provider user-service-ref="userDetailsService">
        <password-encoder hash="plaintext" />
    </authentication-provider>
</authentication-manager>

我知道我可以在 http 标记中使用 use-expression="true" 但这意味着我必须在 intercept-url 标记和 java 代码中使用表达式。有解决办法吗?

【问题讨论】:

  • 一个不相关的观察。您的配置中的daoAuthenticationProviderauthenticationManager 未被使用。

标签: spring spring-security


【解决方案1】:

您可以在应用程序上下文中添加一个

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

但最简单的方法是在您的&lt;http&gt; 配置中启用表达式,我们会为您添加一个。这仅意味着您必须在该块中使用表达式,而不是在 Java 代码中,例如方法 @Secured 注释。

【讨论】:

  • 你说得对,我只需要在 块中使用表达式。问题解决了。
  • 为了绝对明确,您的 标签应该包含一个设置为 true 的“use-expressions”属性,例如
猜你喜欢
  • 2015-06-24
  • 2012-09-28
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2018-03-07
  • 2016-09-27
相关资源
最近更新 更多