我正在使用以下代碼從Spring身份驗證類中提取deviceId
。如何擺脫「類型安全:從對象到地圖<字符串,字符串>未經檢查轉換」
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.google.common.base.Optional;
public static Optional<String> getId() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!auth.isAuthenticated()) {
return Optional.absent();
}
// I see a warning as "Type safety: Unchecked cast from Object to Map<String,String>"
Map<String, String> details = (Map<String, String>) auth.getDetails();
return Optional.of(details.get("deviceId"));
}
如何渡過這個類型的安全警告信息?我想避免添加Suprress Warnings
標籤。
類型安全:未選中從
Object
轉換爲Map<String,String>
如果這可能,我會感到驚訝。 –