Oauth2实现:收到错误{“ error”:“未经授权”,“ error_description”:“需要完全验证才能访问此资源”}

时间:2020-07-18 13:04:33

标签: spring-boot spring-security oauth-2.0

我正在尝试获取OAuth令牌。我已经实现了授权,资源和安全性配置。 安全配置已通过,但是在获取OAuth令牌时遇到了一些问题

我遇到错误:

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

我的网址是

卷曲客户端:secret @ localhost:8080 / oauth / token -d grant_type = authorization_code -d username = abhi -d password = password

我想ResourceServer配置存在一些问题

能否请您帮助解决此问题?在下面附加我的代码。

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

private final AuthenticationManager authenticationManager;
private final PasswordEncoder passwordEncoder;
//private final UserDetailsService userDetailsService;

@Value("${jwt.clientId:client}")
private String clientId;

@Value("${jwt.client-secret:secret}")
private String clientSecret;

@Value("${jwt.signing-key:123}")
private String jwtSigningKey;

@Value("${jwt.accessTokenValidititySeconds:43200}") // 12 hours
private int accessTokenValiditySeconds;

@Value("${jwt.authorizedGrantTypes:password,authorization_code,refresh_token}")
private String[] authorizedGrantTypes;

@Value("${jwt.refreshTokenValiditySeconds:2592000}") // 30 days
private int refreshTokenValiditySeconds;

public AuthorizationServerConfig(AuthenticationManager authenticationManager, PasswordEncoder passwordEncoder) {
    this.authenticationManager = authenticationManager;
    this.passwordEncoder = passwordEncoder;
    //this.userDetailsService = userDetailsService;
}

@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory()
            .withClient(clientId)
            .secret(clientSecret)
            //.secret(passwordEncoder.encode(clientSecret))
            .accessTokenValiditySeconds(accessTokenValiditySeconds)
            .refreshTokenValiditySeconds(refreshTokenValiditySeconds)
            .authorizedGrantTypes(authorizedGrantTypes)
            .scopes("read", "write")
            .resourceIds("api");
}


@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
    endpoints
            .accessTokenConverter(accessTokenConverter())
            //.userDetailsService(userDetailsService)
            .authenticationManager(authenticationManager);
}

@Bean
JwtAccessTokenConverter accessTokenConverter() {
    JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
    return converter;
}
}



@Configuration
@EnableResourceServer
public class ResourceServer extends ResourceServerConfigurerAdapter {

/*@Override
public void configure(HttpSecurity httpSecurity) throws  Exception{
    httpSecurity.requestMatchers().antMatchers("/user/getusers/**")
            .and().authorizeRequests().anyRequest().access("#oauth2.hasScope('read')");
}*/

@Override
public void configure(ResourceServerSecurityConfigurer serverSecurityConfigurer) {
    serverSecurityConfigurer.resourceId("api");
}

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .antMatcher("/**")
            .authorizeRequests()
            .antMatchers("/user**").permitAll()
            .antMatchers("/user/**").permitAll()
            .antMatchers("/admin**").hasAuthority("ADMIN")
            .antMatchers("/api/**").authenticated()
            .and().authorizeRequests().anyRequest().access("#oauth2.hasScope('read')");
            //.anyRequest().authenticated();
            /*.and()
            .exceptionHandling().authenticationEntryPoint(customAuthenticationEntryPoint).accessDeniedHandler(new CustomAccessDeniedHandler());*/
}
}

0 个答案:

没有答案