[英]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.