Police officers currently use the colloidal gold rapid testing method to detect heroin in the urine of drug abusers, but the results are often rendered erroneous due to the presence of antitussive drugs, which contain opioids. The traditional manual liquid-liquid extraction method for urine testing has low efficiency and poor sensitivity, and hence, it fails to meet the requirements of the public security department to crack down on drug abusers. Therefore, to avoid punishment, most rapid-test-positive people make false claims about intaking cough suppressants. It is imperative to establish a highly efficient automatic method for the simultaneous determination of multiple opioids in urine, to rule out the use of heroin. A method based on solid-phase extraction and derivatization coupled with gas chromatography-mass spectrometry (GC-MS) has been developed for the simultaneous detection of morphine, O6-acetylmorphine, codeine, and acetyl codeine in urine. Since these four opioids exists as cations in acidic aqueous solution, the urine samples collected from dead bodies or drug addicts were adjusted to pH 6 by using phosphate buffer, enriched, and purified by MCX-SPE columns. Then, morphine, O6-acetylmorphine, and codeine were derivatized by N-methyl-N-(trimethylsilyl) trifluoroacetamide (MSTFA) for GC-MS testing. The effects of sample loading and elution flow rate, percentage of formic acid in the wash solvent (methanol), percentage of ammonia in the eluent (methanol), volume of the wash solvent, and drying time of the cartridge on the extraction efficiency were investigated in detail. The best results were obtained under the following conditions:sample loading and elution flow rate, 1.0 mL/min; volume fraction of formic acid in the wash solvent, 3%; volume fraction of ammonia in the eluent solvent, 5%; volume of 3% (v/v) formic acid in methanol (eluent), 1 mL; and drying time of the cartridge, 1 min. The GC-MS results showed good linearity in the range of 0.02-0.8 μg/mL with correlation coefficients (r2) ≥ 0.998. The limits of detection (LODs) and limits of quantification (LOQs) were 0.0016-0.0039 μg/mL and 0.0054-0.0128 μg/mL, respectively. The recoveries of the target analytes were between 93.0% and 110.3% at spiked levels of 0.02, 0.1, and 0.2 μg/mL. As opposed to similar reported methods, our method showed high sensitivity and recovery; furthermore, the matrix interference was eliminated, and the chromatographic peaks of the analytes were completely separated from the impurity peaks at the level of 0.2 μg/mL. The automatic solid-phase extraction equipment is convenient to operate and allows one to process samples in batches. The conditions for solid-phase extraction can be precisely controlled, and the detection accuracy is greatly improved. In addition, a large number of sample tests can be performed by a few experimenters. Hence, this method facilitates simple and rapid forensic toxicology testing and drug abuse monitoring on a large scale.