//获取登录用户信息 public CsmallAuthenticationInfo getUserInfo(){ // 获得SpringSecurity上下文(容器)对象 UsernamePasswordAuthenticationToken authenticationToken= (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext() .getAuthentication(); if(authenticationToken==null){ throw new CoolSharkServiceException(ResponseCode.UNAUTHORIZED,"没有登录信息"); } // 如果authenticationToken不为空,获得其中的用户信息 CsmallAuthenticationInfo csmallAuthenticationInfo= (CsmallAuthenticationInfo) authenticationToken.getCredentials(); // 返回登录用户信息 return csmallAuthenticationInfo; } // 业务逻辑层大多数方法都是需要获得用户Id,所以我们编写一个方法,专门返回当前登录用户的id public Long getUserId(){ return getUserInfo().getId(); }