Ale*_*Tbk 6 authentication spring spring-security thymeleaf spring-boot
我想知道,我如何从Thymeleaf获取User对象.目前我正在调用我的userService,它将从DB获取用户.我不喜欢这种方法,因为每次调用都会进行数据库查询.
是否有可能从内存中获取用户?
<link href="/css/style2.css"
th:if="${@commanderService.getCurrentCommander()} and
${@commanderService.getCurrentCommander().settings == 'template=1'}"
rel="stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)
CommanderService:
public Commander getCurrentCommander() {
Object principal =
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Commander commander = findByName((String)principal);
return commander;
}
Run Code Online (Sandbox Code Playgroud)
如果您使用 spring security 和 thymeleaf,您可以检查:
https://github.com/thymeleaf/thymeleaf-extras-springsecurity3
例如:
<div sec:authentication="name">
The value of the "name" property of the authentication object should appear here.
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">
This content is only shown to administrators.
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10308 次 |
| 最近记录: |