This might work but it's not the best way of doing this because flash memories are not infinitely writeable/readable. STM32s flash typically have 10.000 cycles of flash endurance since it's not designed to be written into repetitiely. Although it might work after that amount of cycles, it's not guaranteed. You might wear the flash down and make it unusable after repetitive writes. The best option here would be connecting a SPI controlled volatile or non-volatile memory to your STM. Using QSPI would be better because it's memory mapped and faster than SPI. Choosing between volatile or non-volatile memory is completely based on your design choice.
You can find the list of possible ICs here
You need to consider the clock speed, write and read times, size and interface of the IC you choose according to your needs