我对 java.security.Principal 如何工作以及如何正确配置感到非常困惑。 我的服务类:
@Override
public BarrierPoint addBarrierPoint(Principal principal,String barrierPt,String location) {
if(checkDuplicate(barrierPt)){return null;}else{
BarrierPoint barrierPoint = new BarrierPoint();
barrierPoint.setBarrierPt(barrierPt);
barrierPoint.setStatus(1);
barrierPoint.setLocation(location);
barrierPoint.setCreatedBy(SecurityUtil.getUser(principal).getUserId());
return barrierPointRepository.save(barrierPoint);
}
}
SecurityUtil类:
public static User getUser(Principal principal)
{
if(principal instanceof UsernamePasswordAuthenticationToken)
return ((UserDetails)((UsernamePasswordAuthenticationToken)principal).getPrincipal()).getUser();
else if(principal instanceof RememberMeAuthenticationToken)
return ((UserDetails)((RememberMeAuthenticationToken)principal).getPrincipal()).getUser();
return null;
}