gpt4 book ai didi

Spring 安全。每页显示用户名

转载 作者:行者123 更新时间:2023-12-02 11:39:20 26 4
gpt4 key购买 nike

我想在登录后存储用户信息,并在每个页面上显示我的登录名和用户名(使用jsp)。如何在 jsp View 中访问存储登录用户信息的 session bean?

最佳答案

使用authentication tag

This tag allows access to the current Authentication object stored in the security context. It renders a property of the object directly in the JSP. So, for example, if the principal property of the Authentication is an instance of Spring Security's UserDetails object, then using <sec:authentication property="principal.username" /> will render the name of the current user.

Of course, it isn't necessary to use JSP tags for this kind of thing and some people prefer to keep as little logic as possible in the view. You can access the Authentication object in your MVC controller (by calling SecurityContextHolder.getContext().getAuthentication()) and add the data directly to your model for rendering by the view.

关于 Spring 安全。每页显示用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3638308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com