gpt4 book ai didi

java - 启动 Jetty 时出现 Spring Security 错误 : No bean named 'org.springframework.web.filter.DelegatingFilterProxy-1b68ddbd' available

转载 作者:行者123 更新时间:2023-12-02 11:17:25 25 4
gpt4 key购买 nike

我知道以前曾有人问过这个问题,但建议的答案都不适合我。使用 Spring Security 启动 Jetty 时出现此错误:

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);

最佳答案

配置 DelegatingFilterProxy 如下:

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

实际上,Spring Security 会查找名为 springSecurityFilterChain 的 bean(默认情况下),它指向 org.springframework.web.filter.DelegatingFilterProxy

关于java - 启动 Jetty 时出现 Spring Security 错误 : No bean named 'org.springframework.web.filter.DelegatingFilterProxy-1b68ddbd' available,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50174819/

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