简体   繁体   English

获取当前登录用户的spring + angular

[英]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. 我在Spring Boot应用程序中使用Spring Security,我想从Principal#getName获取当前登录的用户,但我有模板解析错误,它包含我想要的用户名。

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: 这是我的AngularJs函数来获取登录用户:

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”时出错,模板可能不存在或任何已配置的模板解析器可能无法访问

Kamel.mili is the logged in username. Kamel.mili是登录用户名。 Can you please help me. 你能帮我么。 I am using thymeleaf for just the login page and everything else is html + AngularJs. 我只使用百万美元登录页面,其他一切都是html + AngularJs。 I don't know why thymeleaf had it's hand on this controller. 我不知道为什么百老汇会把它放在这个控制器上。

Add a @ResponseBody annotation to your controller: @ResponseBody注释添加到控制器:

@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: 当您使用@Controller型注释时,Spring MVC将尝试将您的String返回值解析为View ,因此错误:

Error resolving template "kamel.mili", template might not exist or might not be accessible by any of the configured Template Resolvers 解析模板“kamel.mili”时出错,模板可能不存在或任何已配置的模板解析器可能无法访问

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 . 如果你想只写返回值来响应正文,你可以让你的控制器@RestController或注释特定的控制器与@ResponseBody

This is kinda off topic but since you're using client side view rendering, that ModelMap is pretty useless. 这有点偏离主题,但由于您使用的是客户端视图渲染,因此ModelMap非常无用。 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. printWelcome()的返回字符串应该与视图/模板文件夹中创建的视图之一匹配。在您的情况下,您将返回自己的用户名,我想它应该是欢迎的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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