【问题标题】:Security configuration with Spring-boot使用 Spring-boot 进行安全配置
【发布时间】:2014-10-27 07:32:21
【问题描述】:

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


@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("user@domain.com").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("user@domain.com").password("password").roles("USER");

        }
    }   
}

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

[ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : 创建过滤器链: Ant [pattern='/css/**'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : 创建过滤器链: Ant [pattern='/js/**'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain :创建过滤器链:Ant [pattern='/images/**'], [] [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain : 创建过滤器链: Ant [pattern='/**/favicon.ico'], [] [ost-startStop-1] ossweb.DefaultSecurityFilterChain:创建过滤器链:org.springframework.security.web.util.matcher.AnyRequestMatcher@1,[org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@4e3e0069 , org.springframework.security.web.context.SecurityContextPersistenceFilter@3d2dd0cf, org.springframework.security.web.header.HeaderWriterFilter@33fc3b02, org.springframework.security.web.csrf.CsrfFilter@9b7a3ac, org.springframework.security.web .authentication.logout.LogoutFilter@267237ef, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@129495ef, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@7db0a467, org.springframework.security.web.authentication.www .BasicAuthenticationFilter@764d1dbd, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@25a5268d, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@15c01d0c, org.spring framework.security.web.authentication.AnonymousAuthenticationFilter@37818a3b,org.springframework.security.web.session.SessionManagementFilter@3fe57e49,org.springframework.security.web.access.ExceptionTranslationFilter@4278af59,org.springframework.security.web.access。拦截.FilterSecurityInterceptor@424bef91]

【问题讨论】:

    标签: java spring-mvc spring-security spring-boot


    【解决方案1】:

    根据docs,您在第一个示例中使用@EnableWebSecurity 禁用了spring boot 自动配置,因此您必须手动显式忽略所有静态资源。在第二个示例中,您只需提供一个 WebSecurityConfigurer,它是在默认自动配置之上添加的。

    【讨论】:

    • 感谢您指向文档。我使用了EnableWebMVCSecurity,它不同于EnableWebSecurity
    • 它是一样的(从某种意义上说,它是一个超集)——一个被另一个注释。
    • @DaveSyer,你能看看我的问题吗? stackoverflow.com/questions/46065063/…
    【解决方案2】:

    创建一个扩展 WebSecurityConfigurerAdapterConfiguration 文件并用 @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();
            }
    }
    

    【讨论】:

      【解决方案3】:

      添加以下方法以在安全配置中绕过 css 和 js 的安全性 -

       @Override
          public void configure(WebSecurity web) throws Exception {
             web.ignoring().antMatchers("/css/** **","/js/** **");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        • 2015-08-16
        • 2019-08-25
        • 2016-02-12
        • 2015-02-22
        • 2021-02-21
        相关资源
        最近更新 更多