2017-05-04 56 views
0

我想跳過/登錄請求的身份驗證。跳過身份驗證/登錄在彈簧啓動

在我ServerApplication,我寫了這個方法來實現它:

@Configuration 
    @EnableResourceServer 
    protected class ResourceServer extends ResourceServerConfigurerAdapter { 

     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      http.authorizeRequests().antMatchers("/login").permitAll(); 
     } 
    } 

不過,現在我得到這個響應(試圖打/登錄端點,不管是什麼參數/頭我通過):

{ 
    "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,和其他一些類似的解決方案,但仍然得到了同樣的答覆。

也造就/登錄端點,但不是擊中:

@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; 
    } 

我很新的春天和不能找出我在這裏做了錯誤。任何幫助?

+0

'.antMatchers(「/ login *」)。anonymous()'? btw你真的發送JSON嗎?它可能只是內容類型是錯誤的。 – StanislavL

+0

沒有工作.. :( –

+0

嘗試添加'.anyRequest();'給你的電話 'http.authorizeRequests()。antMatchers(「/ login」)。permitAll.anyRequest();' – Kai

回答

0

錯誤信息是;

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported 

這意味着你通過html表單發送到/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; 
}