generated from halo-dev/plugin-starter
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor OAuth2 login for Halo 2.20 (#64)
This PR refactors OAuth2 login for adapting Halo 2.20. See halo-dev/halo#6702 for more. #### How to build? 1. Checkout to halo-dev/halo#6702 2. Execute command `./gradlew publishToMavenLocal -Pversion=2.20.0+local.5` in Halo project 3. Execute command `./gradlew build` in current project. ```release-note None ```
- Loading branch information
Showing
20 changed files
with
152 additions
and
1,213 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
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
52 changes: 0 additions & 52 deletions
52
src/main/java/run/halo/oauth/DefaultSocialUserDetailsService.java
This file was deleted.
Oops, something went wrong.
62 changes: 0 additions & 62 deletions
62
src/main/java/run/halo/oauth/DefaultUserDetailsService.java
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/main/java/run/halo/oauth/HaloOAuth2AuthenticationWebFilter.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,58 @@ | ||
package run.halo.oauth; | ||
|
||
import org.springframework.security.oauth2.client.authentication.OAuth2LoginReactiveAuthenticationManager; | ||
import org.springframework.security.oauth2.client.endpoint.WebClientReactiveAuthorizationCodeTokenResponseClient; | ||
import org.springframework.security.oauth2.client.userinfo.DefaultReactiveOAuth2UserService; | ||
import org.springframework.security.oauth2.client.web.server.ServerOAuth2AuthorizationCodeAuthenticationTokenConverter; | ||
import org.springframework.security.oauth2.client.web.server.authentication.OAuth2LoginAuthenticationWebFilter; | ||
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationFailureHandler; | ||
import org.springframework.security.web.server.authentication.RedirectServerAuthenticationSuccessHandler; | ||
import org.springframework.security.web.server.context.ServerSecurityContextRepository; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.security.AuthenticationSecurityWebFilter; | ||
|
||
/** | ||
* OAuth2 authentication web filter. | ||
* | ||
* @author johnniang | ||
* @since 2.20.0 | ||
*/ | ||
@Component | ||
public class HaloOAuth2AuthenticationWebFilter implements AuthenticationSecurityWebFilter { | ||
|
||
private final WebFilter delegate; | ||
|
||
public HaloOAuth2AuthenticationWebFilter(Oauth2LoginConfiguration configuration, | ||
ServerSecurityContextRepository securityContextRepository) { | ||
var authManager = new OAuth2LoginReactiveAuthenticationManager( | ||
new WebClientReactiveAuthorizationCodeTokenResponseClient(), | ||
new DefaultReactiveOAuth2UserService() | ||
); | ||
var filter = new OAuth2LoginAuthenticationWebFilter(authManager, | ||
configuration.getAuthorizedClientRepository()); | ||
filter.setRequiresAuthenticationMatcher(configuration.getAuthenticationMatcher()); | ||
var converter = new ServerOAuth2AuthorizationCodeAuthenticationTokenConverter( | ||
configuration.getClientRegistrationRepository() | ||
); | ||
var successHandler = new RedirectServerAuthenticationSuccessHandler("/uc"); | ||
successHandler.setRequestCache(configuration.getRequestCache()); | ||
filter.setAuthenticationSuccessHandler(successHandler); | ||
filter.setAuthenticationFailureHandler( | ||
new RedirectServerAuthenticationFailureHandler("/login?oauth2_error") | ||
); | ||
filter.setServerAuthenticationConverter(converter); | ||
filter.setSecurityContextRepository(securityContextRepository); | ||
|
||
this.delegate = filter; | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
return delegate.filter(exchange, chain); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/run/halo/oauth/HaloOAuth2RedirectWebFilter.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,33 @@ | ||
package run.halo.oauth; | ||
|
||
import org.springframework.security.oauth2.client.web.server.OAuth2AuthorizationRequestRedirectWebFilter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
import run.halo.app.security.HttpBasicSecurityWebFilter; | ||
|
||
@Component | ||
public class HaloOAuth2RedirectWebFilter implements HttpBasicSecurityWebFilter { | ||
|
||
private final WebFilter delegate; | ||
|
||
public HaloOAuth2RedirectWebFilter(Oauth2LoginConfiguration configuration) { | ||
this.delegate = createDelegate(configuration); | ||
} | ||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
return delegate.filter(exchange, chain); | ||
} | ||
|
||
private static OAuth2AuthorizationRequestRedirectWebFilter createDelegate( | ||
Oauth2LoginConfiguration configuration | ||
) { | ||
return new OAuth2AuthorizationRequestRedirectWebFilter( | ||
configuration.getClientRegistrationRepository() | ||
); | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.