-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...uth/src/main/java/com/achobeta/www/oauth/config/handler/AuthenticationFailureHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.achobeta.www.oauth.config.handler; | ||
|
||
import com.achobeta.www.common.util.GlobalServiceStatusCode; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.web.server.WebFilterExchange; | ||
import org.springframework.security.web.server.authentication.ServerAuthenticationFailureHandler; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static com.achobeta.www.oauth.utils.ResponseUtil.createAccessDeniedResponse; | ||
|
||
/** | ||
* <span> | ||
* handler authentication failure | ||
* </span> | ||
* | ||
* @author jettcc in 2023/10/23 | ||
* @version 1.0 | ||
*/ | ||
public class AuthenticationFailureHandler implements ServerAuthenticationFailureHandler { | ||
@Override | ||
public Mono<Void> onAuthenticationFailure(WebFilterExchange webFilterExchange, AuthenticationException exception) { | ||
ServerHttpResponse response = webFilterExchange.getExchange().getResponse(); | ||
return createAccessDeniedResponse(response, GlobalServiceStatusCode.USER_NO_PERMISSION); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...uth/src/main/java/com/achobeta/www/oauth/config/handler/AuthenticationSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.achobeta.www.oauth.config.handler; | ||
|
||
import com.achobeta.www.common.util.GlobalServiceStatusCode; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.server.WebFilterExchange; | ||
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static com.achobeta.www.oauth.utils.ResponseUtil.createAccessDeniedResponse; | ||
|
||
/** | ||
* <span> | ||
* authentication success handler | ||
* </span> | ||
* | ||
* @author jettcc in 2023/10/23 | ||
* @version 1.0 | ||
*/ | ||
public class AuthenticationSuccessHandler implements ServerAuthenticationSuccessHandler { | ||
@Override | ||
public Mono<Void> onAuthenticationSuccess(WebFilterExchange webFilterExchange, | ||
Authentication authentication) { | ||
ServerHttpResponse response = webFilterExchange.getExchange().getResponse(); | ||
return createAccessDeniedResponse(response, GlobalServiceStatusCode.SYSTEM_SUCCESS); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...java/com/achobeta/www/oauth/config/handler/logout/AuthenticationLogoutSuccessHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.achobeta.www.oauth.config.handler.logout; | ||
|
||
import com.achobeta.www.common.util.GlobalServiceStatusCode; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.server.WebFilterExchange; | ||
import org.springframework.security.web.server.authentication.logout.ServerLogoutSuccessHandler; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static com.achobeta.www.oauth.utils.ResponseUtil.createAccessDeniedResponse; | ||
/** | ||
* <span> | ||
* handler logout success logic | ||
* </span> | ||
* | ||
* @author jettcc in 2023/10/23 | ||
* @version 1.0 | ||
*/ | ||
public class AuthenticationLogoutSuccessHandler implements ServerLogoutSuccessHandler { | ||
@Override | ||
public Mono<Void> onLogoutSuccess(WebFilterExchange webFilterExchange, Authentication authentication) { | ||
ServerHttpResponse response = webFilterExchange.getExchange().getResponse(); | ||
return createAccessDeniedResponse(response, GlobalServiceStatusCode.SYSTEM_SUCCESS, "logout success"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
achobeta-infra-oauth/src/main/java/com/achobeta/www/oauth/utils/ResponseUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.achobeta.www.oauth.utils; | ||
|
||
import cn.hutool.json.JSONUtil; | ||
import com.achobeta.www.common.util.GlobalServiceStatusCode; | ||
import com.achobeta.www.common.util.SystemJsonResponse; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.server.reactive.ServerHttpResponse; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* <span> | ||
* authentication response util | ||
* </span> | ||
* | ||
* @author jettcc in 2023/10/23 | ||
* @version 1.0 | ||
*/ | ||
public class ResponseUtil { | ||
/** | ||
* 构造webflux返回时的结构 | ||
*/ | ||
public static Mono<Void> createAccessDeniedResponse(ServerHttpResponse resp, GlobalServiceStatusCode code) { | ||
return createResponse(resp, code, null); | ||
} | ||
|
||
public static Mono<Void> createAccessDeniedResponse(ServerHttpResponse resp, GlobalServiceStatusCode code, String msg) { | ||
return createResponse(resp, code, msg); | ||
} | ||
|
||
private static Mono<Void> createResponse(ServerHttpResponse resp, GlobalServiceStatusCode code, String msg) { | ||
resp.setStatusCode(HttpStatus.OK); | ||
resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); | ||
return resp.writeWith(Mono.just(resp.bufferFactory() | ||
.wrap(JSONUtil.toJsonStr(buildResponseMessage(code, msg)) | ||
.getBytes(StandardCharsets.UTF_8)))); | ||
} | ||
|
||
private static SystemJsonResponse buildResponseMessage(GlobalServiceStatusCode code, String msg) { | ||
if (code == GlobalServiceStatusCode.SYSTEM_SUCCESS) { | ||
return SystemJsonResponse.SYSTEM_SUCCESS(msg); | ||
} | ||
return SystemJsonResponse.CUSTOMIZE_ERROR(code); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters