如何从cURL获得令牌?

如何从cURL获得令牌?,curl,spring-security,jwt,Curl,Spring Security,Jwt,我使用的是Spring Security JWT,我想用cURL获取令牌: @RequestMapping(value=“/authenticate”,method=RequestMethod.POST) 公共响应登录(@RequestParam字符串用户名,@RequestParam字符串密码, HttpServletResponse(响应)引发IOException{ 字符串标记=null; AppUser AppUser=appUserRepository.findOneByUsernam

我使用的是Spring Security JWT,我想用cURL获取令牌:

@RequestMapping(value=“/authenticate”,method=RequestMethod.POST)
公共响应登录(@RequestParam字符串用户名,@RequestParam字符串密码,
HttpServletResponse(响应)引发IOException{
字符串标记=null;
AppUser AppUser=appUserRepository.findOneByUsername(用户名);
Map tokenMap=newhashmap();
if(appUser!=null&&appUser.getPassword().equals(password)){
token=Jwts.builder().setSubject(用户名).声明(“角色”,appUser.getRoles()).setIssuedAt(新日期())
.signWith(SignatureAlgorithm.HS256,“secretkey”).compact();
tokenMap.put(“token”,token);
tokenMap.put(“用户”,appUser);
返回新的响应属性(tokenMap,HttpStatus.OK);
}否则{
tokenMap.put(“token”,null);
返回新的响应属性(tokenMap,HttpStatus.UNAUTHORIZED);
}
}
我试着这样做:

curl-xpost-H“内容类型:application/json”localhost:8080/authenticate-d'{“用户名”:“admin”,“密码”:“admin”}”
我得到了这个错误:

必需的字符串参数“username”


您的请求缺少
用户名
密码
参数,您应该将它们与param
数据一起发送,而不是JSON:

curl -X POST http://localhost:8080/authenticate --data "username=admin&password=admin" 

如果您使用的是基本身份验证,那么当我使用这个curl-X POST--data“username=admin&password=admin”时,您可以使用
curl-u username:password xxx

,它返回{“token”:null}