A 10-year guerrilla war in the Western Sahara has reinforced the authority of Morocco's King Hassan II among his people but also has created new strains that could threaten the long-term stability of his nation.

As evidence mounts that the Moroccan Army is succeeding in consolidating its control over the disputed former Spanish colony, the thoughts of Moroccan intellectuals and western diplomats here gradually are turning to the problems that could lie ahead when the soldiers return from the desert.

"The king's real problem is not the war but the peace," commented a western diplomat in Rabat, an opinion echoed by many in Morocco, which serves as the linchpin to U.S. security interests in northwest Africa.

Independent military analysts attribute Morocco's gains in the desert war against the Algerian-backed Polisario Front to a strategy of fencing off ever more extensive areas of the Western Sahara with sand walls bristling with electronic listening devices. Stretching across a distance of more than 1,000 miles, the walls have robbed the Polisario guerrillas of the crucial element of surprise in their hit-and-run attacks on Moroccan government positions.

The struggle to "recover" the Western Sahara has provided Moroccans with a national rallying point and endowed King Hassan's reign with a sense of purpose after two narrowly thwarted coup attempts in the early 1970s. This week the 55-year-old monarch paid his first visit to the Western Saharan capital of El Ayoun to an enthusiastic welcome by a crowd estimated at 50,000.

Official posters and photographs of Hassan are invariably accompanied by the title "The Unifier," a reference to Morocco's historical claim to the territories of the Sahara long before the period of European colonization. Portraits of his father, Mohammed V, who negotiated the country's independence from France in 1956, are labeled "the liberator."

The preoccupation with the conflict in the Sahara has diverted attention from a gathering economic crisis attributable to a variety of natural and man-made causes such as drought, rising oil prices, mismanagement and corruption. Development funds have been poured into places such as El Ayoun, once a run-down village but now a city of 100,000, with magnificent new hotels, two-lane avenues and brand new government housing provided free to Moroccans willing to colonize the desert.

The cost of the war -- which includes keeping a 110,000-strong army permanently deployed in the desert -- is estimated conservatively at $1 million a day. Morocco's foreign debt is now about $13 billion, approaching 100 percent of annual gross domestic product.

The debt burden has led to unrest that has become almost endemic in cities such as Casablanca and Marrakech, where slums adjoin fabulous royal palaces and villas belonging to the elite. The government's attempts to satisfy its foreign creditors by raising prices and cutting food subsidies have led to riots that have been put down with heavy loss of life.

It is estimated that 200 people were killed in protests against food price increases in Casablanca, Morocco's largest city, in 1981. Last year the unrest spread to Marrakech in the south and towns in the Rif region in the north -- an area with a long tradition of defiance of the central authorities. The official death toll was 29, but diplomats put the death toll at around 100.

Despite Algerian claims to the contrary, most Moroccans do not seem to have made the connection between the war and the economic crisis. The campaign in the Western Sahara has broad national support. The left-wing opposition, which is critical of the government's economic policies, is a noisy supporter of the war.

The Western Sahara conflict began in earnest in 1975, when the Spanish Army pulled out of what had been known as Spanish Sahara, leaving it to be divided between and controlled by Mauritania and Morocco.

A more serious concern, according to independent analysts here, is that an end to the war eventually might lead to an unraveling of the national consensus that has been created so skillfully by Hassan. Many intellectuals attribute a modest relaxation of restrictions on free speech to the fact that the king can count on the unqualified support of his people for the Sahara campaign.

"It may sound paradoxical, but the war has led to greater democracy here," said an economist in Rabat, citing parliamentary elections last September and debate in the press about the causes of the country's economic ills. The left-wing opposition denounced the elections as rigged in favor of the government parties -- but the campaign was relatively free by Moroccan standards.

The war effectively has removed the threat of another attempted takeover by the military -- the most obvious alternative to the king. With half the Army deployed in the Western Sahara, it is difficult to see who would mount such a coup attempt. Soldiers on active duty in the desert are well cared for and receive twice their normal pay.

The king, who is his own minister of defense, has taken care to prevent any single officer from gaining unified control over the Army since the former armed forces chief of staff, Gen. Ahmed Dlimi, died in a mysterious car crash in January 1983. Dlimi's functions, which included chief of military intelligence, were split up between several officers.

A measure of the king's new-found confidence in the armed forces was provided earlier this month when he promoted five officers, including the commander of the troops in the Sahara, to general. Previously, the highest rank that a career officer could expect to reach was "colonel-major."

Politically acquiescent at the moment, the Army again could become a threat to the monarchy in peacetime. The problem of finding employment for young soldiers returning from the desert is exacerbated by Morocco's high birthrate. Half the population is under age 20.

There is a history in Morocco of new dynasties emerging from the desert and, armed with a fundamental faith in Islam, sweeping away a regime that has grown corrupt and soft. Established monarchies have been overthrown within the last generation in other Arab countries such as Libya and Egypt, and it is possible to imagine a similar upheaval in Morocco.

Hassan's strength is his awareness of the dangers facing him. After 25 years on the throne he has accumulated enormous experience in the art of governing, and even his critics concede that despite a reputation for high living and autocratic whims, he has proved himself one of the wiliest rulers in the Arab world.

"The king is much more intelligent than most Third World leaders. He has his own sources of reliable information. The regime cracks down hard against the smallest sign of danger but is also trying to find ways to let people express themselves," said a Moroccan political scientist critical of many of the government's economic policies.

As a descendant of the prophet Mohammed, the king derives part of his legitimacy from the fact that he is regarded by the mass of ordinary people as a religious as well as political leader. Islam provides his regime with a pillar of support, rather than a source of major opposition, as in Iran under the shah.