【问题标题】:Spring ouath2Authserver oauth/token returns internal server Error for grant client_credentialsSpring ouath2Authserver oauth/token 为授予 client_credentials 返回内部服务器错误
【发布时间】:2021-01-11 12:45:02
【问题描述】:

我正在尝试使用密码和 client_credentials 授权来实现授权服务器

@Configuration
@EnableAuthorizationServer
public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {



    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    public PasswordEncoder passwordEncoder;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private TokenStore jwtTokenStore;

    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    @Autowired
    private TokenEnhancer jwtTokenEnhancer;

    @Bean
    public TokenEnhancer jwtTokenEnhancer(){
        return new JWTokenEnhancer();
    }

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        enhancerChain.setTokenEnhancers(Arrays.asList(jwtTokenEnhancer(), jwtAccessTokenConverter));

        endpoints
                .authenticationManager(authenticationManager)
                .tokenStore(jwtTokenStore)
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(enhancerChain);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        JdbcClientDetailsServiceBuilder jcsb = clients.jdbc(dataSource);
        jcsb.passwordEncoder(passwordEncoder);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
        oauthServer.passwordEncoder(passwordEncoder)
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("permitAll()");
    }
}

网页配置文件

Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource(name = "userService")
    private UserDetailsService userDetailsService;

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Bean
    public BCryptPasswordEncoder encoder() {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(encoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .csrf().disable()
            .anonymous().disable()
            .authorizeRequests()
            .antMatchers("/api-docs/**").permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        // Allow eureka client to be accessed without authentication
        web.ignoring().antMatchers("/*/")//
                .antMatchers("/eureka/**")//
                .antMatchers(HttpMethod.OPTIONS, "/**"); // Request type options should be allowed.
    }


}


@Configuration
public class JwtTokenConfig {

    @Bean
    public TokenStore jwtTokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
        accessTokenConverter.setSigningKey("dev");
        return accessTokenConverter;
    }

}

我已将客户端详细信息配置为从数据库中获取 -

当我尝试根据密码授权获取访问令牌时,我能够获取访问令牌

但是当我尝试根据 grnat_type 客户端凭据获取访问令牌时 - 我收到内部服务器错误。

请帮助检查我的实现有什么问题。

在此处输入图片描述

【问题讨论】:

    标签: spring spring-boot spring-security spring-security-oauth2 spring-boot-admin


    【解决方案1】:

    发现问题。

    public class JWTokenEnhancer implements TokenEnhancer{
    
        @Override
        public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
            Map<String, Object> info = new HashMap<>();
            info.put("user-info", "user additional information...");
        //    User user = (User) authentication.getPrincipal();
        //    info.put("isAdmin", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()).contains("ROLE_ADMIN"));
    
            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
            return accessToken;
        }
    

    注释行导致问题,因为在 client_credentials 的情况下没有用户

    【讨论】:

      【解决方案2】:

      在您的班级OAuthConfiguration 中,检查configure(ClientDetailsServiceConfigurer clients) 方法中存在的客户端配置。 JDBC 客户端详细信息服务似乎无法找到任何客户端详细信息。

      JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
      jdbcClientDetailsService.listClientDetails(); // This probably would be empty.
      

      如果是这样,配置 JDBC 客户端详细信息服务如下:

      clients.jdbc(dataSource).withClient(CLIEN_ID)
                 .secret(encoder.encode(CLIENT_SECRET))
                 .authorizedGrantTypes("password", "refresh_token", "client_credentials")
                 .scopes("read", "write")
                 .resourceIds(RESOURCE_ID).and().build();
                 
      

      【讨论】:

        猜你喜欢
        • 2014-03-25
        • 2019-02-11
        • 2019-05-18
        • 2017-07-28
        • 2021-12-02
        • 2016-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多