先自定义一个配置类
package com.springboot2.thyemleaf.Configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Created by lpw'ASUS on 2018/5/29. * 自定义CORS配置类 */ @Configuration public class CustomCorsConfiguration { @Bean public WebMvcConfigurer getConfigure(){ return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { //允许访问哪个接口,允许站外的http://localhost:8989访问,allowedOrigins这里天的地址是将来要请求你的地址,例如baidu要请求你,就写百度 registry.addMapping("/api/**").allowedOrigins("http://localhost:9999"); } }; } }
以项目本身端口访问不收限制。例如本服务为localhost:8888、那么以localhost:8888开头的请求路径不受限制Controller,除此之外,http://localhost:9999 只能登陆一部分人。