我使用带有弹簧安全性的AngularJs进行身份验证& Spring REST应用程序中的自动化。我向服务器发送请求以进行登录, 这是我的角色服务:
angular.module('workflowService', ['ngResource']).
factory('Utilisateur', function ($resource) {
return $resource('rest/user/:action', {}, {
authenticate: {
method: 'POST',
params: {'action' : 'authenticate'},
headers: {'Content-Type': 'application/x-www-form-urlencoded',
}
},
}
);
});
在服务器端,我有一个Spring MVC Controller,如下所述:
@RequestMapping(value = "/user/authenticate", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody TokenTransfer authenticate(@PathVariable String username, @PathVariable String password)
{
System.out.println( " appel authnticate ");
System.out.println( "username "+username+" pass "+password);
UsernamePasswordAuthenticationToken authenticationToken =
new UsernamePasswordAuthenticationToken(username, password);
Authentication authentication = this.authManager.authenticate(authenticationToken);
SecurityContextHolder.getContext().setAuthentication(authentication);
/*
* Reload user as password of authentication principal will be null after authorization and
* password is needed for token generation
*/
LdapUserDetails userDetails =(LdapUserDetails) this.userService.loadUserByUsername(username);
return new TokenTransfer(TokenUtils.createToken(userDetails));
}
但我得到了:
POST http://localhost:8080/springrestprojet/rest/user/authenticate 400(错误请求)
答案 0 :(得分:0)
我找到了解决方案。问题出在PathVariable中,Path中没有参数,只有FormParam!我只是将我的代码更新为:
@RequestMapping(value = "/user/authenticate", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody TokenTransfer authenticate(@FormParam("username") String username, @FormParam("password") String password)
{
//no modification
}