使用#authentication实用程序的Spring Boot和Thymeleaf

时间:2015-01-15 06:31:09

标签: java spring-security spring-boot thymeleaf

使用Spring Boot,spring security starter和thymeleaf,登录后我无法访问#authentication实用程序(更准确地说,它是null)。我没有做任何特殊的配置(假设启动器会为我做这个)并且我没有在我的模板中包含sec:namespace(再次,假设我不需要它 - 我见过的所有示例到目前为止''我也需要它。 我想打电话给:{#authentication.expression('isAuthenticated()')}

作为参考,这是在认证后调用的控制器:

import java.security.Principal;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/site-admin")
public class VZSiteAdminController {

    @RequestMapping(method=RequestMethod.GET)
    public String mainScreen(Principal principal){

    return "site-admin";

    }
}

2 个答案:

答案 0 :(得分:3)

如果您想从主要对象访问属性,您应该这样:

<div th:text="${#authentication.principal.something}">
    The value of the "name" property of the authentication object should appear here.
</div>

这篇文章对我很有帮助,因为我添加了一个存储在主体对象中的用户图像:

<img th:if="${#authentication.principal.image}"
    class="img-circle" th:src="${#authentication.principal.image}"
    width="100" height="100" alt="place-holder" />

答案 1 :(得分:1)

Spring boot thymeleaf启动程序不包含它,您需要添加thymeleaf-extras-springsecurity3 / 4/5作为依赖项。 https://github.com/thymeleaf/thymeleaf-extras-springsecurity

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>
相关问题