Heroku deployment error“required a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' that could not be found.”

ffdz8vbo  于 9个月前  发布在  Spring
关注(0)|答案(5)|浏览(296)

我试图将我的springboot应用程序部署到heroku,但我得到一个错误,它找不到JwtDecoder bean。我试着用谷歌搜索了一下,但找不到任何有用的东西。一切都在本地工作正常,只是部署到heroku时不行。
这是我的Heroku日志-尾巴:

2021-02-27T20:18:45.134160+00:00 app[web.1]: ***************************
2021-02-27T20:18:45.134160+00:00 app[web.1]: APPLICATION FAILED TO START
2021-02-27T20:18:45.134160+00:00 app[web.1]: ***************************
2021-02-27T20:18:45.134161+00:00 app[web.1]:
2021-02-27T20:18:45.134161+00:00 app[web.1]: Description:
2021-02-27T20:18:45.134161+00:00 app[web.1]:
2021-02-27T20:18:45.134179+00:00 app[web.1]: Method springSecurityFilterChain in
org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration 
required a bean of type 'org.springframework.security.oauth2.jwt.JwtDecoder' that 
could not be found.
2021-02-27T20:18:45.134179+00:00 app[web.1]: 
2021-02-27T20:18:45.134179+00:00 app[web.1]:
2021-02-27T20:18:45.134180+00:00 app[web.1]: Action:
2021-02-27T20:18:45.134180+00:00 app[web.1]:
2021-02-27T20:18:45.134181+00:00 app[web.1]: Consider defining a bean of type 
'org.springframework.security.oauth2.jwt.JwtDecoder' in your configuration.

WebSecurityConfig:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable().cors().configurationSource(corsConfigurationSource())
                .and()
                .authorizeRequests()
                .antMatchers("/*")
                .authenticated()
                .and()
                .oauth2ResourceServer()
                .jwt();
    }

我不知道还能写些什么...请随意评论我应该添加的其他内容。或者,回购位于https://github.com/AndreTheTallGuy/Kelp2
提前感谢您提供的任何帮助!

zzwlnbp8

zzwlnbp81#

尝试在application.yml中指定jwk-set-uri配置:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: https://example.com/.well-known/jwks.json

application.properties:

spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://example.com/.well-known/jwks.json
zbsbpyhn

zbsbpyhn2#

它找不到org.springframework.security.oauth2.jwt.JwtDecoder,因为您将该依赖项列为test scope

<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

更改scope或删除该条目。默认条目为compile

cgvd09ve

cgvd09ve3#

左心室,右心室

@SpringBootTest(
        properties = {
                "spring.security.oauth2.resourceserver.jwt.jwk-set-uri = http://<host>:<port>/auth/realms/your-realm/protocol/openid-connect/certs"}
)

keycloak的例子

rslzwgfq

rslzwgfq4#

像这样添加JwtDecoder @Bean,并检查它是否工作

@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public JwtDecoder jwtDecoder() {
        return JwtDecoders.fromIssuerLocation("your-issuer-uri");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        http.csrf().disable().cors().configurationSource(corsConfigurationSource())
                .and()
                .authorizeRequests()
                .antMatchers("/*")
                .authenticated()
                .and()
                .oauth2ResourceServer()
                .jwt();
    }
goucqfw6

goucqfw65#

在主应用程序类中使用@SpringBootApplication(scanBasePackages = "org.springframework.security.oauth2.jwt")

相关问题