gpt4 book ai didi

java - 未构建用于过滤 Spring Boot URL 的类

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

这是一个使用 Maven 的 Spring Boot Java 项目。如果我从 WebConfig 中删除 @Configuration 注释,应用程序会构建,但该类似乎会被忽略。如果我包含它,应用程序将失败并显示以下消息:

Error starting Tomcat context. Exception: java.lang.ClassCastException. Message:
org.springframework.boot.web.servlet.DispatcherType cannot be cast to javax.servlet.DispatcherType. Application run failed.

如何正确设置 Spring Boot 以使用过滤器?

这是主要的应用程序类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class GetJobDetailsApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(GetJobDetailsApplication.class);
}

public static void main(String[] args) {
SpringApplication.run(GetJobDetailsApplication.class, args);
}

}

这是 Controller :

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainRESTController {

// inject via application.properties
@Value("${welcome.message:test}")
private String message = "Hello World";

@RequestMapping("/")
public String welcome(Map<String, Object> model) {
model.put("message", this.message);
return "welcome";
}

}

这是我设置过滤器的 WebConfig:

import org.owasp.filters.ClickjackFilter;
import org.springframework.boot.web.servlet.DispatcherType;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import java.util.EnumSet;

@Configuration
public class WebConfig {

@Bean
public FilterRegistrationBean clickjackFilterRegistration() {

FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(clickjackFilter());
registration.addUrlPatterns("/");
registration.addInitParameter("paramName", "paramValue");
registration.setName("clickjackFilter");
registration.setOrder(1);
return registration;
}

@Bean(name = "clickjackFilter")
public ClickjackFilter clickjackFilter() {
return new ClickjackFilter();
}

@Bean
public FilterRegistrationBean shallowEtagHeaderFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new ShallowEtagHeaderFilter());
registration.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
registration.addUrlPatterns("/");
return registration;
}
}

这是 clickjackFilter 类:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class ClickjackFilter implements Filter {

private String mode = "DENY";

/**
* Add X-FRAME-OPTIONS response header to tell IE8 (and any other browsers who
* decide to implement) not to display this content in a frame. For details, please
* refer to http://blogs.msdn.com/sdl/archive/2009/02/05/clickjacking-defense-in-ie8.aspx.
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse res = (HttpServletResponse) response;
res.addHeader("X-FRAME-OPTIONS", mode);
chain.doFilter(request, response);
}

public void destroy() {
}

public void init(FilterConfig filterConfig) {
String configMode = filterConfig.getInitParameter("mode");
if (configMode != null) {
mode = configMode;
}
}
}

pom.xml 文件中的依赖项:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.9</version>
</dependency>

<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
<version>${apachetiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
<version>${apachetiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-extras</artifactId>
<version>${apachetiles.version}</version>
</dependency>

<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-api</artifactId>
<version>${apachetiles.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-servlet</artifactId>
<version>${apachetiles.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>

<!-- Tomcat embedded container-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

<!-- Need this to compile JSP,
tomcat-embed-jasper version is not working -->
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>

<!-- Optional, test for static content, bootstrap CSS-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>

最佳答案

仔细查看您的堆栈跟踪,您可以尝试将 DispatcherType 设置为

 java.lang.Object
java.lang.Enum<DispatcherType>
javax.servlet.DispatcherType

因为 FilterRegistrationBean 需要 javax.servlet.DispatcherType 类型的参数作为集合 setDispatcherTypes()

或者您可以使用以下注释直接注册过滤器 bean:

@Order(Ordered.LOWEST_PRECEDENCE -1)
@Component
public class ABCFilter implements Filter {
------
}

在 Spring Boot 中,通常您会配置如下过滤器:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Autowired
HandlerInterceptor customInjectedInterceptor;

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(...)
...
registry.addInterceptor(customInjectedInterceptor).addPathPatterns("/**");
}
}

或者如果您使用 Spring 5x 那么:

@Configuration
public WebConfig implements WebMvcConfigurer {
// ...
}

通过这样做,我们本质上是在自定义 spring boot 自动配置 bean,以便 springboot 仍然可以自动配置所有其他内容。如果您正在使用 springboot,请考虑删除 @EnableWebMvc 并使用 springboot 的自动配置。

关于java - 未构建用于过滤 Spring Boot URL 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56159589/

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