邮递员中不支持的媒体类型

问题描述 投票:6回答:3

我正在用oauth2和jwt实现spring security。以下是我的登录功能

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';
        }
    });
}

而且我有控制器

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

但是当我和邮递员一起尝试邮寄请求时,它会告诉我

{
  "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"
}

但是当我在ajax调用中执行cosole.log(data)时会打印令牌吗?我无法弄清楚出了什么问题。感谢任何帮助。

spring spring-security jwt spring-security-oauth2
3个回答
34
投票

你需要将postman中的content-type设置为JSON(application / json)。

转到POST请求中的正文,在那里你会找到raw选项。

在它旁边,会有一个下拉列表,选择JSON(application.json)。


1
投票

仅当应用程序不支持您提供的内容类型标题时,才会回复Http 415 Media Unsupported

使用POSTMAN,你发送的Content-type标题是Content type 'multipart/form-data而不是application/json。在ajax代码中,您正确地将它设置为application/json。在POSTMAN中传递正确的Content-type标头,它将起作用。


1
投票

我也遇到了这个错误。我在更改为XML(text / xml)后在body中使用Text,得到了预期的结果。

  • 如果您的请求是XML请求,请使用XML(test / xml)。
  • 如果您的请求是JSON请求使用JSON(application / json)
© www.soinside.com 2019 - 2024. All rights reserved.