java 使用 Spring 授权标签检查用户是否登录时出错?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5481879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:19:24  来源:igfitidea点击:

Error using Spring authorize tag to check to see if user is logged in?

javajspspring-security

提问by SJS

Trying to test to see if a user is logged in I am using the following code:

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

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

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

But I am getting the following erorr?

但我收到以下错误?

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)

回答by John

Setting use-expressions="true" in the http element will work but means all your security settings in both Java code and security contexts must use expression notations. This can be a problem if you are currently using the standard security notations.

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

To use both expressions and standard notations just declare a new bean in your security context like so -

要同时使用表达式和标准符号,只需在您的安全上下文中声明一个新的 bean,就像这样 -

<beans:bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"/>

回答by Philipp Hügelmeyer

To use expressions to secure individual URLs, you would first need to set the use-expressions attribute in the element to true

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

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

<http use-expressions="true">请参阅 Spring Security 文档