当前位置: 首页 > 知识库问答 >
问题:

具有AuthorizationGrantType.PASSWORD的Spring授权服务器

艾跃
2023-03-14

我想用Spring授权服务器项目构建一个授权服务器。现在我想使用AuthorizationGrantType.PASSWORD.

我从Spring授权服务器项目的示例中开发了一个演示项目。但是,当我尝试使用http://localhost:9000/oauth2/token?grant_type=password获取令牌时

我在这里错过了什么?

依赖项:spring boot starter web、spring boot-starter安全性、spring-security-oauth2-authorization-server(版本:0.2.2)

AuthorizationServerConfig类:

import java.security.*;
import java.security.interfaces.*;
import java.util.UUID;
import org.springframework.context.annotation.*;
import org.springframework.security.oauth2.core.*;
import org.springframework.security.oauth2.server.authorization.client.*;
import org.springframework.security.oauth2.server.authorization.config.ProviderSettings;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jose.jwk.*;
import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.proc.SecurityContext;

@Configuration(proxyBeanMethods = false)
public class AuthorizationServerConfig {
    
    @Bean
    public RegisteredClientRepository registeredClientRepository() {
        RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                .clientId("client1")
                .clientSecret("{noop}secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.PASSWORD)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .build();
        return new InMemoryRegisteredClientRepository(registeredClient);
    }
    
    @Bean
    public ProviderSettings providerSettings() {
        return ProviderSettings.builder()
                .issuer("http://auth-server:9000")
                .build();
    }
    
    @Bean
    public JWKSource<SecurityContext> jwkSource() {
        RSAKey rsaKey = generateRsa();
        JWKSet jwkSet = new JWKSet(rsaKey);
        return (jwkSelector, securityContext) -> jwkSelector.select(jwkSet);
    }
    
    private static RSAKey generateRsa() {
        KeyPair keyPair = generateRsaKey();
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
        return new RSAKey.Builder(publicKey)
                .privateKey(privateKey)
                .keyID(UUID.randomUUID().toString())
                .build();
    }
    
    private static KeyPair generateRsaKey() {
        KeyPair keyPair;
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(2048);
            keyPair = keyPairGenerator.generateKeyPair();
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
        return keyPair;
    }    
}

DefaultSecurityConfig类:

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.*;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;

@EnableWebSecurity
public class DefaultSecurityConfig {
    
    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .cors().disable()
                .authorizeRequests(authorizeRequests ->
                        authorizeRequests
                                .anyRequest()
                                .authenticated()
                );
        return http.build();
    }
    
    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails userDetails = User.withDefaultPasswordEncoder()
                .username("user")
                .password("pass")
                .roles("USER")
                .build();
        return new InMemoryUserDetailsManager(userDetails);
    }    
}

日志中有这样一条信息,我发现它可能是相关的:

o.s.s.w.a.i.FilterSecurityInterceptor    : Failed to authorize filter invocation [POST /oauth2/token?grant_type=password&username=user&password=pass] with attributes [authenticated]

共有2个答案

柯锋
2023-03-14

你可以实现一个密码授予仿真过滤器,请看我的例子:https://github . com/andreygrigoriev/spring _ authorization _ server _ password _ grant

屠锦
2023-03-14

似乎不支持密码grant_type。可能这就是我出错的原因。

来源:https://github.com/spring-projects/spring-authorization-server/issues/126

 类似资料:
  • 我们正在开发一个微服务架构中的应用程序,该应用程序在多个OAuth2提供商(如Google和Facebook)上使用Spring Cloud OAuth2实现登录。我们还在开发自己的授权服务器,并将在下一个版本中集成。 现在,在我们的微服务(即资源服务器)上,我想知道如何处理多个< code>token-info-uri或< code>user-info-uri到多个授权服务器(例如脸书或谷歌)。

  • 我在下面设置Spring配置: 和Maven设置如下:

  • 我需要了解在我的项目范围内使用autheorizaion服务器的便利性。 我们正在实现我们的服务并将其部署到客户环境中。 客户基础结构已经提供了一种身份验证机制,以便对用户进行身份验证。 此机制拦截所有通信并将用户重定向到“登录”表单。 之后,用户被重定向到我们的服务,我们必须处理和消化它,并使用JWT令牌进行响应。 这是我感到迷茫的地方: 我在想: 使用Spring oauth2 向授权服务器请

  • 我有两个Spring启动应用程序。后端部分-可以访问数据库,它用作Rest API和管理面板。前端部分-使用Rest API为客户端显示信息。 因此,我对客户端(前端)的配置安全性有问题,对管理面板也有问题,授权是通过会话实现的。以前,客户端部分的授权是通过JWT令牌实现的,但我不太明白如何为每个客户端存储令牌,并在向Rest API发送请求时使用它。 这是我的安全配置: 那么,可以使用JWT令牌

  • 在使用旧Spring的RestTemplate与OAuth授权背后的一些API集成时,我正在进行以下操作: 然后GET会说: 作为一种解决办法,我使用Mono.FromCallable(...)包装了旧的RestTempate。我还尝试使用WebClient做一些定制客户机,它接受令牌和授权用户,然后对RESTAPI做第二次调用。但它不是很好,因为令牌可以过期,我将不得不执行更新过程。OAuth中

  • 我尝试使用Spring Boot 2.0.0和Spring-security-oauth2 2.3.0创建一个具有自己登录页和资源的身份验证服务器。不幸的是,资源服务器的配置似乎不起作用。具有 重定向302总是localhost:8080/login有或没有令牌。 我的安全配置是 如何正确配置资源服务器? 下载示例项目 更新: 我用Spring Boot 1.5.10、Spring-securit