我准备并扩展了这个existing spring example,它运行得很好。您可以简单地使用"user“和"password”登录,然后您将被转发到user/index。
使用此控制器
@Controller
public class LoginController {
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/login-error")
public String loginError(Model model) {
model.addAttribute("loginError", true);
return "login";
}
}
但是,只要我运行使用WebClient的示例测试,相同的登录就会导致异常:
com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 405 Request method 'POST' not supported for http://localhost:8080/login
这很奇怪,因为应用程序本身工作得很好。
EDIT:这是导致问题的测试方法
@And("^the user clicks the login button$")
public void theUserClicksTheLoginButton() throws IOException {
page = page.getElementById("login").click();
}
我没想到WebClient的单击方法是使用POST,而不是真正执行html中的输入域。
<form th:action="@{/login}" method="post">
<label for="username">Username</label>:
<input type="text" id="username" name="username" autofocus="autofocus" /> <br />
<label for="password">Password</label>:
<input type="password" id="password" name="password" /> <br />
<input type="submit" id="login" value="Log in" />
</form>
编辑2:好的,也许我应该把我的问题说清楚一点。我知道登录应该通过POST完成,并且我的@Controller
只提供@GetMapping
,但这是可以的,因为spring security正在处理POST请求,正如我在登录时在标题中看到的那样:
我的问题是为什么它在运行应用程序时运行得很好,而为什么在使用WebClient时没有同样的行为。
发布于 2018-06-06 00:09:01
我不太熟悉用Cucumber设置Spring,我也不确定在同一设置中混合使用SpringRunner
和Cucumber
运行程序。
我已经像这样更新了你的测试设置:
@RunWith(SpringRunner.class)
@WebMvcTest
@ContextConfiguration
@Import(SecurityConfig.class)
public class LoginFeatureStepDefinition {
private String username;
private String password;
private HtmlPage page;
@Autowired
private WebClient webDriver;
@WebMvcTest
替换了@SpringBootTest
,因为mockmvc自动配置将为您处理you客户端设置。如果您想要启动包含整个应用程序的实际服务器,并使用HTTP客户端对其进行测试,则需要设置in a different way.发布于 2018-05-31 04:21:28
web客户端正在通过POST
请求调用服务,这导致了异常:Request method 'POST' not supported
.The服务端看起来很好。
https://stackoverflow.com/questions/50612767
复制相似问题