简体   繁体   中英

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.

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.

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());
}

If you have properly configured template resolver, then you should check yours templates. Return string from printWelcome() should match with one of the view created in your view/template folder.In your case, you are returning user name it self, i guess it should be welcome kind of page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM