2017-03-08 7 views
1

私はoauth2とjwtで春のセキュリティを実装しています。 以下の私のログイン機能は、私がコントローラー郵便配達員にサポートされていないメディアタイプ

@RequestMapping(value = "auth/secret", method = RequestMethod.POST) 
    public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException { 
     System.out.println(); 
     logger.info("authentication request : " + authenticationRequest.getUsername() + " " + authenticationRequest.getPassword()); 
     // Perform the security 
     System.out.println(authenticationRequest.getUsername()+"is the username and "+authenticationRequest.getPassword()); 
     final Authentication authentication = authenticationManager.authenticate(
       new UsernamePasswordAuthenticationToken(
         authenticationRequest.getUsername(), 
         authenticationRequest.getPassword() 


         ) 


     ); 
     SecurityContextHolder.getContext().setAuthentication(authentication); 

     logger.info("authentication passed"); 

     // Reload password post-security so we can generate token 
     final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername()); 
     final String token = jwtTokenUtil.generateToken(userDetails, device); 
     logger.info("token " + token); 

     // Return the token 
     return ResponseEntity.ok(new JwtAuthenticationResponse(token)); 
    } 

を持っている。しかし、私は郵便配達してPOSTリクエストをしようとすると、それは

{ 
    "timestamp": 1488973010828, 
    "status": 415, 
    "error": "Unsupported Media Type", 
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException", 
    "message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundaryY4KgeeQ9ONtKpvkQ;charset=UTF-8' not supported", 
    "path": "/TaxiVis/auth/secret" 
} 

しかし、ときに私は私を示し

function doLogin(loginData) { 

    $.ajax({ 
     url : back+"/auth/secret", 
     type : "POST", 
     data : JSON.stringify(loginData), 
     contentType : "application/json; charset=utf-8", 
     dataType : "json", 
     async : false, 
     success : function(data, textStatus, jqXHR) { 

      setJwtToken(data.token); 


     }, 
     error : function(jqXHR, textStatus, errorThrown) { 
      alert("an unexpected error occured: " + errorThrown); 
      window.location.href= back+'/login_page.html'; 
     } 
    }); 
} 

とダウンしていますajaxの呼び出しでcosole.log(data)を実行するとトークンが印刷されるのですか?何が間違っているのか分かりませんでした。

+2

「メッセージ」の場合:「コンテンツタイプ「マルチパート/フォームデータ;あなたは、その平均値を何JSONペイロード – alfcope

+0

を送信していません?説明してもらえますか? –

+0

@RequestBodyを使ってコントローラのマッピングメソッドがjsonオブジェクトを待っているため、リクエストが間違っていることを意味します。ヘッダーContent-Typeを郵便配達員のapplication-jsonに設定し、 bodyセクションでrawとして送信したいJSONオブジェクトhttps://www.google.com/search?q=postman%20json%20post&&rct=j – alfcope

答えて

7

JSON(application/json)がPOSTリクエスト内の本文に移動すると、郵便配達員のコンテンツタイプを設定する必要があります。そこにドロップダウンリストが表示されます。

+0

これはJSONにある –

+1

のur要求m apping uは@consumesにapplication/jsonという値を追加してJSONオブジェクトを扱うことができます – amRika

0

Http 415 Media Unsupportedは、提供しているコンテンツタイプヘッダーがアプリケーションでサポートされていない場合にのみ返信されます。

POSTMANでは、送信するContent-typeヘッダーはContent type 'multipart/form-dataではなく、application/jsonです。 Ajaxコードでは、正しくapplication/jsonに設定しています。正しいContent-typeヘッダーをPOSTMANに渡すと正常に機能します。

0

私もこのエラーが出ました。私はXML(text/xml)に変更した後、body内のTextを使用していましたが、期待どおりの結果が得られました。

  • リクエストがXML(test/xml)を使用している場合。

  • リクエストはJSONリクエスト利用JSON(アプリケーション/ JSON)

関連する問題