使用JDBC验证时,在Oauth2 Spring Boot中获取“未经授权的完全验证才能访问此资源”

时间:2020-05-02 12:59:02

标签: java spring-boot spring-security spring-data-jpa spring-security-oauth2

我在Spring Boot中使用Oauth2,并且正在使用JDBC令牌存储来存储JWT令牌。这是我的AuthorizationServerConfig

    @Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    static final String CLIENT_ID = "my-client";
    static final String CLIENT_SECRET = "my-client-secret";
    static final String GRANT_TYPE_PASSWORD = "password";
    static final String AUTHORIZATION_CODE = "authorization_code";
    static final String REFRESH_TOKEN = "refresh_token";
    static final String IMPLICIT = "implicit";
    static final String SCOPE_READ = "read";
    static final String SCOPE_WRITE = "write";
    static final String TRUST = "trust";
    private static final String RESOURCE_ID = "resource_id";
    static final int ACCESS_TOKEN_VALIDITY_SECONDS = 864000; 
    static final int FREFRESH_TOKEN_VALIDITY_SECONDS = 2592000;

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private DataSource dataSource;
    @Resource(name = "UserService")
    UserDetailsService userDetailsService;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() throws Exception {
        System.out.println("accessTokenConverter " + dataSource.getConnection().getSchema());
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey("as466gf");


        return converter;
    }

    @Bean
    public JdbcTokenStore tokenStore() throws Exception {
        System.out.println("tokenstore");
        return new JdbcTokenStore(dataSource);
    }

    @Bean
    public ApprovalStore approvalStore() throws Exception {
        TokenApprovalStore tokenApprovalStore = new TokenApprovalStore();
        tokenApprovalStore.setTokenStore(tokenStore());

        return tokenApprovalStore;
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
        System.out.println("configure");

        JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);

        if (!jdbcClientDetailsService.listClientDetails().isEmpty()) {
            jdbcClientDetailsService.removeClientDetails(CLIENT_ID);
        }

        configurer
                .jdbc(dataSource)
                .withClient(CLIENT_ID)
                .secret(bCryptPasswordEncoder.encode(CLIENT_SECRET))
                .authorizedGrantTypes(GRANT_TYPE_PASSWORD, AUTHORIZATION_CODE, REFRESH_TOKEN, IMPLICIT)
                .scopes(SCOPE_READ, SCOPE_WRITE, TRUST)
                .accessTokenValiditySeconds(ACCESS_TOKEN_VALIDITY_SECONDS).
                refreshTokenValiditySeconds(FREFRESH_TOKEN_VALIDITY_SECONDS)
                .and()
                .build()
        ;
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        System.out.println("configure below");
        endpoints
                .userDetailsService(userDetailsService)
                .pathMapping("/oauth/token", "/api/v1/oauth/token")
                .tokenStore(tokenStore())
                .authenticationManager(this.authenticationManager)
        ;
    }

    @Bean
    @Primary
    public DefaultTokenServices tokenServices() throws Exception {
        System.out.println("defaulttokenservices");
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);

        return defaultTokenServices;
    }
}

oauth_client与oauth_client_details和其他所有信息一起保存在表名client_id = my-client上的数据库中。

因此,每当我尝试在邮递员中使用BASE_URL/api/v1/oauth/token的{​​{1}}和userid以及另一个secret,{{1} }和Basic-Auth出现此错误

username

这是我的password

grant_type=password

这是我的{ "error": "unauthorized", "error_description": "Full authentication is required to access this resource" }

ResourceServerConfig

我不知道我在做什么错。任何帮助将不胜感激。谢谢

0 个答案:

没有答案