0

I am having the logged user name displayed in my thyme-leaf page and now i would like to save the name in my db together with the filled form

<div sec:authorize="is Authenticated()">
<span sec:authentication="name" class="form-control"> </span>                                       </div>

that how am fetching the logged user from db .....now how can i make the same name saved in any table i want within a form with other fields that are currently being saved,,,,how do i capture the displayed name?

dru
  • 1
  • 2

1 Answers1

0

You should be finding the currently logged in user. You can do that in your controller then pass this information to your Service and Repository layers respectively. Please check this for a good SO Q&A. Here is the quick solution:

Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String userName = auth.getPrincipal();
Ahmet
  • 1,045
  • 13
  • 28
  • thanks for respond but you didnt get my question.....okay now that i have the name in the UI how do i save it as i save my other fields in my form – dru Feb 15 '19 at 11:02
  • You dont need to get it from the UI, as my answer states you should be getting the current user from the Spring Security Context. – Ahmet Feb 15 '19 at 13:29