Skip to content

Commit

Permalink
修改目录状态,优化增加的问题.
Browse files Browse the repository at this point in the history
  • Loading branch information
vnobo committed Nov 6, 2023
1 parent 496e8c7 commit 9311d51
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.platform.boot.commons.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.boot.commons.Snowflake;
import com.platform.boot.commons.annotation.exception.JsonException;
import com.platform.boot.commons.annotation.exception.RestServerException;
import com.platform.boot.security.SecurityDetails;
import com.platform.boot.security.UserAuditor;
Expand Down Expand Up @@ -60,6 +62,14 @@ public final class ContextUtils implements Serializable {
ContextUtils.USERS_SERVICE = usersService;
}

public static byte[] objectToBytes(Object object) {
try {
return ContextUtils.OBJECT_MAPPER.writeValueAsBytes(object);
} catch (JsonProcessingException e) {
throw JsonException.withError(e);
}
}

public static String getClientIpAddress(ServerHttpRequest httpRequest) {
HttpHeaders headers = httpRequest.getHeaders();
for (String header : IP_HEADER_CANDIDATES) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.platform.boot.config;

import com.platform.boot.commons.ErrorResponse;
import com.platform.boot.commons.utils.ContextUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.autoconfigure.security.reactive.PathRequest;
Expand All @@ -9,6 +11,7 @@
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
Expand Down Expand Up @@ -107,20 +110,20 @@ static class CustomServerAuthenticationEntryPoint extends HttpBasicServerAuthent
public Mono<Void> commence(ServerWebExchange exchange, AuthenticationException e) {
String xRequestedWith = "X-Requested-With";
String xmlHttpRequest = "XMLHttpRequest";
String requestedWith = exchange.getRequest().getHeaders().getFirst(xRequestedWith);
ServerHttpRequest request = exchange.getRequest();
String requestedWith = request.getHeaders().getFirst(xRequestedWith);

log.error("认证失败! 信息: %s".formatted(e.getMessage()));

if (requestedWith != null && requestedWith.contains(xmlHttpRequest)) {
var response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
var body = """
{"code":401,"msg":"认证失败,检查你的用户名,密码是否正确或安全密钥是否过期!","errors":"%s"}
""";
body = body.formatted(e.getMessage());
ErrorResponse errorResponse = ErrorResponse.of(request.getId(), request.getPath().value(),
4010, "认证失败,检查你的用户名,密码是否正确或安全密钥是否过期!", List.of(e.getMessage()));
var body = ContextUtils.objectToBytes(errorResponse);
var dataBufferFactory = response.bufferFactory();
var bodyBuffer = dataBufferFactory.wrap(body.getBytes());
var bodyBuffer = dataBufferFactory.wrap(body);
return response.writeAndFlushWith(Flux.just(bodyBuffer).windowUntilChanged())
.doOnError((error) -> DataBufferUtils.release(bodyBuffer));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ export class HandleErrorInterceptor implements HttpInterceptor {
catchError(err => this.handleError(err)));
}

private handleError(error: HttpErrorResponse) {
private handleError(errorResponse: HttpErrorResponse) {

this._snackBar.open(error.message, $localize`:@@snackBarAction:Close`, {
this._snackBar.open(errorResponse.error.message, $localize`:@@snackBarAction:Close`, {
duration: 3000, verticalPosition: 'top', horizontalPosition: 'center'
});

if (error.status === 401) {
if (errorResponse.status === 401) {
this.authService.logout();
this.router.navigate([this.authService.loginUrl]).then();
return throwError(() => $localize`:@@errorMessage401:Authenticate is noniff ,please login again.`);
} else if (error.status === 407) {
} else if (errorResponse.status === 407) {
this.authService.logout();
this.router.navigate([this.authService.loginUrl]).then();
return throwError(() => $localize`:@@errorMessage407:Authenticate is incorrectness,please login again.`);
} else if (error.status === 403) {
} else if (errorResponse.status === 403) {
this.authService.logout();
this.router.navigate([this.authService.loginUrl]).then();
return throwError(() => $localize`:@@errorMessage403:Captcha Token is incorrectness,please login again.`);
}
console.error($localize`:@@errorMessage:Backend returned code ${error.status}, body was: `, error.error);
console.error($localize`:@@errorMessage:Backend returned code ${errorResponse.status}, body was: `, errorResponse.error);
// return an observable with a user-facing error message
return throwError(() => error);
return throwError(() => errorResponse);
}
}

0 comments on commit 9311d51

Please sign in to comment.