0

我正在尝试使用 POST 方法调用我的 Spring MVC 控制器的 post 方法,这是我的控制器的方法

@Controller
@RequestMapping("springSecurity/login.json")
public class SpringSecurityLoginController
{

    @RequestMapping(method = RequestMethod.GET)
        @ResponseBody
        public SpringSecurityLoginStatus getStatus()
        {
            return springSecurityLoginService.getStatus();
        }

        @RequestMapping(method = RequestMethod.POST)
        @ResponseBody
        public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
                @RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)
        {

            LOG.info("Starting login process");
            return springSecurityLoginService.login(username, password, request, response);
        }
}

HTML

<form:form action="${request.contextPath}/j_spring_security_check" class="login-form " id="loginForm"  method="post" commandName="loginForm">

<form:input path="j_username" class="text" id="login_id" value="" name ="j_username"/>
<form:password path="j_password" class="text" value="Password" name="j_password" id="j_password"/>
<input type="submit" class="submit" value="LOGIN" id="login" />
</form:form>

这是我的 Jquery 代码

jQuery("#login").live('click', function(e) {
        e.preventDefault();

        jQuery.ajax({url: getHost() + "${request.contextPath}/springSecurity/login.json",
            type: "POST",
            beforeSend: function(xhr) {
                xhr.withCredentials = true;
            },
            data: jQuery("#loginForm").serialize(),
            success: function(data, status) {
                if (data.loggedIn) {
                   // location.href = getHost() + '${ctx}/users';
                    //login_pannel

                } else {
                    loginFailed(data);
                }
            },
            error: loginFailed
        });
    });

但是上面的代码正在调用

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public SpringSecurityLoginStatus getStatus()

并不是

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
@RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)

我不确定为什么会发生这种行为

4

2 回答 2

0
@Controller
@RequestMapping("/springSecurity")

public class SpringSecurityLoginController
{

    @RequestMapping(value="/login.json",method = RequestMethod.GET)
        @ResponseBody
        public SpringSecurityLoginStatus getStatus()
        {
            return springSecurityLoginService.getStatus();
        }

        @RequestMapping(value="/login.json",method = RequestMethod.POST)
        @ResponseBody
        public SpringSecurityLoginStatus login(@RequestParam("j_username") final String username,
                @RequestParam("j_password") final String password, final HttpServletRequest request, final HttpServletResponse response)
        {

            LOG.info("Starting login process");
            return springSecurityLoginService.login(username, password, request, response);
        }
}

Make this small change and try.

于 2012-06-14T07:16:27.993 回答
0

我猜您正在使用为普通 HTTP POST 设计的服务器端代码,然后在成功后进行重定向 GET。

如果您想使用 Ajax(并且为用户登录使用 Ajax 是一种非常危险的情况,不能掉以轻心),那么您希望返回更合适的内容,例如 JSON,而不是进行重定向。

在 Firebug for Firefox 中,在 Net 选项卡中,打开该 302 响应并查看响应标头。我打赌你会发现那里有一个 Location 标头告诉浏览器重定向。

于 2012-06-14T07:36:52.937 回答