programing

예외 가져오기:'springSecurityFilterChain'이라는 이름의 빈이 정의되지 않았습니다.

codeshow 2023. 8. 26. 00:05
반응형

예외 가져오기:'springSecurityFilterChain'이라는 이름의 빈이 정의되지 않았습니다.

저는 참고 자료를 통해 스프링 보안을 배우고 있습니다.릴리스 3.1.2.풀어주다.에서 설명한 바와 같이 구성했습니다.security:http꼬리표를 붙입니다.

security-security.xml

<security:http auto-config="true">
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
    </security:http>

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:*-context.xml</param-value>
  </context-param>

  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>security</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>security</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

security-servlet.xml

<context:component-scan base-package="com.pokuri.security.mvc.controllers"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/page/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

하지만 애플리케이션을 시작할 때 이 예외가 발생합니다.보안 구성을 제거하면 스프링 웹 응용 프로그램이 제대로 작동합니다.저는 스택 오버플로에서 같은 종류의 질문을 겪었습니다.하지만 운이 없습니다.

문제의 원인은 웹 앱을 시작할 때 스프링 보안을 위한 xml 구성 파일이 로드되지 않았기 때문일 수 있습니다.

이 문제를 해결하려면 다음과 같이 web.xml에 모든 XML 구성 파일을 지정해야 합니다.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
</context-param>

클래스 경로에 구성 파일이 있는 경우(WEB-INF 폴더 또는 하위 폴더가 아님) 구성 파일 목록을 지정할 수 있습니다.

...
<param-value>
    classpath:applicationContext.xml,
    classpath:spitter-security.xml
</param-value>
...

또한 구성 파일을 로드할 특수 수신기를 추가해야 합니다.

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

Spring이 다음과 같이 요청했기 때문에 applicationContext.xml에 bean 정의를 추가했습니다.

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/>

web.xml을 추가합니다.

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml, /WEB-INF/spring-security.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

        <!-- filter declaration for Spring Security -->
<filter>
  <filter-name>springSecurityFilterChain</filter-name>
  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

도움이 될 경우를 대비하여 패키지 이름을 변경했지만 Eclipse는 자동으로 업데이트하지 않습니다.@ComponentScan경로도 변경해야 합니다.

@ComponentScan(basePackages = "com.package.spring")

Spring Security 5.2.1-SNAPshot을 사용할 때 다음과 같은 오류가 발생했습니다.<http/>요소가 보안 XML 구성에 있습니다.

샘플을 시도하던 중에 다음과 같은 구성이 있었습니다.

    <b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd">

    <user-service>
        <user name="user" password="{noop}password" authorities="ROLE_USER" />
    </user-service>
</b:beans>

추가하기 위해 변경해야 했습니다.<http/>아래와 같이

<b:beans xmlns="http://www.springframework.org/schema/security" xmlns:b="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/security https://www.springframework.org/schema/security/spring-security.xsd">

    <http />
    <user-service>
        <user name="user" password="{noop}password" authorities="ROLE_USER" />
    </user-service>
</b:beans>

xml 없이 봄을 배우는 사람들에게 이것은 도움이 될 수 있습니다.

xml 없이 봄을 배울 때도 같은 예외에 직면했습니다.

클래스 AbstractSecurityWebApplication을 확장하여 스프링 보안 필터를 등록했습니다.Initializer이지만 스프링 보안을 구성하지 않았습니다.스프링 보안을 구성하기 위해 아래 코드를 추가했으며 작동합니다.

@Configuration
@EnableWebSecurity
public class SpringSecurityConfigInitializer extends 
WebSecurityConfigurerAdapter {

@Override
protected void configure(AuthenticationManagerBuilder auth) throws 
Exception {
    UserBuilder users = User.withDefaultPasswordEncoder();
auth.inMemoryAuthentication().withUser(users.username("test").
password("test123").roles("EMPLOYEE"));
 }

 }

위는 제 경우를 위한 샘플일 뿐이지만 구성이 누락되어 예외가 발생했습니다.

언급URL : https://stackoverflow.com/questions/12123516/getting-exception-no-bean-named-springsecurityfilterchain-is-defined

반응형