reactjs - 使用 /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();
}
解决方案
推荐阅读
- android - 分页库和房间:Recyclerview 滚动时内容未加载
- reactjs - react-slick - 元素类型无效:需要一个字符串
- babeljs - 我如何告诉 Babel 不要关心它不理解的东西?
- sql - 如何在 Oracle 中使用字符串选择表?
- python - 在 Python 中,如何创建别名?我想将“elif”重命名为“elseif”
- ios - 从重叠视图触发 UITapGestureRecognizers
- php - 如何从自定义路由发送来自 Laravel 5.7 的电子邮件验证电子邮件
- java - 什么是正确的正则表达式模式?
- c# - Newtonsoft.Json 在长树中的用法
- pine-script - 如何在较小的时间范围内计算较大时间范围内的指标值