java - Spring 4.3.0 RELEASE - auto login not redirect to home page aftre regestration -
i build web application login & regestration from:
- after login (username , password) - directing working
- but after registration "auto login" , redirect user home page isn't working.
why happening?
usercontroller:
@controller public class usercontroller { @autowired private userservice userservice; @autowired private securityservice securityservice; @autowired private uservalidator uservalidator; @requestmapping(value = "/registration", method = requestmethod.get) public string registration(model model) { model.addattribute("userform", new userentity()); return "registration"; } @requestmapping(value = "/registration", method = requestmethod.post) public string registration(@modelattribute("userform") userentity userform, bindingresult bindingresult, model model) { uservalidator.validate(userform, bindingresult); if (bindingresult.haserrors()) { return "registration"; } userservice.save(userform); securityservice.autologin(userform.getname(), userform.getpasswordconfirm()); //model.addattribute("user", getprincipal()); return "home"; } @requestmapping(value = "/login", method = requestmethod.get) public string login(model model, string error, string logout) { if (error != null) model.addattribute("error", "your username , password invalid."); if (logout != null) model.addattribute("message", "you have been logged out successfully."); return "login"; } @requestmapping(value = { "/", "/home" }, method = requestmethod.get) public string homepage(modelmap model) { model.addattribute("user", getprincipal()); return "home"; } @requestmapping(value = "/admin", method = requestmethod.get) public string adminpage(modelmap model) { model.addattribute("user", getprincipal()); return "admin"; } private string getprincipal(){ string username = null; object principal = securitycontextholder.getcontext().getauthentication().getprincipal(); if (principal instanceof userdetails) { username = ((userdetails)principal).getusername(); } else { username = principal.tostring(); } return username; } }
customsuccesshandler:
@component public class customsuccesshandler extends simpleurlauthenticationsuccesshandler { private redirectstrategy redirectstrategy = new defaultredirectstrategy(); @override protected void handle(httpservletrequest request, httpservletresponse response, authentication authentication) throws ioexception { string targeturl = determinetargeturl(authentication); if (response.iscommitted()) { system.out.println("can't redirect"); return; } redirectstrategy.sendredirect(request, response, targeturl); } /* * method extracts roles of logged-in user , returns * appropriate url according his/her role. */ protected string determinetargeturl(authentication authentication) { string url = ""; collection<? extends grantedauthority> authorities = authentication.getauthorities(); list<string> roles = new arraylist<string>(); (grantedauthority : authorities) { roles.add(a.getauthority()); } if (isdba(roles)) { url = "/db"; } else if (isadmin(roles)) { url = "/admin"; } else if (isuser(roles)) { url = "/home"; } else { url = "/accessdenied"; } return url; } private boolean isuser(list<string> roles) { if (roles.contains("role_user")) { return true; } return false; } private boolean isadmin(list<string> roles) { if (roles.contains("role_admin")) { return true; } return false; } private boolean isdba(list<string> roles) { if (roles.contains("role_dba")) { return true; } return false; } public void setredirectstrategy(redirectstrategy redirectstrategy) { this.redirectstrategy = redirectstrategy; } protected redirectstrategy getredirectstrategy() { return redirectstrategy; } }
securityserviceimp:
@service("securityservice") public class securityserviceimpl implements securityservice { @autowired private authenticationmanager authenticationmanager; @autowired private userdetailsservice userdetailsservice; private static final logger logger = loggerfactory.getlogger(securityserviceimpl.class); @override public string findloggedinusername() { object userdetails = securitycontextholder.getcontext().getauthentication().getdetails(); if (userdetails instanceof userdetails) { return ((userdetails)userdetails).getusername(); } return null; } @override public void autologin(string name, string password) { userdetails userdetails = userdetailsservice.loaduserbyusername(name); usernamepasswordauthenticationtoken usernamepasswordauthenticationtoken = new usernamepasswordauthenticationtoken(userdetails, password, userdetails.getauthorities()); authenticationmanager.authenticate(usernamepasswordauthenticationtoken); if (usernamepasswordauthenticationtoken.isauthenticated()) { securitycontextholder.getcontext().setauthentication(usernamepasswordauthenticationtoken); logger.debug(string.format("auto login %s successfully!", name)); } } }
userdetailserviceimp:
@service("userdetailsservice") public class userdetailsserviceimpl implements userdetailsservice { @autowired private userrepository userrepository; @override @transactional(readonly = true) public userdetails loaduserbyusername(string name) throws usernamenotfoundexception { userentity user = userrepository.findbyname(name); set<grantedauthority> grantedauthorities = new hashset<>(); (roleentity role : user.getroles()){ grantedauthorities.add(new simplegrantedauthority(role.getname())); } return new org.springframework.security.core.userdetails.user(user.getname(), user.getpassword(), grantedauthorities); } }
appconfig-security.xml:
<?xml version="1.0" encoding="utf-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.1.xsd"> <http auto-config="true"> <intercept-url pattern="/" access="hasrole('role_user')"/> <intercept-url pattern="/home" access="hasrole('role_user')"/> <intercept-url pattern="/admin**" access="hasrole('role_admin')"/> <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/login?error" username-parameter="name" password-parameter="password" authentication-success-handler-ref="customsuccesshandler"/> <logout logout-success-url="/login?logout" /> </http> <authentication-manager alias="authenticationmanager"> <authentication-provider user-service-ref="userdetailsserviceimpl"> <password-encoder ref="encoder"></password-encoder> </authentication-provider> </authentication-manager> <beans:bean id="userdetailsserviceimpl" class="com.searcher.service.userdetailsserviceimpl"></beans:bean> <beans:bean id="customsuccesshandler" class="com.searcher.configuration.customsuccesshandler" /> <beans:bean id="encoder" class="org.springframework.security.crypto.bcrypt.bcryptpasswordencoder"> <beans:constructor-arg name="strength" value="11"/> </beans:bean> </beans:beans>
Comments
Post a Comment