2014-05-11 61 views
4

在我的spring應用程序中,我使用Spring 4.0.4和Spring Security 3.2.3。我直接從sprin網站的教程中複製了這段代碼,但是我正在編譯問題,因爲方法registerAuthentication不能被WebSecurityConfigurerAdapter類覆蓋,而HttpSecurity類沒有方法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(); 
    } 
} 

回答

0

我有相同的代碼,它適用於我。你有沒有檢查過你的classpath中有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> 
0

要獲得教程的該部分與gradle這個運行,它使用

compile 'org.springframework.security:spring-security-web:3.2.0.M2' 
compile 'org.springframework.security:spring-security-core:3.2.0.M2' 
compile 'org.springframework.security:spring-security-config:3.2.0.M2' 

我不知道以後的版本會做。

此外,請注意,.anyRequest().anonymous()行將鎖定除訂單和結帳之外的所有頁面,因爲您不再是匿名的。只要刪除該行,使其按照教程期望的方式工作即可。

1

現在它.authorizeRequests()

0

試試這個

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