springboot-Security获取当前的用户信息案例

短命女 2023-05-29 03:17 66阅读 0赞

获取当前的用户信息:

  1. public class HrUtils {
  2. public static Hr getCurrentHr() {
  3. return (Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  4. }
  5. }

使用的时候,可以根据上面那段代码中获取到的Hr实体类信息,然后通过get获取Hr中的某个属性:

  1. public List<Hr> getAllHrExceptAdmin() {
  2. return hrMapper.getAllHr(HrUtils.getCurrentHr().getId());
  3. }

顺便把相关的都放出来:
Mapper接口中存放:

  1. /** * 通过编号获取所有的人员信息 * * @param currentId 当前编号ID * @return 人员信息集合 */
  2. List<Hr> getAllHr(@Param("currentId") Long currentId);

mapper.xml

  1. <select id="getAllHr" resultType="com.zto.hrms.model.Hr">
  2. select * from hr
  3. <if test="currentId!=null">
  4. WHERE id !=#{ currentId}
  5. </if>
  6. </select>

如果感兴趣可以关注本人的微信公众号,微信搜索”IT微资源”,或者扫下面的二维码
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,66人围观)

还没有评论,来说两句吧...

相关阅读

    相关 java_后端获取当前登录用户信息

    后端获取当前登录用户信息 开发过程中,发现有很多地方需要获取当前登录的用户信息,比如新增、修改时候要记录创建人和更新人信息,如果每次操作都从数据库中获取用户信息,会增加不