0

I have some trouble figuring out how to create a login form in angularjs using springboot.

I can register a user and send the data to the database but the problems begin when i want to login.

in angularjs i have a function like this

  function Login(username, password, callback) {


        $http.post('/api/authenticate', { username: username, password: password })
           .success(function (response) {
              callback(response);
           });

    }

What i managed to do but probably is't right:

@RequestMapping(value = "/authenticate/{id}",method = RequestMethod.GET)
public  User getUser(@PathVariable Integer id) {

    return repo.findOne(id);
}

This gives me following json

{"id":2,"username":"jdoe","password":"$2a$10$5hgIyQr.K9wb8cXEyWGbROAU.rkYzd19vP7ajHpwp1KUYdShfcPn.","lastname":"doe","firstname":"john","customfield":"Hello there"}

But now i have following problems and questions :

How can i check if the username and password is equal to the username and password of json by going to api/authenticate ? (without {id})

can i hide this json from the users ?

Is this safe ?

how will angular now all the users propertys ? (i suggest i can retrieve this from the json)

any pro tips on how to solve this?

3
  • Do consider using spring security, that will be a wise choice. Else sent the username/password to the Controller validate and return the UserId and authstatus in response and manage it in session , Here you have to handle everything yourself including Logoit, Authorization etc. Commented Sep 22, 2015 at 9:30
  • I know how i can create a custom login with jsp with spring security. But how do i manage this with angularjs. ? This would indeed be a perfect solution. But how should i start on this. ? Commented Sep 22, 2015 at 9:38
  • 1
    Have a look at this . Commented Sep 22, 2015 at 9:41

1 Answer 1

1

From AngularJS you are calling HTTP POST method and at Spring side you have declared as HTTP GET, which is wrong.

Correct request mapping is

 @RequestMapping(value = "/api/authenticate",method = RequestMethod.POST, consumes = "application/json")
 @ResponseBody
 public User getUser(@RequestBody User user) {
 //here request body contains User POJO object's payload (JSON object)

   //You are getting username from JSON, 
   //so you need to update your call to findOne method
   return repo.findOne(user.getUserName());
}

Please refer

Sign up to request clarification or add additional context in comments.

1 Comment

I will look first at what @M4ver1k said and look into Spring Security and Angular JS first. But mark your question as solve because this was a solution to this problem. Thanks for the information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.