![](/img/trans.png)
[英]How to skip authentication for TestRestTemplate in Spring Boot Tests?
[英]Skip Authentication for /login in spring boot
我想跳过/ login请求的身份验证。
在ServerApplication中,我编写了此方法来实现它:
@Configuration
@EnableResourceServer
protected class ResourceServer extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/login").permitAll();
}
}
但是,我现在收到此响应(无论我传递什么参数/标题,都尝试打/ login端点时):
{
"timestamp": 1493881871867,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/login"
}
尝试consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
和其他几种类似的解决方案,但仍得到相同的响应。
还为/ login创建了一个端点,但是没有成功:
@RequestMapping(value = "/login",
method = RequestMethod.POST,
produces = "application/json")
public ResponseEntity login(@RequestBody Map<String, String> requestBody) {
System.out.println("DO I EVEN REACH HERE?");
return null;
}
我是Spring的新手,无法弄清楚我在这里犯的错误。 有什么帮助吗?
错误消息是;
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
这意味着您通过html表单发布到/login
。 但您的login
端点不接受表单参数。 如下更改login
端点
@RequestMapping(value = "/login",
method = RequestMethod.POST,
produces = "application/json")
public ResponseEntity login(@RequestParam("username") String username, @RequestParam("password") String password) {
System.out.println("DO I EVEN REACH HERE?");
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.