spring安全配置类错误

use*_*694 5 java spring spring-mvc spring-security

在我的 Spring 应用程序中,我使用 Spring 4.0.4 和 Spring Security 3.2.3。我直接从 sprin 网站上的教程复制了此代码,但编译时遇到问题,因为registerAuthentication无法从 WebSecurityConfigurerAdapter类中覆盖该方法,并且该类HttpSecurity没有 methode authorizeUrls。我缺少一个罐子吗?或者说是版本问题?

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("letsnosh").password("noshing").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeUrls()
                .antMatchers("/order/**").hasRole("USER")
                .antMatchers("/checkout").hasRole("USER")
                .anyRequest().anonymous()
                .and()
                //This will generate a login form if none is supplied.
                .formLogin();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

我有相同的代码,它对我来说工作得很好。您是否检查过您的类路径中是否有 spring-security-web 和 spring-security-config ?尝试将它们添加到您的依赖项部分(如果您使用的是 Maven)。

<!-- Spring security -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)