春季安全.在每一页上显示用户名 [英] spring security. display user name on every page

查看:62
本文介绍了春季安全.在每一页上显示用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在登录后存储用户信息,并在每个页面上显示我的登录名和用户名(使用jsp).如何在我的jsp视图中访问将存储已登录用户信息的会话bean?

I want to store user information after logging in and to display my login and username on every page (using jsp). How can I get access in my jsp views to the session bean that would store information of the user that is logged in?

推荐答案

使用

此标签允许访问当前 身份验证对象存储在 安全上下文.它呈现一个 对象的属性直接在 JSP.因此,例如,如果校长 身份验证的属性是 Spring Security的实例 UserDetails对象,然后使用 < sec:身份验证 property ="principal.username"/>将要 呈现当前用户的名称.

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.

当然,没有必要使用 JSP标签用于这种情况,并且 有些人宁愿少存钱 在视图中尽可能的逻辑.你可以 访问身份验证对象 您的MVC控制器(通过调用 SecurityContextHolder.getContext().getAuthentication()) 并将数据直接添加到您的 用于通过视图呈现的模型.

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.

这篇关于春季安全.在每一页上显示用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆