15 spring spring-security jwt spring-security-oauth2
我正在用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';
}
});
}
Run Code Online (Sandbox Code Playgroud)
而且我有控制器
@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));
}
Run Code Online (Sandbox Code Playgroud)
但是当我和邮递员一起尝试邮寄请求时,它会告诉我
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
但是当我 cosole.log(data)
在ajax调用中打印令牌时?我无法弄清楚出了什么问题.感谢任何帮助.
小智 59
你需要在postman中设置内容类型为JSON(application/json)转到你的POST请求中的正文,你会发现它旁边的选项raw会有一个下拉表做.
Adi*_*Sai 14
我也有类似的问题。就我而言,我做了两处更改
单击标题标签并添加键“Content-Type”,值为“application/json”
第二步是单击“正文”选项卡并选择“原始”单选按钮,然后从下拉列表中选择类型“JSON”,如下所示
小智 7
Http 415 Media Unsupported
仅当应用程序不支持您提供的内容类型标头时才会响应。
使用 POSTMAN,Content-type
您发送的标头Content type 'multipart/form-data
不是application/json
. 在 ajax 代码中,您将其正确设置为application/json
. 在 POSTMAN 中传递正确的 Content-type 标头,它将起作用。
小智 6
我也得到了这个错误。我在更改为 XML(text/xml) 后在正文中使用 Text ,得到了预期的结果。
如果您的请求是 XML 请求,请使用 XML(text/xml)。
如果您的请求是 JSON 请求,请使用 JSON(application/json)
归档时间: |
|
查看次数: |
61112 次 |
最近记录: |