package com.xingxi.api.common;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


@Slf4j
@RequiredArgsConstructor
public abstract class BaseApiController {

    private Authentication getAuthentication() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            return authentication;
        } else {
            throw new RuntimeException("未登录");
        }
    }

    protected UserContext getUserContext() {
        Authentication authentication = getAuthentication();
        return (UserContext) authentication.getPrincipal();
    }
}
