java - get current user in service layer -


i want current user in service layer

i tried following code null pointer exception

@service public class myservice{  @autowired todorepository todorepository;  public void execute(){ authentication auth = securitycontextholder.getcontext().getauthentication(); todo todo = new todo(); todo.setdomain(auth.getname()); todorepository.save(todo);               }  

can show me how can login user spring security in service layer ?

this find authenticated user :

 @override     public person getcurrentlyauthenticateduser() {         authentication authentication = securitycontextholder.getcontext().getauthentication();         if (authentication == null) {             return null;         } else { // line below calls method person database, search users email(username)             return persondao.findpersonbyusername(authentication.getname());         }     } 

hope helps. if not, please let me kno,w delete answer.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -