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

z8dt9xmd  于 2023-10-15  发布在  Spring
关注(0)|答案(2)|浏览(133)

只有当用户经过身份验证时,我才能在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”,但这意味着我必须在拦截url标签和java代码中使用expression。是否有变通办法?

hyrbngr7

hyrbngr71#

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

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

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

oalqel3c

oalqel3c2#

我分享了对我有用的东西:
1.加豆
<b:bean id="jspExpresionHandler" class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"> </b:bean>
1.在'http'中添加使用表达式
<http auto-config="true" use-expressions="true" pattern="/**">
1.将global-method-security添加到
<global-method-security pre-post-annotations="enabled"> <expression-handler ref="jspExpresionHandler"/> </global-method-security>

相关问题