spring安全性中的beans.NotReadablePropertyException

Vin*_*C M 5 spring-security

春天安全我是新手.我拿起本书并尝试执行代码.

虽然我这样做,但我得到了

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?
Run Code Online (Sandbox Code Playgroud)

我的spring-security xml配置:

<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/login.do" access="permitAll"/>
    <intercept-url pattern="/*" access="hasRole('ROLE_USER')"/>
    <form-login login-page="/login.do"/>
</http>


<authentication-manager alias="authenticationManager">

    <authentication-provider>
        <user-service id="userService">
            <user authorities="ROLE_USER" name="guest" password="guest"/>
        </user-service>
    </authentication-provider>
    <!-- Ch 3 Change Password Service -->
    <!-- 
    <authentication-provider user-service-ref="userService"/>
     -->
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

如果您需要任何其他信息,请与我们联系.

Ste*_*n C 10

错误消息似乎表明是某些东西试图访问一个不存在的属性AnonymousAuthenticationToken; 即spring会话未登录时使用的身份验证令牌.

我怀疑问题实际上发生在你的servlet代码中,或者是在试图通过spring安全标记访问当前用户名的JSP中.

错误的完整堆栈跟踪可能会给我们提供更多线索.至少它应该告诉我们异常的来源.

(对于它的价值,a AnonymousAuthenticationToken确实有一个principal属性,但该属性通常不是具有username属性的对象.实际上,它通常只是一个String.)

  • 对此深表歉意!请在我自己维护的 [非官方图书网站](http://www.springsecuritybook.com) 上找到这个和任何其他小勘误。与发布者一起更新代码以解决此问题的机制可能会导致更多问题:(--Peter(作者,_Spring Security 3_) (2认同)

小智 8

我正在阅读/关注"Spring Security 3"一书.只需将以下行添加到header.jsp问题是如果您未登录,则principal.username不存在.

<div class="username">
    Welcome, 
    <sec:authorize access="isAuthenticated()">  
        <strong><sec:authentication property="principal.username"/></strong>
    </sec:authorize>
</div>
Run Code Online (Sandbox Code Playgroud)