spring mvc - SecurityContextHolder.getContext().getAuthentication().getPrincipal() is null on Controller -


i have spring mvc application i'm exposing endpoint, , small library wrote common functionality.

i have utility class this:

class securityutil {     public static principal getprincipal(){        return securitycontextholder.getcontext().getauthentication()               .getprincipal();     }  } 

and controller i'm doing like:

 class mycontroller {        public responseentity<void> myendpoint(){            //            principal principal = securityutil.getprincipal();            // use principal information audit processes       }  } 

in case principal null, if replace code this:

 class mycontroller {        public responseentity<void> myendpoint(){            //            principal principal = securitycontextholder.getcontext()                                                       .getauthentication()                                                       .getprincipal();            // use principal information audit processes       }  } 

in case principal not null , has information need.
know happening?

i going through same problem , have solved in following manner.

create userservice interface

public interface userservice {     string getloggedinusername();     user getloggedinuser(); } 

provide implementation userservice, however, can without creating interface , creating userservice class.

@service public class userserviceimpl implements userservice {       private static log log = logfactory.getlog(userserviceimpl.class);      @override     public string getloggedinusername() {         try {             return getloggedinuser().getusername();         } catch (exception ex) {             throw new usernamenotfoundexception("please log in", ex);         }     }      @override     public user getloggedinuser() {         authentication authentication = securitycontextholder.getcontext().getauthentication();          if (authentication.getprincipal() instanceof user) {             return (user) authentication.getprincipal();         } else {             throw new usernamenotfoundexception("user not authenticated; found " + authentication.getprincipal() + " of type " + authentication.getprincipal().getclass() + "; expected type user");         }     }  } 

and calling userservice.getloggedinusername() auto wiring userservice

@autowired userservice userservice 

update: if getting them in controller can pass principal principal method argument controller method instead of getting security context. auto-wired controller automatically , later on can pass service methods. way considered practice spring mvc, getting principal security context in service layer

@requestmapping(value = "/myendpoint", method = get) public responseentity<void> myendpoint(principal principal){    //    // use principal information audit processes } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -