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
Post a Comment