2014-09-02 110 views
5

我为Spring-Boot创建了一个Spring Security配置类。我的登录页面有资源css,js和ico文件。由于安全原因资源被拒绝,并且每次都重定向到登录页面。为什么EnableWebMVCSecurity不添加Classpath资源位置。在第二个片段中更改代码后,添加I Classpath资源位置。不明白我缺少的第一个代码片段中的资源。使用Spring引导的安全配置


@Configuration 

/* 
* Enable Spring Security’s web security support and provide the Spring MVC integration 
* It also extends WebSecurityConfigurerAdapter and overrides a couple of its methods to set some specifics of the web security configuration. 
*/ 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

/** 
* The configure(HttpSecurity) method defines with URL paths should be 
    * secured and which should not. 
    */ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
     .authorizeRequests() 
      .anyRequest().authenticated(); 

//  There is a custom "/login" page specified by loginPage(), and everyone 
//  is allowed to view it.  
     http 
      .formLogin() 
       .loginPage("/login.html") 
       .permitAll() 
       .and() 
      .logout() 
       .permitAll().logoutSuccessUrl("/login.html"); 
    } 

    @Configuration 
    protected static class AuthenticationConfiguration extends 
      GlobalAuthenticationConfigurerAdapter { 
     @Override 
     public void init(AuthenticationManagerBuilder auth) throws Exception { 
//   As for the configure(AuthenticationManagerBuilder) method, it sets up 
//   an in-memory user store with a single user. That user is given a 
//   username of "user", a password of "password", and a role of "USER". 
      auth 
        .inMemoryAuthentication() 
        .withUser("[email protected]").password("password").roles("USER"); 
     } 
    } 

我得到这个工作,通过改变代码


@Configuration 
/* 
* Enable Spring Security’s web security support and provide the Spring MVC integration 
* It also extends WebSecurityConfigurerAdapter and overrides a couple of its methods to set some specifics of the web security configuration. 
*/ 
public class WebSecurityConfig{ 

    @Bean 
    public ApplicationSecurity applicationSecurity() { 
     return new ApplicationSecurity(); 
    } 

    @Bean 
    public AuthenticationSecurity authenticationSecurity() { 
     return new AuthenticationSecurity(); 
    } 

    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter { 
     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
      .authorizeRequests() 
       .anyRequest().authenticated(); 
      http 
       .formLogin() 
        .loginPage("/login.html") 
        .permitAll() 
        .and() 
       .logout() 
        .permitAll().logoutSuccessUrl("/login.html"); 

     } 
    } 

    @Order(Ordered.HIGHEST_PRECEDENCE + 10) 
    protected static class AuthenticationSecurity extends 
      GlobalAuthenticationConfigurerAdapter { 
     @Override 
     public void init(AuthenticationManagerBuilder auth) throws Exception { 
      auth 
      .inMemoryAuthentication() 
      .withUser("[email protected]").password("password").roles("USER"); 

     } 
    } 
} 

改变我注意到,忽略路径添加到过滤器的代码后,我看到在日志中的以下内容:

 
[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain  : Creating filter chain: Ant [pattern='/css/**'], [] 
[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain  : Creating filter chain: Ant [pattern='/js/**'], [] 
[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain  : Creating filter chain: Ant [pattern='/images/**'], [] 
[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain  : Creating filter chain: Ant [pattern='/**/favicon.ico'], [] 
[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain  : Creating filter chain: [email protected]1, [org.springframework.secu[email protected]4e3e0069, org.spring[email protected]3d2dd0cf, [email protected]b02, [email protected], org.[email protected]267237ef, org.springframework.s[email protected]129495ef, org.springframework.[email protected]7db0a467, org.springfram[email protected]764d1dbd, org.sp[email protected]25a5268d, org.springframework.[email protected]15c01d0c, org.springfram[email protected]37818a3b, o[email protected]3fe57e49, org[email protected]4278af59, org.springfr[email protected]424bef91] 

回答

6

根据docs,您已使用@EnableWebSecurity禁用了第一个示例中的弹簧引导自动配置,因此您必须明确忽略所有的sta手动抽取资源。在第二个示例中,您只需提供一个WebSecurityConfigurer,这是在默认自动配置之上添加的。

+0

感谢指针文档。我使用了与EnableWebSecurity不同的'EnableWebMVCSecurity'。 – randominstanceOfLivingThing 2014-09-03 19:15:56

+0

它是一样的(从某种意义上说,它是一个超集) - 一个用另一个注解。 – 2014-09-03 20:51:49

+0

@DaveSyer,你能看看我的问题吗? https://stackoverflow.com/questions/46065063/spring-boot-basic-authentication – 2017-09-06 02:48:18

0

创建配置文件扩展WebSecurityConfigurerAdapter和注释类@EnableWebSecurity

您可以覆盖像configure(HttpSecurity http)方法来添加基本的安全像下面

@Configuration 
@EnableWebSecurity 
public class AppWebSecurityConfigurer extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception {  
     http 
      .csrf().disable() 
      .authorizeRequests() 
       .anyRequest().permitAll(); 
     } 
}