spring security. display user name on every page

前端 未结 3 1481
执笔经年
执笔经年 2021-02-04 21:18

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 w

3条回答
  •  梦谈多话
    2021-02-04 21:51

    use the 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 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.

提交回复
热议问题