有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Spring授权标记检查用户是否登录时发生java错误?

正在尝试测试用户是否已登录,我正在使用以下代码:

<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>

<sec:authorize access="isAuthenticated()">
   YES, you are logged in!
</sec:authorize>

但我得到的是以下信息

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.
        at org.springframework.security.taglibs.authz.AuthorizeTag.getExpressionHandler(AuthorizeTag.java:100)
        at org.springframework.security.taglibs.authz.AuthorizeTag.authorizeUsingAccessExpression(AuthorizeTag.java:58)
        at org.springframework.security.taglibs.authz.AuthorizeTag.doStartTag(AuthorizeTag.java:48)

共 (2) 个答案

  1. # 1 楼答案

    在http元素中设置use expressions=“true”将起作用,但这意味着Java代码和安全上下文中的所有安全设置都必须使用表达式符号。如果您当前正在使用标准的安全性符号,这可能是一个问题

    要同时使用表达式和标准符号,只需在安全上下文中声明一个新bean,如下所示-

    <beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>
    
  2. # 2 楼答案

    要使用表达式保护各个URL,首先需要将元素中的use expressions属性设置为true

    <http use-expressions="true"> See Spring Security doc