Why does calling the security authentication property `principal.displayName` in a decorator throw an exception?

流过昼夜 提交于 2019-12-06 08:19:27

Your request's Authentication object at that point is an instance of the AnonymousAuthenticationToken class, and that class does not have a property called displayName.

Clearly, SpringSecurity believes that the user is not logged. You probably need to

  • change the access rules so that that JSP can only be viewed when the user is logged in, or

  • change the JSP so to something like the following (assuming that you are using Spring 3.0.x and you've enabled web security expressions).


<c:set var="displayName">
    <sec:authorize access="isAuthenticated()">
        <sec:authentication property="principal.displayName" />
    </sec:authorize>
</c:set>

References:

Followed by the Answer of Stephen C and the References given by him

I successfully wrote my code As

<sec:authorize access="hasAnyRole('ROLE_DEFINED_1','ROLE_DEFINED_2')">
    <sec:authentication property="principal.displayName" />
</sec:authorize>

Where, ROLE_DEFINED_1 and ROLE_DEFINED_2 are Roles defined in your application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!