【发布时间】:2015-08-09 09:51:01
【问题描述】:
我正在处理一个旧的 Spring 3.0.7 项目。我必须添加一个任何人都可以访问的jsp。我必须检查登陆此页面的人是经过身份验证的还是匿名的,所以我决定使用这个
<security:authorize access="isAuthenticated()">
...
</security:authorize>
<security:authorize access="isAnonymous()">
....
</security:authorize>
向用户显示适当的内容。当我以登录用户身份登陆jsp 时,一切都很好。当我在没有登录的情况下登陆时 isAuthenticated() 不会返回 false 但会引发异常:
java.lang.RuntimeException: org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.username' of bean class [org.springframework.security.authentication.AnonymousAuthenticationToken]: Bean property 'principal.username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
我对 Spring 很陌生,当然我想念一些东西。但是什么?
更新
这是我的jsp相关代码:
<%@ page isELIgnored="false" contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<content tag="top">
<security:authorize access="isAuthenticated()">
<!-- authenticated users content -->
</security:authorize>
<security:authorize access="isAnonymous()">
<li><a href="${contextPath}/"><i class="login"></i> Login</a></li>
</security:authorize>
</content>
...
【问题讨论】:
-
@underdog 你好,我看到了这两个问题,但我找不到它们有用,也许我对 Spring 太陌生了。你可以添加一些信息吗?谢谢。
-
您确定问题出在 isAuthenticated() 上吗?对我来说,您似乎正在尝试访问主体属性,但未登录的用户主体对象不存在。
-
@jgr 不,我不太确定。看着docs.spring.io/autorepo/docs/spring-security/3.0.7.RELEASE/…,我希望
isAuthenticated()回复我false,也不例外。我是否误解了如何使用该功能?
标签: java spring jsp spring-mvc spring-security