Fine if it works for your table.
What you described fits within the rules as written, and if it works for your table, it's "legit".
Is it legal
You cited the rules of a long rest. What you described mechanically fits within the definition of a long rest, or can be trivially jiggered to fit. If you start your rest at fantasy-2200 and end it at fantasy-0600, if you spend t time at, say, fantasy-0500 casting spells, then as long as you extend the long rest past fantasy-0600 by t, and t is less than an hour, then you're good.
See also this question:
Can you cast a spell just before the end of a long rest?
An argument that this technique is not even within the rules is that the phrase
If the rest is interrupted by a period of strenuous activity - at least 1 hour of walking, fighting, casting spells, or similar adventuring activity - the characters must begin the rest again to gain any benefit from it.
is equivalent to "if the rest is interrupted by any one or more of these things: a) at least 1 hour of walking, b) fighting, c) casting spells, or d) similar adventuring activity then the characters must begin the rest again to gain any benefit from it."
If this is correct, casting even one spell invalidates the long rest.
However, to my reading, the quoted passage is equivalent to: "If the rest is interrupted by a period of strenuous activity, the period being at least 1 hour, and being composed of one or more of the following: a) walking, b) fighting, c) casting spells, or d) similar adventuring activity - then the characters must begin the rest again to gain any benefit from it."
But you aren't asking if it fits within the rules. You're saying given it's accepted practice in your game, is it "legit".
Is it legit
You used legit in two different ways in your question. One meant conforming to rules-as-written. We've covered that.
I think you summed up your question well at the end:
But is [casting spells] at the end of a long rest purely exploiting a technical loophole in the rules or can it be a legitimate use of rules from a narrative perspective, for the characters in-game?
So in this case you mean "legit" as not just conforming to the rules technically, but conforming in a broader sense, as opposed to "shenaniganry", exploiting a technical loophole in the rules that has no narrative support in-game, that conforms to rules as written but in some sense violates rules-as-intended.
The rules are abstractions
The rules are abstractions. We use the rules to provide structure to shared story-telling. There are many, many places where if you closely examine the rules, they fail to create a perfect simulation of a fantasy world. For example, the concept of a long rest itself does not bear minute examination, as a simulation. For instance, is it really realistic that 8 hours works, but 7 1/2 don't? That 57 minutes of fighting only requires extension of the 8 hours by 57 minutes, but an hour and 2 minutes of fighting requires the 8 hours to begin again?
The characters don't know the rules
The characters don't know the rules, right? (Although for another perspective, see OOTS.)
So, if your characters say, "wow, let's use up the last of our spell slots, we've got 5 minutes before the end of the long rest", some might say you are exploiting a technical loophole in the rules that has no narrative support in-game.
It is easy to see how that could be considered to be a violation of rules-as-intended, or shenaniganry.
However, it is not unrealistic at all to imagine that the characters, knowing magical combat is likely, would want to prepare as well as possible.
The narrative gap
So, that leaves us with a narrative gap, between the static rules, and the imagined world of the game. We constantly bridge the narrative gap, any time we imagine what characters do, that fits within the rules. The character swings a sword, the player rolls to-hit. We fill the narrative gap by imagining whatever level of detail or narration to that sword-swinging that we want.
Bridging the narrative gap
The game is very broad in its support for how to bridge the narrative gap. There are no specific rules requiring how the narrative gap must be described, or that everything be justified in detail. In the Introduction, the Basic Rules describes How to Play:
- The DM describes the environment.
- The players describes what they want to do.
- The DM narrates the results of the adventurers' actions.
The rules do not specify a narrative quality to the descriptions and narrations. It is just as RAW to say "we take a long rest" as to say, "we bed down for the night".
When a player says "we take a long rest", the players are free to interpret that as that the characters are saying "wow, it's getting late, let's get some shut-eye."
The players (and DM) of the game decides what makes sense for that particular game. Third parties can offer an opinion, often a valuable one, but in the end what matters is your game.
In one game, a player may say, "before the end of the long rest, I cast aid to use up a spell slot", and the DM says, "sure, that works, you don't even need to tell me."
In another game, the player may say the same thing, and the DM says, "yeah, that won't work. The mechanics of the long rest are an abstraction. There isn't a moment when you know you have so many minutes left before the long rest ends."
Both are correct. Both are RAW. It is up to the those participating in the game to bridge the gap from the rules to their game.
Everyone at the table need not even completely agree, or even necessarily imagine it working exactly the same way. The DM makes a ruling and you move on.
A narrative solution
If you need a narrative solution to your quandry you can easily invent one.
It had been a nervous night, but fortunately the party had been able to get some rest. Ricq had seen something moving off in the distance, but it didn't close on where the party rested in the hut. As she did every morning, Wendy held her crystal and closed her eyes, and felt for the power within. Like breathing, yesterday's power was being exhaled from her psyche, and once that breath was exhaled, today's power would come flooding in. With the last of the exhalation, Wendy summoned the arcane not-images of mage armor to her mind's eye, and repeating the familiar words and gestures, felt the familiar force settling about her. She remained still a few moments, and then felt the power flooding back into her. With a physical exhale, she opened her eyes, ready to face the day.
This is just A way to visualize the in-game narrative, supporting using yesterday's spell slots before regaining today's. This isn't RAW, it isn't RAI, it isn't how I play, or a recommendation. It's just an example showing that the narrative could support the rule.
So, who's to say exactly how it all works in-game? The players of the game. If it works for you, go for it.