From 9654776025968dcc4c9e41ee7b6578d72bbf9b43 Mon Sep 17 00:00:00 2001 From: xiangpei <xiangpei@timesnew.cn> Date: 星期四, 16 五月 2024 09:49:26 +0800 Subject: [PATCH] 统一相应类重构 --- src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java | 36 +++++++++--------- src/main/java/com/mindskip/xzs/configuration/spring/security/RestUtil.java | 2 src/main/java/com/mindskip/xzs/base/RestResponse.java | 48 ++++++------------------ 3 files changed, 31 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/mindskip/xzs/base/RestResponse.java b/src/main/java/com/mindskip/xzs/base/RestResponse.java index 8c7a0ac..e8b9d09 100644 --- a/src/main/java/com/mindskip/xzs/base/RestResponse.java +++ b/src/main/java/com/mindskip/xzs/base/RestResponse.java @@ -15,8 +15,8 @@ * @param message the message */ public RestResponse(int code, String message) { - this.code = code; - this.message = message; + this.put("code", code); + this.put("message", message); } /** @@ -26,10 +26,12 @@ * @param message the message * @param response the response */ - public RestResponse(int code, String message, T response) { - this.code = code; - this.message = message; - this.response = response; + public static RestResponse response(int code, String message, Object response) { + RestResponse restResponse = new RestResponse(); + restResponse.put("code", code); + restResponse.put("message", message); + restResponse.put("response", response); + return restResponse; } /** @@ -62,7 +64,7 @@ */ public static <F> RestResponse<F> ok(F response) { SystemCode systemCode = SystemCode.OK; - return new RestResponse<>(systemCode.getCode(), systemCode.getMessage(), response); + return RestResponse.response(systemCode.getCode(), systemCode.getMessage(), response); } /** @@ -71,16 +73,7 @@ * @return the code */ public int getCode() { - return code; - } - - /** - * Sets code. - * - * @param code the code - */ - public void setCode(int code) { - this.code = code; + return (int) this.get("code"); } /** @@ -89,17 +82,9 @@ * @return the message */ public String getMessage() { - return message; + return (String) this.get("message"); } - /** - * Sets message. - * - * @param message the message - */ - public void setMessage(String message) { - this.message = message; - } /** * Gets response. @@ -107,16 +92,7 @@ * @return the response */ public T getResponse() { - return response; - } - - /** - * Sets response. - * - * @param response the response - */ - public void setResponse(T response) { - this.response = response; + return (T) this.get("response"); } public RestResponse() { diff --git a/src/main/java/com/mindskip/xzs/configuration/spring/security/RestUtil.java b/src/main/java/com/mindskip/xzs/configuration/spring/security/RestUtil.java index e077343..9a74bd3 100644 --- a/src/main/java/com/mindskip/xzs/configuration/spring/security/RestUtil.java +++ b/src/main/java/com/mindskip/xzs/configuration/spring/security/RestUtil.java @@ -52,7 +52,7 @@ */ public static void response(HttpServletResponse response, int systemCode, String msg, Object content) { try { - RestResponse res = new RestResponse<>(systemCode, msg, content); + RestResponse res = RestResponse.response(systemCode, msg, content); String resStr = JsonUtil.toJsonStr(res); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(resStr); diff --git a/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java b/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java index 4440272..c2de944 100644 --- a/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java +++ b/src/main/java/com/mindskip/xzs/configuration/spring/security/SecurityConfigurer.java @@ -75,6 +75,7 @@ List<String> securityIgnoreUrls = systemConfig.getSecurityIgnoreUrls(); String[] ignores = new String[securityIgnoreUrls.size()]; http + .addFilterAt(authenticationFilter(), UsernamePasswordAuthenticationFilter.class) .exceptionHandling().authenticationEntryPoint(restAuthenticationEntryPoint) .and().authenticationProvider(restAuthenticationProvider) .authorizeRequests() @@ -99,26 +100,25 @@ .and().rememberMe().key(CookieConfig.getName()).tokenValiditySeconds(CookieConfig.getInterval()).userDetailsService(formDetailsService) .and().csrf().disable() .cors(); - http.addFilterAt(authenticationFilter(), UsernamePasswordAuthenticationFilter.class); } -// /** -// * Cors configuration source cors configuration source. -// * -// * @return the cors configuration source -// */ -// @Bean -// public CorsConfigurationSource corsConfigurationSource() { -// final CorsConfiguration configuration = new CorsConfiguration(); -// configuration.setMaxAge(3600L); -// configuration.setAllowedOrigins(Collections.singletonList("*")); -// configuration.setAllowedMethods(Collections.singletonList("*")); -// configuration.setAllowCredentials(true); -// configuration.setAllowedHeaders(Collections.singletonList("*")); -// final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); -// source.registerCorsConfiguration("/api/**", configuration); -// return source; -// } + /** + * Cors configuration source cors configuration source. + * + * @return the cors configuration source + */ + @Bean + public CorsConfigurationSource corsConfigurationSource() { + final CorsConfiguration configuration = new CorsConfiguration(); + configuration.setMaxAge(3600L); + configuration.setAllowedOrigins(Collections.singletonList("*")); + configuration.setAllowedMethods(Collections.singletonList("*")); + configuration.setAllowCredentials(true); + configuration.setAllowedHeaders(Collections.singletonList("*")); + final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/api/**", configuration); + return source; + } /** -- Gitblit v1.8.0