2014-09-02 53 views
5

Spring-Boot için bir Spring Security yapılandırma sınıfı oluşturdum. Giriş sayfamın kaynakları css, js ve ico dosyaları var. Kaynaklar güvenlik nedenleriyle reddediliyor ve her defasında giriş sayfasına yönlendiriliyor. EnableWebMVCSecurity neden Classpath kaynak konumunu eklemez. Kodu ikinci snippet'te olduğu gibi değiştirdikten sonra, Classpath kaynak yeri eklenir. İlk kod snippet'indeki kaynaklar için neyi kaçırdığımı anlamıyorum.Yaylı önyükleme ile güvenlik yapılandırması


@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"); 
     } 
    } 

ben görmezden yolları filtreye eklenmiştir fark kodunu değiştirdikten sonra


@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"); 

     } 
    } 
} 

kodunu değiştirerek bu çalışma var ve ben günlüklerinde bkz:

 
[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.springframework.security.web.access.ExceptionTran[email protected], org.springfr[email protected]424bef91] 
Başına

cevap

6

docs Eğer @EnableWebSecurity kullanarak ilk örnekte yay çizme Otomatik yapılandırma devre dışı, bu yüzden açıkça tüm sta görmezden zorunda kalacak tik kaynakları elle. İkinci örnekte, varsayılan autoconfig'in üstüne ek bir WebSecurityConfigurer sağlamanız yeterlidir.

+0

teşekkür altında dokümantasyon. EnableWebSecurity’den farklı olan EnableWebMVCSecurity’i kullandım. – randominstanceOfLivingThing

+0

Aynı şey (bir üst simge olduğu anlamında) - biri diğeriyle açıklamalı. –

+0

@DaveSyer, soruma bakar mısınız lütfen? https://stackoverflow.com/questions/46065063/spring-boot-basic-authentication –

0

WebSecurityConfigurerAdapter uzanan bir Yapılandırma dosyası oluşturun ve benzeri temel güvenlik eklemek için @EnableWebSecurity

Sen configure(HttpSecurity http) gibi yöntemler geçersiz kılabilir ile sınıf açıklama için işaretçisi

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