加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Spring boot thymeleaf:登录用户

发布时间:2020-12-15 01:37:54 所属栏目:大数据 来源:网络整理
导读:我想知道,我如何从Thymeleaf获取User对象.目前我正在调用我的userService,它将从DB获取用户.我不喜欢这种方法,因为每次调用都会进行数据库查询. 是否有可能从内存中获取用户? CommanderService: public Commander getCurrentCommander() { Object principa

我想知道,我如何从Thymeleaf获取User对象.目前我正在调用我的userService,它将从DB获取用户.我不喜欢这种方法,因为每次调用都会进行数据库查询.

是否有可能从内存中获取用户?

CommanderService:

public Commander getCurrentCommander() {
    Object principal = 
        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    Commander commander = findByName((String)principal);

    return commander;
}
最佳答案
如果您使用的是春季安全和百里香,您可以查看:
https://github.com/thymeleaf/thymeleaf-extras-springsecurity3

例如:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读