Skip to content

Commit

Permalink
perf($starter): beanify ResponseUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Sep 17, 2021
1 parent 0b33af7 commit 7e4e7be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jmsoftware.maf.apigateway.remoteapi.AuthCenterRemoteApi;
import com.jmsoftware.maf.apigateway.security.impl.*;
import com.jmsoftware.maf.reactivespringcloudstarter.configuration.MafConfiguration;
import com.jmsoftware.maf.reactivespringcloudstarter.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -79,13 +80,13 @@ SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http,
}

@Bean
public ServerAuthenticationEntryPoint serverAuthenticationEntryPoint() {
return new ServerAuthenticationEntryPointImpl();
public ServerAuthenticationEntryPoint serverAuthenticationEntryPoint(ResponseUtil responseUtil) {
return new ServerAuthenticationEntryPointImpl(responseUtil);
}

@Bean
public ServerAccessDeniedHandler serverAccessDeniedHandler() {
return new GatewayServerAccessDeniedHandlerImpl();
public ServerAccessDeniedHandler serverAccessDeniedHandler(ResponseUtil responseUtil) {
return new GatewayServerAccessDeniedHandlerImpl(responseUtil);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jmsoftware.maf.apigateway.security.impl;

import com.jmsoftware.maf.reactivespringcloudstarter.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
Expand All @@ -16,11 +17,14 @@
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/29/2020 9:56 AM
**/
@Slf4j
@RequiredArgsConstructor
public class GatewayServerAccessDeniedHandlerImpl implements ServerAccessDeniedHandler {
private final ResponseUtil responseUtil;

@Override
public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException denied) {
log.error("Access denied! Exception message: {}. Request URL: [{}] {}", denied.getMessage(),
exchange.getRequest().getMethod(), exchange.getRequest().getURI());
return ResponseUtil.renderJson(exchange, HttpStatus.FORBIDDEN, denied.getMessage(), null);
return this.responseUtil.renderJson(exchange, HttpStatus.FORBIDDEN, denied.getMessage(), null);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jmsoftware.maf.apigateway.security.impl;

import com.jmsoftware.maf.reactivespringcloudstarter.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
Expand All @@ -14,11 +15,14 @@
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/21/2020 9:48 AM
**/
@Slf4j
@RequiredArgsConstructor
public class ServerAuthenticationEntryPointImpl implements ServerAuthenticationEntryPoint {
private final ResponseUtil responseUtil;

@Override
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
log.error("Exception occurred when authenticating! Exception message: {}. Request URL: [{}] {}", e.getMessage(),
exchange.getRequest().getMethod(), exchange.getRequest().getURI());
return ResponseUtil.renderJson(exchange, HttpStatus.NETWORK_AUTHENTICATION_REQUIRED, e.getMessage(), null);
return this.responseUtil.renderJson(exchange, HttpStatus.NETWORK_AUTHENTICATION_REQUIRED, e.getMessage(), null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jmsoftware.maf.reactivespringcloudstarter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jmsoftware.maf.reactivespringcloudstarter.configuration.*;
import com.jmsoftware.maf.reactivespringcloudstarter.controller.CommonController;
import com.jmsoftware.maf.reactivespringcloudstarter.filter.AccessLogFilter;
Expand All @@ -8,6 +9,7 @@
import com.jmsoftware.maf.reactivespringcloudstarter.redis.RedisConfiguration;
import com.jmsoftware.maf.reactivespringcloudstarter.service.CommonService;
import com.jmsoftware.maf.reactivespringcloudstarter.service.impl.CommonServiceImpl;
import com.jmsoftware.maf.reactivespringcloudstarter.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down Expand Up @@ -72,4 +74,10 @@ public CommonController commonController(CommonService commonService) {
log.warn("Initial bean: '{}'", CommonController.class.getSimpleName());
return new CommonController(commonService);
}

@Bean
public ResponseUtil responseUtil(ObjectMapper objectMapper) {
log.warn("Initial bean: '{}'", ResponseUtil.class.getSimpleName());
return new ResponseUtil(objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jmsoftware.maf.common.bean.ResponseBodyBean;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.val;
import org.springframework.http.HttpStatus;
Expand All @@ -17,7 +18,10 @@
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/21/2020 9:19 AM
**/
@RequiredArgsConstructor
public class ResponseUtil {
private final ObjectMapper objectMapper;

/**
* Render json mono.
*
Expand All @@ -28,9 +32,8 @@ public class ResponseUtil {
* @return the mono
*/
@SneakyThrows
public static Mono<Void> renderJson(@NonNull ServerWebExchange exchange, @NonNull HttpStatus httpStatus,
@Nullable String message, @Nullable Object data) {
val objectMapper = new ObjectMapper();
public Mono<Void> renderJson(@NonNull ServerWebExchange exchange, @NonNull HttpStatus httpStatus,
@Nullable String message, @Nullable Object data) {
exchange.getResponse().setStatusCode(httpStatus);
val response = exchange.getResponse();
response.setStatusCode(httpStatus);
Expand All @@ -42,7 +45,7 @@ public static Mono<Void> renderJson(@NonNull ServerWebExchange exchange, @NonNul
val message2 = String.format("%s. %s", httpStatus.getReasonPhrase(), message);
val responseBody = ResponseBodyBean.ofStatus(httpStatus.value(), message2, data);
try {
return bufferFactory.wrap(objectMapper.writeValueAsBytes(responseBody));
return bufferFactory.wrap(this.objectMapper.writeValueAsBytes(responseBody));
} catch (JsonProcessingException e) {
return bufferFactory.wrap(new byte[0]);
}
Expand Down

0 comments on commit 7e4e7be

Please sign in to comment.