AuthHolder
public class AuthHolder { private static final ThreadLocal<User> threadLocal = new ThreadLocal<>(); public static Optional<User> getUser(){ return Optional.fromNullable(threadLocal.get()); } public static boolean hasLogin(){ User user= threadLocal.get(); return null!=user; } public static void setUser(User user){ threadLocal.set(user); } public static Optional<Long> getUserId(){ User user= threadLocal.get(); if(null == user|| null == user.getId()){ return Optional.absent(); } return Optional.fromNullable(user.getId().longValue()); } public static Optional<String> getUserName() { User user = threadLocal.get(); if (null == user) { return Optional.absent(); } return Optional.fromNullable(user.getName()); } }
登录拦截器添加:
if(userRole.getType()<=functionRole.getType()){ AuthHolder.setUser(SessionUtils.getUser(httpServletRequest.getSession())); return true; }else { httpServletResponse.setStatus(HttpStatus.FORBIDDEN.value()); P.throwMessageExceptionIf(true,HttpStatus.FORBIDDEN.value(),"没有权限访问"); return false; }
在后续逻辑即可使用
@Override
@Transactional
public Long saveInfo(LocationRequest locationRequest) {
checkLocationRequest(locationRequest);
List<Location> locationList = locationService.getByDisName(locationRequest.getAppId(), locationRequest.getDisName());
if (CollectionUtils.isNotEmpty(locationList)) {
P.throwMessageExceptionIf(true, 200, "位置的描述名已经存在,请修改描述名称");
}
Location location = locationRequest.toLocation(locationRequest);
Long id = locationService.save(location,AuthHolder.getUserId().get(), AuthHolder.getUserName().get());
return id;
}