Contudo, o usuário colocando no form o usuário e senha e clicando em OK, o usuário consegue autenticar normalmente.
O que quero é o seguinte, eu tenho o usuario e senha e gostaria de envia-lo para a action /j_acegi_security_check sem usar a JSP, ou seja, sem clicar no OK.
importorg.apache.commons.httpclient.*;importorg.apache.commons.httpclient.cookie.CookiePolicy;importorg.apache.commons.httpclient.cookie.CookieSpec;importorg.apache.commons.httpclient.methods.*;/** * <p> * A example that demonstrates how HttpClient APIs can be used to perform * form-based logon. * </p> * * @author Oleg Kalnichevski * */publicclassFormLoginDemo{staticfinalStringLOGON_SITE="localhost";staticfinalintLOGON_PORT=8080;publicFormLoginDemo(){super();}publicstaticvoidmain(String[]args)throwsException{HttpClientclient=newHttpClient();client.getHostConfiguration().setHost(LOGON_SITE,LOGON_PORT,"http");client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);// 'developer.java.sun.com' has cookie compliance problems// Their session cookie's domain attribute is in violation of// the RFC2109/ We have to resort to using compatibility// cookie policyGetMethodauthget=newGetMethod("/Teste/j_acegi_security_check");client.executeMethod(authget);System.out.println("Login form get: "+authget.getStatusLine().toString());// release any connection resources used by the methodauthget.releaseConnection();// See if we got any cookiesCookieSpeccookiespec=CookiePolicy.getDefaultSpec();Cookie[]initcookies=cookiespec.match(LOGON_SITE,LOGON_PORT,"/",false,client.getState().getCookies());System.out.println("Initial set of cookies:");if(initcookies.length==0){System.out.println("None");}else{for(inti=0;i<initcookies.length;i++){System.out.println("- "+initcookies[i].toString());}}PostMethodauthpost=newPostMethod("/Teste/j_acegi_security_check");// Prepare login parametersNameValuePairaction=newNameValuePair("action","Teste");NameValuePairurl=newNameValuePair("url","/j_acegi_security_check");NameValuePairuserid=newNameValuePair("j_username","teste");NameValuePairpassword=newNameValuePair("j_password","teste");authpost.setRequestBody(newNameValuePair[]{action,url,userid,password});client.executeMethod(authpost);System.out.println("Login form post: "+authpost.getStatusLine().toString());// release any connection resources used by the methodauthpost.releaseConnection();// See if we got any cookies// The only way of telling whether logon succeeded is// by finding a session cookieCookie[]logoncookies=cookiespec.match(LOGON_SITE,LOGON_PORT,"/",false,client.getState().getCookies());System.out.println("Logon cookies:");if(logoncookies.length==0){System.out.println("None");}else{for(inti=0;i<logoncookies.length;i++){System.out.println("- "+logoncookies[i].toString());}}// Usually a successful form-based login results in a redicrect to// another urlintstatuscode=authpost.getStatusCode();if((statuscode==HttpStatus.SC_MOVED_TEMPORARILY)||(statuscode==HttpStatus.SC_MOVED_PERMANENTLY)||(statuscode==HttpStatus.SC_SEE_OTHER)||(statuscode==HttpStatus.SC_TEMPORARY_REDIRECT)){Headerheader=authpost.getResponseHeader("location");if(header!=null){Stringnewuri=header.getValue();if((newuri==null)||(newuri.equals(""))){newuri="/";}System.out.println("Redirect target: "+newuri);GetMethodredirect=newGetMethod(newuri);client.executeMethod(redirect);System.out.println("Redirect: "+redirect.getStatusLine().toString());// release any connection resources used by the methodredirect.releaseConnection();}else{System.out.println("Invalid redirect");System.exit(1);}}}}
E a conexão foi feita com sucesso.
Como faço para redirecionar para a url final, dentro de uma servlet ou action do struts?