java - Spring-Security : Optimizing get currently authenticated user… -


i working on spring-mvc application in using spring-security authentication. due excessive usage of getting authenticated user mechanism, profiler shows 'allocation hotspot' , 9.5kb memory consumed single user. there way optimize infrastructure.

personserviceimpl :

 @override     public person getcurrentlyauthenticateduser() {         authentication authentication = securitycontextholder.getcontext().getauthentication();         if (authentication == null) {             return null;         } else {             return persondao.findpersonbyusername(authentication.getname());         }     } 

if somehow can push user in cache retrieved after first retrieving, atleast should improve performance, have no idea how that. suggestions welcome. thanks.

you can use variants. can try ue spring: 1. extends org.springframework.security.core.userdetails.user , add userentity (person)

public class user extends org.springframework.security.core.userdetails.user {     private person sysuser;     public user(person sysuser) {         super(sysuser.getlogin(), sysuser.getpassword(), new arraylist<>());         this.sysuser = sysuser;     }  public person getyouruser(){ return sysuser; }  } 
  1. implements userdetailsservice using user entity , extended org.springframework.security.core.userdetails.user

    @service(value = "userservice") public class userservice implements userdetailsservice { @autowired securitydao securitydao;  @override public userdetails loaduserbyusername(string login) throws usernamenotfoundexception {     user user = null;     try {         person su = securitydao.getuseronlybylogin(login);         if (su != null)             user = new user(person);      } catch (exception e) {          e.printstacktrace();     }       return user; } 

    }

  2. configure spring (xml or annotation):

 <beans:bean id="userservice" class="your.pack.userservice"/>     <authentication-manager alias="authenticationmanager">         <authentication-provider user-service-ref="userservice"/>     </authentication-manager> 
  1. use method

    @override

       public person getcurrentlyauthenticateduser() {         authentication authentication = securitycontextholder.getcontext().getauthentication();          if (authentication == null) {             return null;         } else {  user user = (user) authentication.getprincipal();             return user.getyouruser();         }     } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -