【问题标题】:Get 401: Invalid Token After install okta-spring-boot-starter安装 okta-spring-boot-starter 后获取 401: Invalid Token
【发布时间】:2020-09-29 04:27:49
【问题描述】:

您好,我现在正在使用 Angluar + Spring Boot 构建 Web,我正在使用 okta Single-Page App 进行身份验证。在我的前端,我可以登录并重定向回来,当我调用后端api时,我还在标题中添加Authorization: Bearer {Token}

所以问题是当我安装 okta-spring-boot-starter(1.4.0) 时,我会得到错误:

 Bearer error="invalid_token", error_description="An error occurred while attempting to decode the Jwt: Invalid token", error_uri="https://tools.ietf.org/html/rfc6750#section-3.1"

我认为这可能是因为我没有设置 okta 配置。 然后我在 application.yml 文件中添加 okta 属性:

spring:
  profiles:
    active: "dev"
  main:
    banner-mode: "off"

okta.oauth2:
  client-id: 0oaebbeiaBCbFFtDU4x6
  issuer: https://dev-574831.okta.com
  audience: https://dev-574831.okta.com

并通过参考:https://github.com/okta/samples-java-spring/blob/master/resource-server/src/main/java/com/okta/spring/example/ResourceServerExampleApplication.java来使用 WebSecurityConfigurerAdapter


import com.okta.spring.boot.oauth.Okta;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class OktaOAuth2WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/userinfo").permitAll()
                .antMatchers("/health").permitAll()
                .anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt();

        http.cors();

        Okta.configureResourceServer401ResponseBody(http);

    }
}

我仍然遇到同样的错误。

所以我错过了什么吗?或者我的项目无法识别 okta 配置?

【问题讨论】:

    标签: java spring spring-boot spring-security okta


    【解决方案1】:

    试试这个:

    okta:
      oauth2:
        client-id: 0oaebbeiaBCbFFtDU4x6
        issuer: https://dev-574831.okta.com/oauth2/default
    

    【讨论】:

    • 其实我之前试过这个。但得到一个不同的错误,说“签名的 JWT 被拒绝:需要另一个算法,或者找不到匹配的密钥”。然后我用了```dev-574831.okta.com```
    • 一件事是我应该使用“Web APP”而不是“Single Page App”吗??
    • 解决的问题将前端(角度)和后端(弹簧启动)发行者都更改为dev-574831.okta.com/oauth2/default。所以这里的事情是,当我使用dev-574831.okta.com 作为发行者时,我从“组织授权服务器”而不是“默认授权服务器”获取令牌
    猜你喜欢
    • 2021-11-26
    • 2020-09-30
    • 2020-10-19
    • 2020-03-17
    • 2017-07-24
    • 2014-04-07
    • 2016-01-29
    • 1970-01-01
    • 2015-07-07
    相关资源
    最近更新 更多