简体   繁体   English

将angularJS与Spring安全性结合使用并发送JSON请求时出现问题

[英]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.

相关问题 使用angularjs将json发送到spring控制器时,浏览器控制台中出现不受支持的媒体类型错误 - Unsupported Media Type error in browser console when sending json using angularjs to spring controller 发送PUT请求时出现Spring + AngularJs + Tomcat 9.0-403错误 - Spring +AngularJs + Tomcat 9.0 - 403 error when sending a PUT request 使用Spring Security&#39;hasPermission()&#39;对未经授权的REST服务请求返回JSON - Return JSON on unauthorized REST service request using Spring Security 'hasPermission()' Spring Security吃AngularJS POST请求 - Spring security eating angularjs POST request Spring Security Oauth - 发送令牌请求时所需的基本访问身份验证 - Spring Security Oauth - Basic access authentication needed when sending token request Java AngularJS Spring MVC - 将 bean 发送到控制器时,$http.post 400 错误请求在语法上不正确 - Java AngularJS Spring MVC - $http.post 400 bad request syntactically incorrect when sending bean to controller 无法使用Spring Security保护AngularJS页面 - Can not secure AngularJS page using Spring Security Spring 发送请求时启动错误 403 - Spring Boot error 403 when sending request AngularJS和Spring MVC安全性 - AngularJS and Spring MVC security 将文件发送到FTP时出现Spring FTP问题 - Spring FTP issue when sending file TO FTP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM