java - 在 Spring 中导入安全库时遇到问题

标签 java spring spring-security

我正在尝试使用spring提供的BCrypt库。我查阅了很多教程,但由于某些奇怪的原因,几乎没有一个教程试图显示 build.gradle 文件应包含哪些内容来访问它们。目前,我有这个:

dependencies {
    ....
    compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '5.0.8.RELEASE'
}

在我实际的 java 文件中,我有这个:

package project;

import org.springframework.security.config.annotation.web.configuration;
import org.springframework.security.crypto.password;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

但是,当我尝试构建时,我看到这些错误:

error: cannot find symbol import org.springframework.security.config.annotation.web.configuration;
                                                     ^
symbol:   class configuration
location: package org.springframework.security.config.annotation.web

error: package org.springframework.security.crypto does not exist 
import org.springframework.security.crypto.password;

error: cannot find symbol public class SecurityConfig extends WebSecurityConfigurerAdapter {
                                     ^
symbol: class WebSecurityConfigurerAdapter

error: cannot find symbol
    public PasswordEncoder passwordEncoder() {
           ^
symbol:   class PasswordEncoder
location: class SecurityConfig

我缺少哪些依赖项以及当教程遗漏它们时如何找到它们?

最佳答案

您似乎试图导入包本身而不是包内的类

而不是

import org.springframework.security.config.annotation.web.configuration;

你应该有

import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

请注意,使用 IDE 将使此过程变得更加容易,因为它会自动为您补全类名并自动添加适当的导入。

综上所述,查找您似乎找不到的类的依赖项的最佳方法是搜索 the Central Repository 。您可以通过简单的类名搜索或使用fc:search by full class name .

关于java - 在 Spring 中导入安全库时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396996/

相关文章:

java - HandlerThread 没有在后台运行?

java - Camel EIP 过滤重复项

java - 忽略 ExpectedDataSet (dbunit) 中的列

java - 为什么我无法在 hibernate 中删除两个相邻的对象?

java - spring security csrf禁用问题

java - 多模块项目中的spring配置

java - 用于在文本 Java 中搜索单词的最有效数据结构

java - 单链表冒泡排序

mysql - 一起使用比较和喜欢从表中获取结果

spring - j_spring_security_check 不可用。请帮助Spring3登录