扩展WebSecurityConfigurerAdapter无法解决

时间:2018-07-03 12:04:15

标签: spring-boot

我正在使用SpringBoot,我需要使用WebSecurityConfigurerAdapter扩展才能使用“ CORS”。

但是当我输入“ extends WebSecurityConfigurerAdapter”时,出现错误

Description Resource    Path    Location    Type
The type org.springframework.security.authentication.AuthenticationManager cannot be resolved. It is indirectly referenced from required .class files   ServiciosConfig.java    /tutorial/src/main/java/com/service/configure   line 1  Java Problem

我更新了pom.xml:

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

使用版本:

<version>1.5.14.RELEASE</version>

代码:

package com.service.configure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpMethod;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;


@Configuration
@EnableWebSecurity
@ComponentScan(basePackages = "com.service.services")
@PropertySource("file:${ruta_properties}")
public class ServiciosConfig  extends WebSecurityConfigurerAdapter {

    @Value("${usrRest}")
    private String usrRest; 

    @Value("${passRest}")
    private String passRest; 


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http.authorizeRequests().antMatchers("/").permitAll().anyRequest().authenticated().and().httpBasic().and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    }

    // To allow Pre-flight [OPTIONS] request from browser
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(HttpMethod.OPTIONS);
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser(usrRest).password(passRest).roles("USER");

    }

}

错误从package com.service.configure;的{​​{1}}开始,当我删除扩展时,p不会出错...但是我不能使用package这么麻烦...

编辑pom.xml:

WebSecurityConfigurerAdapter

3 个答案:

答案 0 :(得分:1)

更改您的依赖关系以使用

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>

这将解决问题

答案 1 :(得分:0)

最后我找到了解决方法

添加pom.xml

 <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>5.0.0.RELEASE</version>
 </dependency>

但是当我运行程序时,我得到其他错误...

PasswordEncoder mapped for the id “null”

答案 2 :(得分:0)

我有同样的问题。 我删除了.m2文件夹中的仓库,并使用以下命令在cmd中重建了应用程序: mvn全新安装-U 它对我有用。