Ошибка безопасности Spring при запуске Jetty: недоступен компонент с именем org.springframework.web.filter.DelegatingFilterProxy-1b68ddbd ' - PullRequest
0 голосов
/ 04 мая 2018

Я знаю, что об этом уже спрашивали, но ни один из предложенных ответов для меня не работает. При запуске Jetty с Spring Security я получаю эту ошибку:

No bean named 'org.springframework.web.filter.DelegatingFilterProxy-1b68ddbd' available

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:component-scan base-package="de.pack.webservice" />
    <context:annotation-config />
[...]

    <jpa:repositories base-package="de.pack.webservice.security"
        entity-manager-factory-ref="customEntityManagerFactory" />

</beans>

SecurityConfig.java

package de.pack.webservice.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.dao.ReflectionSaltSource;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;

import de.pack.webservice.security.StandardPasswordEncoder;

@EnableWebSecurity
@Configuration
@Import(PrePostEnabledConfig.class)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userService;

    @Autowired
    private StandardPasswordEncoder encoder;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userService);
        authProvider.setPasswordEncoder(encoder);
        ReflectionSaltSource saltSource = new ReflectionSaltSource();
        saltSource.setUserPropertyToUse("salt");
        authProvider.setSaltSource(saltSource);
        auth.authenticationProvider(authProvider);
        System.out.println("in gobal");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        System.out.println("in configure");
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable().authorizeRequests().anyRequest().authenticated()
                .and().httpBasic();
    }
}

А для Dev мы не используем Web.xml, а устанавливаем параметры при запуске сервера. Вот соответствующая часть с определением фильтра:

        ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
        contextHandler.setContextPath("/");
        contextHandler.setInitParameter("contextConfigLocation", "classpath*:**applicationContext.xml");
        contextHandler.addEventListener(new ResteasyBootstrap());
        contextHandler.addEventListener(new SpringContextLoaderListener());
        ServletHolder holder = new ServletHolder(new HttpServletDispatcher());
        holder.setInitParameter("javax.ws.rs.Application", WebApplication.class.getName());
        contextHandler.addServlet(holder, "/*");

        EnumSet<DispatcherType> x = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
        contextHandler.addFilter(DelegatingFilterProxy.class, "/*", x);

        server.setHandler(contextHandler);

1 Ответ

0 голосов
/ 04 мая 2018

Настройте DelegatingFilterProxy, как показано ниже:

context.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")), "/*", x);

На самом деле Spring Security ищет компонент с именем springSecurityFilterChain (по умолчанию), который указывает на org.springframework.web.filter.DelegatingFilterProxy

...