java - How to send received jsessionid via spring 4 resttemplate -
i'm writing messenger javafx , spring4 on client-site , spring4 on server-site. secured server spring-security 3.2. problem: have loginpage on client witch sends login information spring-security , receive jsessionid cookie. works fine when try send jsessionid request become
org.springframework.web.client.restclientexception: not extract response: no suitable httpmessageconverter found response type [class org.messenger.rest.jsonconversationresult] , content type [text/html;charset=utf-8]
server inizializer
public class springmvcinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class[] {applicationconfig.class}; } @override protected class<?>[] getservletconfigclasses() { return new class[] {webconfig.class}; } @override protected string[] getservletmappings() { return new string[] {"/"}; } }
server securityinizializer
public class springsecurityinitializer extends abstractsecuritywebapplicationinitializer { }
server securityconfig
@configuration @enablewebsecurity public class securityconfig extends websecurityconfigureradapter { @autowired private drivermanagerdatasource datasource; @override protected void configure(authenticationmanagerbuilder auth) throws exception { string authquery = "select userid, authority user userid = ?"; string userquery = "select userid, pw, enabled user userid = ?"; auth.jdbcauthentication().datasource(datasource) .passwordencoder(passwordencoder()) .usersbyusernamequery(userquery) .authoritiesbyusernamequery(authquery); } @override protected void configure(httpsecurity http) throws exception { http .authorizerequests() .antmatchers("/register").permitall() .antmatchers("/getconvs", "/getcontacts").hasrole("user") .and() .formlogin() .and() .csrf().disable(); } @bean public authenticationentrypoint authenticationentrypoint() { return new de.daschner.messenger.security.authenticationentrypoint(); } @bean public successhandler successhandler() { return new successhandler(); } @bean public simpleurlauthenticationfailurehandler failurehandler() { return new simpleurlauthenticationfailurehandler(); } @bean public authenticationmanager authenticationmanager() throws exception { return super.authenticationmanager(); } @bean public passwordencoder passwordencoder() { return new bcryptpasswordencoder(11); } }
server requestmapping secured "page"
@requestmapping(value="/getconvs", method={requestmethod.get}, produces={mediatype.application_json_value}) public @responsebody jsonconversationresult getconvslist(httpservletrequest request, @requestparam(value="uid") string uid){ jsonconversationresult ret = new jsonconversationresult(); map<string, map<date, string>> convs = convservice.getconvslist(uid); if (convs != null) { ret.setconversations(convs); ret.setmessage("ok"); ret.seterror(0); } else { ret.seterror(1); ret.setmessage("verbindungsfehler"); } return ret; }
client send login , cookie
map<string, string> loginform = new hashmap<string, string>(); loginform.put("username", user); loginform.put("password", pw); httpentity<map<string, string>> login = new httpentity<map<string, string>>(loginform); responseentity<httpservletresponse> response = resttemplate.exchange( "http://localhost:8080/messenger-webapp/login", httpmethod.post, login, httpservletresponse.class); httpheaders headers = response.getheaders(); set<string> keys = headers.keyset(); string cookie = ""; (string header : keys) { if (header.equals("set-cookie")) { cookie = headers.get(header).get(0); } } string jsessionid = cookie.split(";")[0]; conf.setjsessionid(jsessionid.split("=", 2)[1]); return ret;
client send jsessionid request
responseentity<jsonconversationresult> response = resttemplate.exchange( "http://localhost:8080/messenger-webapp/getconvs?uid=" + uid, httpmethod.get, getauthheader(), jsonconversationresult.class); jsonconversationresult ret = response.getbody(); return ret; private httpentity<string> getauthheader() { httpheaders requestheaders = new httpheaders(); requestheaders.add("cookie", "jsessionid=" + config.getjsessionid()); return new httpentity<string>(requestheaders); }
i hope can me.
edit:
ok figured out problem not jsessionid wasn't sent correctly. login incorrect , query user database.
the correct login-post
clienthttpresponse response = resttemplate.execute( "http://localhost:8080/messenger-webapp/login", httpmethod.post, new requestcallback() { @override public void dowithrequest(clienthttprequest request) throws ioexception { request.getbody().write(("username=" + user + "&password=" + pw).getbytes()); } }, new responseextractor<clienthttpresponse>() { @override public clienthttpresponse extractdata(clienthttpresponse response) throws ioexception { return response; } });
the correct query
string authquery = "select u.userid, r.role_name user u, role r, user_role u.dbid = a.user_id , r.dbid = a.role_id , u.userid = ?";
i hope other people. if has alternative please let me know.
ok figured out problem not jsessionid wasn't sent correctly. login incorrect , query user database.
the correct login-post
clienthttpresponse response = resttemplate.execute( "http://localhost:8080/messenger-webapp/login", httpmethod.post, new requestcallback() { @override public void dowithrequest(clienthttprequest request) throws ioexception { request.getbody().write(("username=" + user + "&password=" + pw).getbytes()); } }, new responseextractor<clienthttpresponse>() { @override public clienthttpresponse extractdata(clienthttpresponse response) throws ioexception { return response; } });
the correct query
string authquery = "select u.userid, r.role_name user u, role r, user_role u.dbid = a.user_id , r.dbid = a.role_id , u.userid = ?";
i hope other people. if has alternative please let me know.
Comments
Post a Comment