Urbanization is the development trend of all countries in the world, but it has caused considerable ecological problems that need to be alleviated by building ecological security patterns. This study took Ningbo as an example to construct and optimize an ecological security pattern. We analyzed land use types, normalized difference vegetation index, and landscape connectivity for ecological sources selection. In constructing the resistance surface, we considered natural and socio-economic factors. On this basis, we identified ecological corridors based on a minimum cumulative resistance model. Finally, the ecological security pattern was optimized through space syntax. Results showed that Ningbo has 18 ecological sources, with an area of 3,051.27 km2 and 29 ecological corridors, with a length of 1,172.18 km. Among them, 11 are first-level, 10 are second-level, and 8 are third-level corridors. After optimization, the area and protection cost of the ecological security pattern were significantly reduced, which can effectively alleviate the trade-off between ecological protection and economic development. This research can provide a reference for the construction and optimization of ecological security patterns and has reference significance for ecological protection in rapidly urbanized areas.