[英]Issue when using angularJS with Spring security and sending JSON request
I am using spring security for authentication and authorization. 我正在使用Spring Security进行身份验证和授权。 For sending a request I am using Angular JS http.post request.
为了发送请求,我使用的是Angular JS http.post请求。 The sample 'home.jsp' code is:
示例“ home.jsp”代码为:
<form role="form">
<div class="form-group">
<label for="username">Username:</label> <input type="text"
class="form-control" id="username" name="username" ng-model="userinfo.username"/>
</div>
<div class="form-group">
<label for="password">Password:</label> <input type="password"
class="form-control" id="password" name="password" ng-model="userinfo.password"/>
</div>
<button type="submit" class="btn btn-primary" ng-click='login()'>Submit</button>
My UserInfo class is bind with model: 我的UserInfo类与模型绑定:
UserInfo.java
private String username;
private String password;
// Generated getters & setters
The sample 'home.js' is: I am doing something below ie I am converting the userInfo in JSON string. 示例“ home.js”是:我正在做以下事情,即我正在将userInfo转换为JSON字符串。
$scope.login = function() {
$http.post('login', angular.toJson(userInfo)
.success(function(data) {
// Some response handling
})};
The login reach the server and all built in spring security filters executed but when I debug 'UserandPasswordAuthenticationFilter' it shows me request.getParameter('username') and password is NULL. 登录名到达服务器并执行所有内置的spring安全过滤器,但是当我调试'UserandPasswordAuthenticationFilter'时,它向我显示request.getParameter('username')且密码为NULL。 I am sending the JSON string which is of type UserInfo class.
我正在发送类型为UserInfo类的JSON字符串。 Can any one please guide me that would I add a new filter which 'parse' the JSON string and convert into UserInfo object before executing the 'UserandPasswordAuthenticationFilter' and set the username and password in servlet request parameter and similarly another filter which convert the object into JSON while sending the response back?
谁能指导我,在执行“ UserandPasswordAuthenticationFilter”之前,添加一个“解析” JSON字符串并转换为UserInfo对象的新过滤器,并在servlet请求参数中设置用户名和密码,同样,另一个过滤器将对象转换为JSON同时发送回响应?
Please help and propose the best solution. 请帮助并提出最佳解决方案。
Thanks. 谢谢。
You're faced with two options. 您面临两个选择。 Either just post what Spring is expecting from you (ie simple j_username and j_password parameters) or configure a different set of filters and auth providers that will construct and expect a different token (not UsernamePasswordToken that is being created by UsernamePasswordAuthenticationFilter now).
只需发布Spring对您的期望(即简单的j_username和j_password参数),或者配置一组不同的过滤器和auth提供程序,它们将构造并期望使用不同的令牌(不是现在由UsernamePasswordAuthenticationFilter创建的UsernamePasswordToken)。 Your auth provider should expect the new token type and check for it in its
supports(Class<?> clazz)
method. 您的身份验证提供程序应该期望新的令牌类型,并在其
supports(Class<?> clazz)
方法中进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.