【问题标题】:Spring security Class not found DefaultLogoutPageGeneratingFilter找不到 Spring 安全类 DefaultLogoutPageGeneratingFilter
【发布时间】:2018-08-24 08:02:10
【问题描述】:

我正在尝试将身份验证功能添加到我的项目中,但我遇到了这个问题:错误创建名称为“springSecurityFilterChain”的 bean 原因:java.lang.NoClassDefFoundError: org/springframework/security/web/authentication/ui/DefaultLogoutPageGeneratingFilter


  @Service
    public class MyUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {

        private static final Logger LOGGER = LoggerFactory.getLogger(MyUserDetailsAuthenticationProvider.class);
        private final Map<String, String> usersAndPasswords = new HashMap<>();


        @PostConstruct
        public void initUsers() {
            // this will add a user and encode its password to md5
            // Spring StandardPasswordEncoder will add salt by default
            usersAndPasswords.put("admin", "admin");
            usersAndPasswords.put("hamou", "amroun");
        }

        @Override
        public void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication)
            throws AuthenticationException {
            // Do nothing
        }

        @Override
        public UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

            LOGGER.debug("Authentication = {}", authentication);

            if (!usersAndPasswords.containsKey(username)) {
                throw new UsernameNotFoundException("User name not found : " + username);
            }

            String password = authentication.getCredentials().toString();
            String encodedPassword = usersAndPasswords.get(username);
            if (!password.equals(encodedPassword)) {
                throw new BadCredentialsException("Wrong password");
            }
            if (username.equals("hamou")) {
                return new User(username, "", createAuthorities("ROLE_USER"));
            } else {
                return new User(username, "", createAuthorities("ROLE_ADMIN"));
            }
        }

        public UserDetails whoMe(String username) {
            if (!usersAndPasswords.containsKey(username)) {
                throw new UsernameNotFoundException("User name not found : " + username);
            } else {

                if (username.equals("hamou")) {
                    return new User(username, "", createAuthorities("ROLE_USER"));
                } else {
                    return new User(username, "", createAuthorities("ROLE_ADMIN"));
                }
            }

        }

        private Collection<? extends GrantedAuthority> createAuthorities(String... roles) {
            Collection<GrantedAuthority> authorities = new ArrayList<>();
            for (String role : roles) {
                authorities.add(new SimpleGrantedAuthority(role));
            }
            return authorities;
        }
    }

这是我的依赖 spring security 和 spring-boot parent

<!-- dependency for security -->
<dependency>
    <!-- Starter for using Spring Security -->
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <!-- Make method based security testing easier -->
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <!-- JSON Web Token Support -->
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.7.0</version>
</dependency>
        <!-- dependency for security -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
</parent>

【问题讨论】:

  • org.springframework.bootspring-boot-starter-parent2.0.3.RELEASE
  • 我会说,你有一个依赖问题,因此你必须发布你的 POM 的依赖部分。使用edit 链接为您的问题添加信息。不要在 cmets 中这样做。
  • 感谢您的回答 :) 您的意思是版本问题?
  • 是的,Spring Security 的版本有些问题。您使用 BOM 吗?
  • 是的,我分享了它,实际上我使用的是 2.0.3 版本的 spring-boot

标签: java spring-boot authentication spring-security


【解决方案1】:

DefaultLogoutPageGeneratingFilter 类已在 Spring Security 版本中引入 5.1.x 行。所以这主要是依赖问题,检查版本是否正确或只是将以下依赖添加到您的 pom 文件中-

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-02
    • 2015-10-09
    • 2012-09-02
    • 2017-05-15
    • 1970-01-01
    • 2021-12-24
    • 2018-01-27
    • 2015-01-15
    相关资源
    最近更新 更多