gpt4 book ai didi

Bean named 'springSecurityFilterChain' is expected to be of type 'jakarta.servlet.Filter' but was actually of type(名为“springSecurityFilterChain”的Bean应为“jakarta.servlet.Filter”类型,但实际为)

转载 作者:bug小助手 更新时间:2023-10-25 22:26:03 37 4
gpt4 key购买 nike



Using Spring Boot 3.0.7 and trying to keep things updated I ran into this error and can't figure out what it means.

在使用Spring Boot 3.0.7并试图保持更新时,我遇到了这个错误,不知道它是什么意思。


The dependencies loaded are Spring Security 6.0.9 and the problem exists using either Java 17 or 19 (Spring Boot seems to load Tomcat 10 for the spring-boot:run target and has a message about using Java 19.

加载的依赖项是Spring Security 6.0.9,问题存在于使用Java 17或19的情况下(Spring Boot似乎加载Tomcat 10作为Spring-Boot:Run目标,并且有一条关于使用Java 19的消息。


The application is an OAuth client. The application compiles with no errors and starts up, but you try to hit localhost:8080 and immediately get the error:

该应用程序是OAuth客户端。应用程序编译后没有出现任何错误并启动,但您尝试按下Localhost:8080并立即得到错误:



org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'springSecurityFilterChain' is expected to be of type 'jakarta.servlet.Filter' but was actually of type 'org.springframework.security.web.DefaultSecurityFilterChain'



Why would the bean need to be jakarta.servlet.Filter?

为什么Bean需要是jakarta.servlet.Filter?


pom.xml

Pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.7</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>oauth2-sso</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>oauth2-sso</name>
<description>OAUTH2</description>
<properties>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.19</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

SecurityFilterChain

SecurityFilterChain


package com.example.oauth2;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;

@Configuration
@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

/*
@Autowired
private AuthenticationSuccessHandler authenticationSuccessHandler;
*/
@Bean
SecurityFilterChain springSecurityFilterChain(HttpSecurity http) throws Exception {
http.oauth2Login()
//.successHandler(authenticationSuccessHandler);
;
return http.build();
}
}

更多回答

Also add your, application properties

还要添加、应用程序属性

@user63868 Your packaging is WAR, does it mean that you are deploying the WAR in a servlet container like Tomcat?

@user63868您的包装是WAR,这是否意味着您正在像Tomcat一样将WAR部署在Servlet容器中?

I believe the problem is that @EnableWebSecurity already defines a bean named 'springSecurityFilterChain' so redefining that exact bean name caused the problem. Renaming it to 'filterChain' seems to fix the issue.

我认为问题在于@EnableWebSecurity已经定义了一个名为‘springSecurityFilterChain’的Bean,因此重新定义该Bean名称导致了问题。将其重命名为‘filterChain’似乎可以解决这个问题。

优秀答案推荐

The annotation EnableWebSecurity in Spring Security 6 (at least 6.0.9 and 6.1.0) imports WebSecurityConfiguration which defines a bean of name springSecurityFilterChain.

Spring Security6(至少是6.0.9和6.1.0)中的注释EnableWebSecurity导入了WebSecurityConfiguration,它定义了一个名为springSecurityFilterChain的Bean。


WebSecurityConfiguration.class

WebSecurityConfiguration.class


@Bean(
name = {"springSecurityFilterChain"}
)
public Filter springSecurityFilterChain() throws Exception {
// ...

and other beans depend on it and that it has type Filter, so when you change the type of the bean, you're breaking their creation.

而其他Bean依赖于它,并且它具有类型过滤器,所以当您更改Bean的类型时,您就破坏了它们的创建。


Rename your bean using a different method name like securityFilterChain() or use a different bean name explicitly @Bean(name = {"securityFilterChain"}.

使用不同的方法名称重命名您的Bean,如securityFilterChain(),或者显式地使用不同的Bean名称@Bean(name={“securityFilterChain”}。


更多回答

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com