From 07ad9509e6f1500d40b60f346e968884b815c2b8 Mon Sep 17 00:00:00 2001 From: "shengzhaoli.shengz" Date: Tue, 31 Oct 2023 12:01:27 +0800 Subject: [PATCH] /oauth2/rest_token impl/ test, comment PASSWORD --- .../sos/config/OAuth2ServerConfiguration.java | 2 +- .../web/controller/OAuthRestController.java | 20 ++++++++++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/monkeyk/sos/config/OAuth2ServerConfiguration.java b/src/main/java/com/monkeyk/sos/config/OAuth2ServerConfiguration.java index a4d258c..c4366dd 100644 --- a/src/main/java/com/monkeyk/sos/config/OAuth2ServerConfiguration.java +++ b/src/main/java/com/monkeyk/sos/config/OAuth2ServerConfiguration.java @@ -140,7 +140,7 @@ public class OAuth2ServerConfiguration { strings.add(SignatureAlgorithm.ES256.getName()); }).grantTypes(grantTypes -> { //向下兼容添加,v3.0.0 - grantTypes.add(AuthorizationGrantType.PASSWORD.getValue()); +// grantTypes.add(AuthorizationGrantType.PASSWORD.getValue()); grantTypes.add(AuthorizationGrantType.JWT_BEARER.getValue()); }) .scopes(strings -> { diff --git a/src/main/java/com/monkeyk/sos/web/controller/OAuthRestController.java b/src/main/java/com/monkeyk/sos/web/controller/OAuthRestController.java index 9558d1a..59c8eee 100644 --- a/src/main/java/com/monkeyk/sos/web/controller/OAuthRestController.java +++ b/src/main/java/com/monkeyk/sos/web/controller/OAuthRestController.java @@ -17,6 +17,7 @@ import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -25,6 +26,7 @@ import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.server.ServletServerHttpResponse; import org.springframework.security.authentication.AbstractAuthenticationToken; import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.context.SecurityContextHolder; @@ -38,7 +40,9 @@ import org.springframework.security.web.authentication.WebAuthenticationDetails; import org.springframework.stereotype.Controller; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.ResponseBody; import java.io.IOException; import java.time.temporal.ChronoUnit; @@ -166,15 +170,21 @@ public class OAuthRestController implements InitializingBean, ApplicationContext @Override public void afterPropertiesSet() throws Exception { -// Assert.state(authenticationManager != null, "AuthenticationManager must be provided"); + Assert.state(authenticationManager != null, "AuthenticationManager must be provided"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { -// if (this.authenticationManager == null) { -// this.authenticationManager = (AuthenticationManager) applicationContext.getBean("authenticationManagerBean"); -// } + if (this.authenticationManager == null) { + AuthenticationConfiguration configuration = applicationContext.getBean(AuthenticationConfiguration.class); + Assert.notNull(configuration, "AuthenticationManagerBuilder is null"); + try { + this.authenticationManager = configuration.getAuthenticationManager(); + } catch (Exception e) { + throw new BeanInitializationException("Call 'getAuthenticationManager' error", e); + } + } } }