I am using spring security for authentication and authorization. For sending a request I am using Angular JS http.post request. The sample 'home.jsp' code is:

<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.java
private String username;
private String password;
// Generated getters & setters

The sample 'home.js' is: I am doing something below i.e. I am converting the userInfo in JSON string.

$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. I am sending the JSON string which is of type UserInfo class. 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?

Please help and propose the best solution.

Thanks.

  • could you please post the current UserandPasswordAuthenticationFilter code? – Paul John Mar 8 '15 at 7:45
up vote 0 down vote accepted

You're faced with two options. Either just post what Spring is expecting from you (i.e. 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). Your auth provider should expect the new token type and check for it in its supports(Class<?> clazz) method.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.