无法访问javax.servlet.Filter;未找到javax.servlet.Filter的类文件

u5i3ibmn  于 2023-03-16  发布在  Java
关注(0)|答案(7)|浏览(264)

我尝试按照本文所述实现OAuth2 AuthorizationServer,但一直收到以下错误。对于Spring安全配置:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@Order(1)
class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${user.oauth.user.username}")
    private String username;
    @Value("${user.oauth.user.password}")
    private String password;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatchers()
                .antMatchers("/login", "/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin().permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser(username)
                .password(passwordEncoder().encode(password))
                .roles("USER");
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

我一直收到这个错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project auth-service: Compilation failure
[ERROR] /home/mcs/auth-service/src/main/java/com/example/authservice/config/SecurityConfig.java:[14,1] cannot access javax.servlet.Filter
[ERROR] class file for javax.servlet.Filter not found

这是什么意思,哪里看起来是错的?
我start.spring.io使用Sping Boot 的最新M2版本从www.example.com生成的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://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>2.2.0.M2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>auth-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>auth-service</name>
    <description>Authorization service</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>com.okta.spring</groupId>
            <artifactId>okta-spring-boot-starter</artifactId>
            <version>1.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>2.6.7</version>
        </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>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>
wlp8pajw

wlp8pajw1#

您分享的文章要求您添加pom.xml文件中没有的Web依赖项。添加下面的依赖项可以解决javax.servlet错误

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

如果检查spring-boot-starter-web pom文件,您将看到对javax.servlet-api- 3.1.0的依赖关系。

zsbz8rwp

zsbz8rwp2#

我遇到了同样的问题,但在添加依赖项spring-boot-starter-web后得到了修复

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

pu82cl6c3#

查看Spring所依赖的servlet API版本,然后在Gradle中包括:

compileOnly("javax.servlet:javax.servlet-api:X")

其中X是spring依赖的servlet-api版本。

jexiocij

jexiocij4#

您看到的错误消息(无法访问javax.servlet.filter)表明类路径有问题,并且缺少Servlet API。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

将Servlet API添加到类路径之后,应该能够成功编译代码。

yjghlzjz

yjghlzjz5#

检查依赖关系层次结构树。它是否包含javax servlet api。如果不包含,请从此处选择相关版本https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api并将其添加到pom.xml中

sqxo8psd

sqxo8psd6#

虽然这是一个不同的问题,但它具有相同的错误消息,因此,如果您到达此处并尝试使WebFlux工作,您会看到此错误

public class PreAuthorizeSecurityConfigAdapter extends WebSecurityConfigurerAdapter {
       ^
  class file for javax.servlet.Filter not found

可能是WebFlux不支持你的用例,我试着得到@PreAuthorize(hasPermission(...)),但是在谷歌上搜索了很久之后发现WebFlux不支持它,你应该像这样使用你自己的bean:

@PreAuthorize("@somePermissionEvaluatorBean.hasPermission(authentication, ...")
8mmmxcuj

8mmmxcuj7#

对于那些使用gradle只需添加,你是好去

implementation 'org.springframework.boot:spring-boot-starter-web'

相关问题