首页 > 解决方案 > 使用 /authorize 和 /oauth/token 端点进行身份验证以访问自定义 Api 后获取访问令牌的正确方法是什么

问题描述

我有一个 Spring Boot Java 后端和一个 ReactJs 前端。我的登录页面有一个欢迎按钮,其中包含使用授权代码流的 Auth0 登录页面的链接。url 是 /authorize?client_id&client_secret 等。登录后,我得到一个授权码,用于 POST 到 /oauth/token 端点以获取 ID_TOKEN 和 ACCESS_TOKEN。我的 Java 后端上有需要访问的自定义 API,但是当我添加使用 ACCESS_TOKEN 作为授权标头时,我得到了 403 禁止。我肯定错过了什么?

请帮助我,我已经为此奋斗了一段时间。如果我需要提供更多信息,请告诉我。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
        protected void configure(final HttpSecurity http) throws Exception {
    
    http.sessionManagement()
                 .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                    .cors()
                    .and()
                    .exceptionHandling()
                    .and()
                    .anonymous()
                    .and()
                    .servletApi()
                    .and()
                    .headers()
                    .cacheControl()
                    .and()
                    .and()
                    .authorizeRequests()
                    .antMatchers("/app/cfg/ext/**").permitAll()
                    .antMatchers("/user").authenticated()
                    .and()
                    .authorizeRequests()
                    .antMatchers(LOGIN_URL)
                    .permitAll()
                    .and()
                    .addFilterBefore(
                            new CorsFilter(configSource), SecurityContextPersistenceFilter.class)
                    .addFilterBefore(
                            new AuthTokenFilter(
                                    LOGIN_URL,
                                    new Auth0AccessTokenRequester(remoteEndpointService),
                                    successHandler),
                            SecurityContextPersistenceFilter.class);
    
    
            http.csrf()
                    .csrfTokenRepository(new CookieCsrfTokenRepository())
                    .and()
                    .addFilterAfter(
                            new CsrfTokenResponseHeaderBindingFilter(), SessionManagementFilter.class);
            http.csrf().disable();
    }

标签: reactjsspring-bootauth0

解决方案


推荐阅读