简体   繁体   中英

Trouble importing security libraries in Spring

I am trying to make use of the BCrypt library that spring provides. I've looked up many tutorials, but for some odd reason, almost none of them attempt to show what the build.gradle file should contain to access them. Currently, I have this:

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

In my actual java file, I have this:

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

However, I see these errors when I try to build:

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

Which dependencies am I missing and how can I find them when the tutorials leave them out?

You seem to be trying to be trying to import the package itself instead of a class from inside the package .

Instead of

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

you should have

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

Note that using an IDE will make this much easier, as it will autocomplete the class names for you and automatically add the appropriate import.

All of this said, the best way to find a dependency for a class you can't seem to find is to search the Central Repository . You can search by simple class name or use fc: to search by full class name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM