【问题标题】:make a post from ajax to controller (Spring security)从 ajax 发布到控制器(Spring 安全)
【发布时间】:2017-05-27 19:01:39
【问题描述】:

你好有问题有错误

WARNING [http-nio-8080-exec-8] org.springframework.web.servlet.PageNotFound.handleHttpRequestMethodNotSupported Request method 'POST' not supported

当我试图在控制器上发帖时。我认为问题出在 Spring Security 中,我已将其添加到我的项目中。在它运作良好之前。

  <form  id="estimationForm" method="post" class="form-horizontal">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4><i class="fa fa-address-card-o" aria-hidden="true"></i> 평가</h4>
            </div>

            <!-- Estimate modal -->
            <div class="modal-body">
                <div class="form-group">
                    <div class="col-lg-12 text-left">
                        <input type="text" name="name" class="form-control form-block" placeholder="이름" required />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-12 text-left">
                        <input type="email" name="email" class="form-control form-block" placeholder="이메일" required />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-lg-12 text-left">
                        <textarea class="form-control form-block" name="message" rows="4" placeholder="아이디" required></textarea>
                    </div>
                </div>
            </div>

            <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>

            <!-- Estimate modal footer -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">닫기</button>
                <button type="button" onclick="estimate()" class="btn btn-primary">보내기</button>
            </div>
        </form>

这是我的 js `function estimate() {

var loginForm = document.forms["estimationForm"];
var name = loginForm.name.value;
var email = loginForm.email.value;
var message = loginForm.message.value;
alert(name +" " + email + " " + message);
$.ajax({
    type: "POST",
    data: {name: name , email: email , message: message},
    url: "estimate",
    dataType: "json",
    success: function(data) {
        if(data.url == "ok") {
            alert("message have been sent")
        } else if (data.url == ""){
            alert("fail")
        }
    },
});

}`

这是我的控制器

@Controller
@SessionAttributes("roles")
public class IndexPageController {



    @Autowired
    AuthenticationTrustResolver authenticationTrustResolver;


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage(Locale locale, Model model) {

    /*  // add parametrized message from controller
        String welcome = messageSource.getMessage("welcome.message", new Object[]{"John Doe"}, locale);
        model.addAttribute("message", welcome);*/

        // obtain locale from LocaleContextHolder
        Locale currentLocale = LocaleContextHolder.getLocale();
        model.addAttribute("locale", currentLocale);


        if (isCurrentAuthenticationAnonymous()) {
            return "index";
        } else {
            return "redirect:/list";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/estimate" ,  method = RequestMethod.POST)
    public String estimateWindow(@RequestParam("name") String recipientName, @RequestParam("email") String recipientMail,  @RequestParam("message") String recipientRequestText, Model model) {



        System.out.print("name " + recipientName + " email " + recipientMail + " text " + recipientRequestText);

        JSONObject myJsonObj = new JSONObject();

        myJsonObj.append("response", "ok");
        return myJsonObj.toString();
    }


    /**
     * This method returns true if users is already authenticated [logged-in], else false.
     */
    private boolean isCurrentAuthenticationAnonymous() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authenticationTrustResolver.isAnonymous(authentication);
    }


}

【问题讨论】:

    标签: ajax spring security spring-mvc controller


    【解决方案1】:

    您的 AJAX 函数正在尝试 POST 到 /login,您的控制器已将其定义为 GET 方法。

    尝试将您的登录方法更改为 POST,或创建另一个登录端点来处理 POST。

    【讨论】:

    • 在我的 ajax 中,url 是估计的,我试图将数据发送到控制器中的 /estimate 方法。问题是当我在没有 ajax 的情况下发布帖子时,只有 jsp 表单并提交 - 它运行良好。我认为是crsf token的问题,但是我不知道怎么写。
    • 我有表单
    • 也许我必须通过 ajax 发送 csrf 令牌。但我不知道怎么做。
    • 你得到的错误不是因为缺少 CSRF 令牌,而是因为你没有发布到正确的 URL。尝试向您的 JS 函数添加更多调试信息,仅输出 fail 不会有太大帮助。我先看看你的 URL,现在是 estimate,应该是 /estimate 吗?
    【解决方案2】:

    正如我所提到的,问题出在 Crsf 中。我确实喜欢本指南。

    https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html

    现在 ajax 在 /estimate 控制器上进行 POST

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多