Ni-doped TiO2, which was fabricated via sol–gel method in the present work, possesses the best photoelectrochemical anticorrosion property for 304 stainless steel under visible light illumination when the Ni doping amount is 0.5% (Ti0.995Ni0.005O2). The Ni-doping leads to the extension of the photoresponse of TiO2 from ultraviolet to visible light. Ni is substitutionally doped in TiO2 matrix by substituting the Ti4+ lattice sites and the oxygen vacancy forms for Ti0.995Ni0.005O2. The oxygen vacancy promotes the transfer of photoinduced electrons, resulting in the increase of the photo-to-current conversion efficiency of TiO2 under visible light. Finally, the oxygen vacancy and Ni-doping efficiently enhance the photoelectrochemical anticorrosion property of TiO2 under visible light illumination.