我正在用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,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)时会打印令牌吗?我无法弄清楚出了什么问题.感谢任何帮助.
最佳答案
你需要在postman中设置内容类型为JSON(application / json)转到你的POST请求中的正文,你会发现它旁边的选项raw会有一个下拉表做.