在Spring应用程序中使用apache的反向代理时如何避免404

时间:2019-04-18 17:20:18

标签: spring

我在spring 5上有一个小应用程序。这是一个带有index.html的REST服务,其中包含一个表单。该表格将邮寄请求发送到其余服务。不幸的是,当我发送请求时,我得到了404。

@RestController
@RequestMapping("/")
public class RegistrationController {

   ...

    public RegistrationController(RegistrationService registrationService, HttpServletRequest request) {
        this.registrationService = registrationService;
        this.request = request;
    }

    @PostMapping("/register")
    @ResponseBody
    public ResponseEntity register(@ModelAttribute("loginForm") LoginForm loginForm) {

        logger.log(Level.DEBUG, () -> "Registration request from " + getClientIP() + " for username " + loginForm.getUsername());

        if (!loginForm.isValid()) {
            logger.log(Level.DEBUG, () -> "Invalid Form received");
            return new ResponseEntity(HttpStatus.BAD_REQUEST);
        }

        final States.REGISTRATION_CHECK_STATE regState = registrationService.register(loginForm.getUsername(), loginForm.getPassword(), loginForm.getRegistrationPassword(), getClientIP());
        return new ResponseEntity<>(regState, HttpStatus.OK);
    }

...

我使用的html是:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
    <title>Register your matrix account on asdf.de (alpha!)</title>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
</head>
<body>
<form action="/register" id="submitForm" method="post">
    username: <input name="username" type="text"><br>
    password: <input name="password" type="password"><br>
    registration password: <input name="registrationPassword" type="password"><br>
    <input type="submit" value="Submit">
</form>

<script type="text/javascript">
    $('#submitForm').submit(function (e) {
        // reference to form object
        var form = this;
        // for stopping the default action of element
        e.preventDefault();
        // mapthat will hold form data
        var formData = {}
        //iterate over form elements
        $.each(this, function (i, v) {
            var input = $(v);
            // populate form data as key-value pairs
            // with the name of input as key and its value as value
            formData[input.attr("name")] = input.val();
        });
        $.ajax({
            type: form.attr('method'), // method attribute of form
            url: form.attr('action'),  // action attribute of form
            dataType: 'json',
            // convert form data to json format
            data: JSON.stringify(formData),
        });
    });
</script>

</body>
</html>

有人可以告诉我我该怎么做吗? 当我单击“提交”按钮时,它会调用/ register,这是错误的,因为未在apache配置中进行配置。

我的Apache的配置:

<Location /registration>
            ProxyPreserveHost on
            RequestHeader set X-Forwarded-Proto https
            RequestHeader set X-Forwarded-Port 443
            ProxyPass http://127.0.0.1:8080/ nocanon
            ProxyPassReverse http://localhost:8080/
    </Location>

当我在intellij中运行该应用程序时,它可以按预期工作。 它是我的asdf.de域(matrix.asdf.de)的子域。 我是否必须仅为该应用程序创建一个子域才能映射/代理所有请求?

编辑: 我现在已经在其自己的子域上运行该应用程序。感觉有点矫kill过正,但至少可以起作用。

0 个答案:

没有答案