2424import org .springframework .beans .BeansException ;
2525import org .springframework .boot .SpringApplication ;
2626import org .springframework .boot .autoconfigure .SpringBootApplication ;
27- import org .springframework .boot .context . embedded . ConfigurableEmbeddedServletContainer ;
28- import org .springframework .boot .context . embedded . EmbeddedServletContainerCustomizer ;
27+ import org .springframework .boot .web . server . WebServerFactoryCustomizer ;
28+ import org .springframework .boot .web . servlet . server . ConfigurableServletWebServerFactory ;
2929import org .springframework .context .ApplicationContext ;
3030import org .springframework .context .ApplicationContextAware ;
3131import org .springframework .context .annotation .Bean ;
3232import org .springframework .context .annotation .Configuration ;
33- import org .springframework .web .cors .CorsConfiguration ;
34- import org .springframework .web .cors .UrlBasedCorsConfigurationSource ;
35- import org .springframework .web .filter .CorsFilter ;
33+ import org .springframework .web .servlet .config .annotation .CorsRegistry ;
34+ import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
3635
3736import com .alibaba .fastjson .JSON ;
3837import com .alibaba .fastjson .JSONObject ;
3938import com .alibaba .fastjson .serializer .PropertyFilter ;
4039
4140import apijson .Log ;
41+ import apijson .NotNull ;
4242import apijson .StringUtil ;
4343import apijson .demo .DemoFunctionParser ;
4444import apijson .demo .DemoParser ;
5757import unitauto .MethodUtil .Argument ;
5858import unitauto .MethodUtil .InstanceGetter ;
5959import unitauto .MethodUtil .JSONCallback ;
60- import unitauto .NotNull ;
6160import unitauto .jar .UnitAutoApp ;
6261
6362
6766 */
6867@ Configuration
6968@ SpringBootApplication
70- public class DemoApplication implements ApplicationContextAware {
69+ public class DemoApplication implements ApplicationContextAware , WebServerFactoryCustomizer < ConfigurableServletWebServerFactory > {
7170 private static final String TAG = "DemoApplication" ;
7271
7372 static {
7473 // APIJSON 配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
75-
74+
7675 Map <String , Pattern > COMPILE_MAP = AbstractVerifier .COMPILE_MAP ;
7776 COMPILE_MAP .put ("PHONE" , StringUtil .PATTERN_PHONE );
7877 COMPILE_MAP .put ("EMAIL" , StringUtil .PATTERN_EMAIL );
7978 COMPILE_MAP .put ("ID_CARD" , StringUtil .PATTERN_ID_CARD );
80-
79+
8180 // 使用本项目的自定义处理类
8281 APIJSONApplication .DEFAULT_APIJSON_CREATOR = new APIJSONCreator () {
83-
82+
8483 @ Override
8584 public Parser <Long > createParser () {
8685 return new DemoParser ();
@@ -89,12 +88,12 @@ public Parser<Long> createParser() {
8988 public FunctionParser createFunctionParser () {
9089 return new DemoFunctionParser ();
9190 }
92-
91+
9392 @ Override
9493 public Verifier <Long > createVerifier () {
9594 return new DemoVerifier ();
9695 }
97-
96+
9897 @ Override
9998 public SQLConfig createSQLConfig () {
10099 return new DemoSQLConfig ();
@@ -103,17 +102,17 @@ public SQLConfig createSQLConfig() {
103102 public SQLExecutor createSQLExecutor () {
104103 return new DemoSQLExecutor ();
105104 }
106-
105+
107106 };
108-
107+
109108 // APIJSON 配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
110-
111-
112-
109+
110+
111+
113112 // UnitAuto 单元测试配置 https://github.com/TommyLemon/UnitAuto <<<<<<<<<<<<<<<<<<<<<<<<<<<
114-
113+
115114 UnitAutoApp .init ();
116-
115+
117116 // 适配 Spring 注入的类及 Context 等环境相关的类
118117 final InstanceGetter ig = MethodUtil .INSTANCE_GETTER ;
119118 MethodUtil .INSTANCE_GETTER = new InstanceGetter () {
@@ -131,7 +130,7 @@ public Object getInstance(@NotNull Class<?> clazz, List<Argument> classArgs, Boo
131130 return ig .getInstance (clazz , classArgs , reuse );
132131 }
133132 };
134-
133+
135134 // 排除转换 JSON 异常的类,一般是 Context 等环境相关的类
136135 final JSONCallback jc = MethodUtil .JSON_CALLBACK ;
137136 MethodUtil .JSON_CALLBACK = new JSONCallback () {
@@ -145,7 +144,7 @@ public JSONObject newSuccessResult() {
145144 public JSONObject newErrorResult (Throwable e ) {
146145 return jc .newErrorResult (e );
147146 }
148-
147+
149148 @ Override
150149 public JSONObject parseJSON (String type , Object value ) {
151150 if (value == null || unitauto .JSON .isBooleanOrNumberOrString (value ) || value instanceof JSON || value instanceof Enum ) {
@@ -154,7 +153,6 @@ public JSONObject parseJSON(String type, Object value) {
154153
155154 if (value instanceof ApplicationContext
156155 || value instanceof Context
157- || value instanceof javax .validation .MessageInterpolator .Context
158156 || value instanceof org .omg .CORBA .Context
159157 || value instanceof org .apache .catalina .Context
160158 || value instanceof ch .qos .logback .core .Context
@@ -172,7 +170,6 @@ public boolean apply(Object object, String name, Object value) {
172170
173171 if (value instanceof ApplicationContext
174172 || value instanceof Context
175- || value instanceof javax .validation .MessageInterpolator .Context
176173 || value instanceof org .omg .CORBA .Context
177174 || value instanceof org .apache .catalina .Context
178175 || value instanceof ch .qos .logback .core .Context
@@ -190,12 +187,12 @@ public boolean apply(Object object, String name, Object value) {
190187
191188 return jc .parseJSON (type , value );
192189 }
193-
190+
194191 };
195-
192+
196193 // UnitAuto 单元测试配置 https://github.com/TommyLemon/UnitAuto >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
197-
198-
194+
195+
199196 // 把以下需要用到的数据库驱动取消注释即可,如果这里没有可以自己新增
200197 // try { //加载驱动程序
201198 // Log.d(TAG, "尝试加载 SQLServer 驱动 <<<<<<<<<<<<<<<<<<<<< ");
@@ -228,66 +225,47 @@ public boolean apply(Object object, String name, Object value) {
228225 // }
229226 }
230227
231-
228+
232229 public static void main (String [] args ) throws Exception {
233230 SpringApplication .run (DemoApplication .class , args );
234231
235232 Log .DEBUG = true ; // 上线生产环境前改为 false,可不输出 APIJSONORM 的日志 以及 SQLException 的原始(敏感)信息
236233 APIJSONApplication .init ();
237234 }
238-
239-
235+
236+ // SpringBoot 2.x 自定义端口方式
237+ @ Override
238+ public void customize (ConfigurableServletWebServerFactory server ) {
239+ server .setPort (8080 );
240+ }
241+
242+ // 全局 ApplicationContext 实例,方便 getBean 拿到 Spring/SpringBoot 注入的类实例
240243 private static ApplicationContext APPLICATION_CONTEXT ;
241244 public static ApplicationContext getApplicationContext () {
242245 return APPLICATION_CONTEXT ;
243246 }
244247 @ Override
245248 public void setApplicationContext (ApplicationContext applicationContext ) throws BeansException {
246249 APPLICATION_CONTEXT = applicationContext ;
247- }
248-
249- // SpringBoot 2.x 自定义端口方式
250- // @Bean
251- // public TomcatServletWebServerFactory servletContainer(){
252- // return new TomcatServletWebServerFactory(8081) ;
253- // }
254- // SpringBoot 1.x 自定义端口方式,配置文件加 server.port=80 无效(MacOS 10.10.?)
255- @ Bean
256- public EmbeddedServletContainerCustomizer containerCustomizer () {
257- return new EmbeddedServletContainerCustomizer () {
250+ }
251+
252+
253+ // 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
258254
255+ @ Bean
256+ public WebMvcConfigurer corsConfigurer () {
257+ return new WebMvcConfigurer () {
259258 @ Override
260- public void customize (ConfigurableEmbeddedServletContainer container ) {
261- container .setPort (8080 ); //自定义端口号,如果和 TiDB 等其它程序端口有冲突,可改为 8081, 9090, 9091 等未被占用的端口
259+ public void addCorsMappings (CorsRegistry registry ) {
260+ registry .addMapping ("/**" )
261+ .allowedOriginPatterns ("*" )
262+ .allowedMethods ("*" )
263+ .allowCredentials (true )
264+ .maxAge (3600 );
262265 }
263266 };
264267 }
265268
266-
267- // 支持 APIAuto 中 JavaScript 代码跨域请求 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
268-
269- /**
270- * 跨域过滤器
271- * @return
272- */
273- @ Bean
274- public CorsFilter corsFilter () {
275- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource ();
276- source .registerCorsConfiguration ("/**" , buildConfig ());
277- return new CorsFilter (source );
278- }
279- /**CORS跨域配置
280- * @return
281- */
282- private CorsConfiguration buildConfig () {
283- CorsConfiguration corsConfiguration = new CorsConfiguration ();
284- corsConfiguration .addAllowedOrigin ("*" ); //允许的域名或IP地址
285- corsConfiguration .addAllowedHeader ("*" ); //允许的请求头
286- corsConfiguration .addAllowedMethod ("*" ); //允许的HTTP请求方法
287- corsConfiguration .setAllowCredentials (true ); //允许发送跨域凭据,前端Axios存取JSESSIONID必须要
288- return corsConfiguration ;
289- }
290-
291269 // 支持 APIAuto 中 JavaScript 代码跨域请求 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
292270
293271}
0 commit comments