java.security.Principal如何在Spring MVC Application中工作?

时间:2016-07-27 06:03:00

标签: java spring spring-mvc spring-security spring-boot

我对 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;
}

0 个答案:

没有答案