Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting current logged user in spring + angular

I am using Spring Security in my Spring Boot app and i want to get the current logged user from Principal#getName but i have an error of template resolution and it contains the username that i want to get.

This is my controller:

import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PageController {

   @RequestMapping(value = "/api/loggeduser", method = RequestMethod.GET)
   public String printWelcome(ModelMap model, Principal principal) {
        String name = null;
        if (principal !=null) {
            name = principal.getName();
        }
        model.addAttribute("username", name);

        return name;
   } 

}

And this is my AngularJs function to get the logged in user:

app.controller('Usercontr', function($scope, $http) {
    $http.get("/api/loggeduser").success(function (data) {
        $scope.nom = data;  
        console.log($scope.nom) 
    })
});

And here's the error:

Error resolving template "kamel.mili", template might not exist or might not be accessible by any of the configured Template Resolvers

Kamel.mili is the logged in username. Can you please help me. I am using thymeleaf for just the login page and everything else is html + AngularJs. I don't know why thymeleaf had it's hand on this controller.

like image 887
Kamel Mili Avatar asked Sep 19 '25 09:09

Kamel Mili


2 Answers

Add a @ResponseBody annotation to your controller:

@ResponseBody
@RequestMapping(value = "/api/loggeduser", method = RequestMethod.GET)
public String printWelcome(ModelMap model, Principal principal ) { ... }

When you're using the @Controller stereotype annotation, Spring MVC will try to resolve your String return value to a View, hence the error:

Error resolving template "kamel.mili", template might not exist or might not be accessible by any of the configured Template Resolvers

If you want to just write the return value to the response body, you can either make your controller a @RestController or annotate specific controllers with @ResponseBody.

This is kinda off topic but since you're using client side view rendering, that ModelMap is pretty useless. You can safely get rid of it.

like image 174
Ali Dehghani Avatar answered Sep 21 '25 02:09

Ali Dehghani


You need to do like following:

@RequestMapping(value = "/api/loggeduser", produces =MediaType.APPLICATION_JSON_VALUE )
public ResponseEntity<String> findMessagesForUser(Principal principal) {
        return new ResponseEntity<String>(principal.getName(), HttpStatus.OK);
}

Or you can use response builders:

@RequestMapping(value = "/api/loggeduser", produces =MediaType.APPLICATION_JSON_VALUE )
public ResponseEntity<String> findMessagesForUser(Principal principal) {
        return ResponseEntity.ok(principal.getName());
}
like image 35
Ravi Kavaiya Avatar answered Sep 21 '25 01:09

Ravi Kavaiya