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