data-process-spring/src/main/java/com/aqroid/dataprocess/auth/controller/AuthenticationAdvice.java

29 lines
1.0 KiB
Java

package com.aqroid.dataprocess.auth.controller;
import com.aqroid.dataprocess.mybatisPlus.dto.KmSystemStaffDto;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
/**
* 将用户注入到 req Model 里面方便在controller里面直接注入
* @author dost
*/
@ControllerAdvice
public class AuthenticationAdvice {
@ModelAttribute
public KmSystemStaffDto staff() {
return handleStaff();
}
public static KmSystemStaffDto handleStaff() {
return (KmSystemStaffDto) (SecurityContextHolder.getContext().getAuthentication() == null
|| SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null
|| (SecurityContextHolder.getContext().getAuthentication().getPrincipal() instanceof String)
? null : SecurityContextHolder.getContext().getAuthentication().getPrincipal());
}
}