Code style
This commit is contained in:
@@ -178,5 +178,4 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -19,19 +19,13 @@ package we.plugin.dedicatedline.auth;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
|
|
||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
import we.dedicated_line.DedicatedLineService;
|
import we.dedicated_line.DedicatedLineService;
|
||||||
|
|
||||||
|
|
||||||
import we.plugin.FizzPluginFilter;
|
import we.plugin.FizzPluginFilter;
|
||||||
import we.plugin.FizzPluginFilterChain;
|
import we.plugin.FizzPluginFilterChain;
|
||||||
import we.util.ReactorUtils;
|
import we.util.ReactorUtils;
|
||||||
@@ -41,62 +35,52 @@ import javax.annotation.Resource;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Francis Dong
|
* @author Francis Dong
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@Component(DedicatedLineApiAuthPluginFilter.DEDICATED_LINE_API_AUTH_PLUGIN_FILTER)
|
@Component(DedicatedLineApiAuthPluginFilter.DEDICATED_LINE_API_AUTH_PLUGIN_FILTER)
|
||||||
public class DedicatedLineApiAuthPluginFilter implements FizzPluginFilter {
|
public class DedicatedLineApiAuthPluginFilter implements FizzPluginFilter {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(DedicatedLineApiAuthPluginFilter.class);
|
private static final Logger log = LoggerFactory.getLogger(DedicatedLineApiAuthPluginFilter.class);
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private DedicatedLineService dedicatedLineService;
|
||||||
|
|
||||||
|
public static final String DEDICATED_LINE_API_AUTH_PLUGIN_FILTER = "dedicatedLineApiAuthPlugin";
|
||||||
|
|
||||||
@Resource
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
private DedicatedLineService dedicatedLineService;
|
@Override
|
||||||
|
public Mono<Void> filter(ServerWebExchange exchange, Map<String, Object> config) {
|
||||||
public static final String DEDICATED_LINE_API_AUTH_PLUGIN_FILTER = "dedicatedLineCodecPlugin";
|
String traceId = WebUtils.getTraceId(exchange);
|
||||||
|
try {
|
||||||
|
String dedicatedLineId = WebUtils.getDedicatedLineId(exchange);
|
||||||
|
String service = WebUtils.getClientService(exchange);
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
String path = WebUtils.getClientReqPath(exchange);
|
||||||
@Override
|
HttpMethod method = exchange.getRequest().getMethod();
|
||||||
public Mono<Void> filter(ServerWebExchange exchange, Map<String, Object> config) {
|
if (dedicatedLineService.auth(dedicatedLineId, method, service, path)) {
|
||||||
String traceId = WebUtils.getTraceId(exchange);
|
// Go to next plugin
|
||||||
try {
|
Mono next = FizzPluginFilterChain.next(exchange);
|
||||||
|
return next.defaultIfEmpty(ReactorUtils.NULL).flatMap(nil -> {
|
||||||
String dedicatedLineId = WebUtils.getDedicatedLineId(exchange);
|
doAfter();
|
||||||
String service = WebUtils.getClientService(exchange);
|
return Mono.empty();
|
||||||
String path = WebUtils.getClientReqPath(exchange);
|
});
|
||||||
HttpMethod method = exchange.getRequest().getMethod();
|
} else {
|
||||||
if (dedicatedLineService.auth(dedicatedLineId, method, service, path)) {
|
// Auth failed
|
||||||
|
ServerHttpResponse response = exchange.getResponse();
|
||||||
// Go to next plugin
|
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
||||||
Mono next = FizzPluginFilterChain.next(exchange);
|
response.getHeaders().setCacheControl("no-store");
|
||||||
return next.defaultIfEmpty(ReactorUtils.NULL).flatMap(nil -> {
|
response.getHeaders().setExpires(0);
|
||||||
doAfter();
|
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
||||||
return Mono.empty();
|
HttpStatus.UNAUTHORIZED.getReasonPhrase(), traceId);
|
||||||
});
|
return WebUtils.response(exchange, HttpStatus.UNAUTHORIZED, null, respJson);
|
||||||
} else {
|
}
|
||||||
// Auth failed
|
} catch (Exception e) {
|
||||||
ServerHttpResponse response = exchange.getResponse();
|
log.error("{} {} exception", traceId, DEDICATED_LINE_API_AUTH_PLUGIN_FILTER, e);
|
||||||
response.setStatusCode(HttpStatus.UNAUTHORIZED);
|
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||||
response.getHeaders().setCacheControl("no-store");
|
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), traceId);
|
||||||
response.getHeaders().setExpires(0);
|
return WebUtils.response(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, respJson);
|
||||||
String respJson = WebUtils.jsonRespBody(HttpStatus.UNAUTHORIZED.value(),
|
}
|
||||||
HttpStatus.UNAUTHORIZED.getReasonPhrase(), traceId);
|
}
|
||||||
return WebUtils.response(exchange, HttpStatus.UNAUTHORIZED, null, respJson);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("{} {} exception", traceId, DEDICATED_LINE_API_AUTH_PLUGIN_FILTER, e);
|
|
||||||
String respJson = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), traceId);
|
|
||||||
return WebUtils.response(exchange, HttpStatus.INTERNAL_SERVER_ERROR, null, respJson);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void doAfter() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void doAfter() {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user