2012-07-21 74 views
18

僅當用戶通過身份驗證時,我無法在JSP頁面中顯示註銷鏈接。這裏是例外,我在這條線的JSP頁面:Spring Security - 在應用程序上下文中找不到可見的WebSecurityExpressionHandler實例

<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) 

這裏是我的應用程序上下文的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代碼中使用表達式。有沒有解決方法?

+0

一個無關的觀察。您的配置中的「daoAuthenticationProvider」和「authenticationManager」未被使用。 – 2012-07-21 22:41:54

回答

41

您只需添加一個到你的應用環境

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

,但最簡單的方法就是讓你的<http>配置表情,一會爲你添加。這隻意味着您必須使用該塊內的表達式,而不是Java代碼,例如方法@Secured註釋。

+1

你說得對,我不得不在區塊中使用表達式。問題已經解決了。 – dukable 2012-07-21 21:00:24

+10

並且絕對明確,您的標籤應包含設置爲true的「use-expressions」屬性,例如, 2012-11-21 18:01:27

相關問題