【问题标题】:Spring Boot and Thymeleaf using the #authentication utilitySpring Boot 和 Thymeleaf 使用 #authentication 实用程序
【发布时间】:2015-01-15 06:31:09
【问题描述】:

使用 Spring Boot、spring security starter 和 thymeleaf,我在登录后无法访问 #authentication 实用程序(更准确地说,它是 null)。我没有做任何特殊的配置(假设初学者会为我做这些)并且我没有在我的模板中包含 sec: 命名空间(再次,假设我不需要它 - 我到目前为止看到的所有示例都没有'也不需要它)。 我想打电话给:{#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";

    }
}

【问题讨论】:

  • 如果你只有启动器,你仍然需要添加对thymeleaf-extras-springsecurity3 jar 的依赖。其中包含注册与 thymeleaf 的安全集成的类。
  • 是不是...仍然没有成功。我必须将其添加到我的模板中吗?
  • 您确定您的日志记录工作正常吗?表达式不应该类似于${#authentication},请注意$

标签: java spring-security spring-boot thymeleaf


【解决方案1】:

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

<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" />

【讨论】:

    【解决方案2】:

    Spring boot thymeleaf starter 不包含它,您需要添加 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>
    

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 2019-03-12
      • 2018-01-13
      • 2018-07-02
      • 2018-05-02
      • 2018-05-01
      • 2016-08-24
      • 2020-11-08
      • 2017-01-09
      相关资源
      最近更新 更多