Spring Security - 在应用程序上下文中找不到可见的WebSecurityExpressionHandler实例

duk*_*ble 20 spring spring-security

仅当用户通过身份验证时,我才在JSP页面中显示注销链接时遇到问题.以下是我在JSP页面这一行的例外情况:

<sec:authorize access="isAuthenticated()">
Run Code Online (Sandbox Code Playgroud)

例外:

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)
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

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

Sha*_*eep 45

您只需在应用程序上下文中添加一个即可

<bean id="webexpressionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler" /> 
Run Code Online (Sandbox Code Playgroud)

但最简单的方法是在<http>配置中启用表达式,并为您添加一个表达式.这只意味着您必须在该块中使用表达式,而不是在Java代码中使用,例如方法@Secured注释.

  • 并且绝对明确,您的<http>标记应包含设置为true的"use-expressions"属性,例如<http use-expressions ="true"> (11认同)