Spring安全登录表单是否相当于Httpclient post方法?

时间:2012-05-01 19:29:43

标签: forms login spring-security httpclient

SPRING SECURITY LOGIN FORM 这是login.jsp,即http://localhost:8082/Spring-MVC/login.jsp

<form name='f' action="<c:url value='j_spring_security_check' />"
        method='POST'>

        <table>
            <tr>
                <td>User:</td>
                <td><input type='text' name='j_username' value=''>
                </td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type='password' name='j_password' />
                </td>
            </tr>
            <tr>
                <td colspan='2'><input name="submit" type="submit"
                    value="submit" />
                </td>
            </tr>
            <tr>
                <td colspan='2'><input name="reset" type="reset" />
                </td>
            </tr>
        </table>

    </form>

JSP代码等同于上面的post form方法,

<%  HttpClient httpClient = new HttpClient();
            PostMethod postMethod = new PostMethod("http://localhost:8082/SpringMVC/j_spring_security_check");
            postMethod.addParameter("j_username", "mkyong");
            postMethod.addParameter("j_password", "123456");
            try {
                httpClient.executeMethod(postMethod);
            } catch (HttpException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
                String resp = postMethod.getResponseBodyAsString();
            } else {
                 //...postMethod.getStatusLine();
            }
            %>

实际上,我认为两者结果相同,但在第二种情况下,使用post方法,它不会将我重定向到主页。我想使用httpclient代码自动进行上面的表单提交。请帮助我

1 个答案:

答案 0 :(得分:0)

您没有在post请求中从JSP传播会话,因此,您正在向身份验证过滤器发送一个丢弃请求