2014-09-02 38 views
5

Tôi đã tạo một lớp cấu hình bảo mật Spring cho Spring-Boot. Trang đăng nhập của tôi có các tài nguyên css, js và ico. Các tài nguyên bị từ chối vì lý do bảo mật và được chuyển hướng đến trang đăng nhập mỗi lần. Tại sao EnableWebMVCSecurity không thêm vị trí tài nguyên Classpath. Sau khi thay đổi mã như trong đoạn thứ hai, vị trí tài nguyên I Classpath được thêm vào. không hiểu những gì tôi đang thiếu cho các tài nguyên trong đoạn mã đầu tiên.Cấu hình bảo mật với Spring-boot


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

tôi đã làm việc này bằng cách thay đổi mã để


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

     } 
    } 
} 

Sau khi thay đổi mã tôi nhận thấy rằng Bỏ qua những con đường đã được thêm vào các bộ lọc và tôi thấy như sau trong nhật ký:

 
[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] 

Trả lời

6

Mỗi docs bạn đã vô hiệu hóa tính năng tự động khởi động mùa xuân trong ví dụ đầu tiên bằng cách sử dụng @EnableWebSecurity, vì vậy bạn sẽ phải bỏ qua tất cả các sta một cách rõ ràng tài nguyên tic bằng tay. Trong ví dụ thứ hai, bạn chỉ cần cung cấp WebSecurityConfigurer là phụ gia ở trên cùng của cấu hình tự động mặc định.

+0

Cám ơn con trỏ đến tài liệu. Tôi đã sử dụng 'EnableWebMVCSecurity' khác với' EnableWebSecurity'. – randominstanceOfLivingThing

+0

Nó giống nhau (theo nghĩa là nó là một siêu bên) - một được chú thích với cái kia. –

+0

@DaveSyer, bạn có thể xem câu hỏi của tôi không? https://stackoverflow.com/questions/46065063/spring-boot-basic-authentication –

0

Tạo một Cấu hình tập tin mở rộng WebSecurityConfigurerAdapter và chú thích các lớp học với @EnableWebSecurity

Bạn có thể ghi đè lên các phương pháp như configure(HttpSecurity http) để thêm an ninh cơ bản như dưới đây

@Configuration 
@EnableWebSecurity 
public class AppWebSecurityConfigurer extends WebSecurityConfigurerAdapter { 
    @Override 
    protected void configure(HttpSecurity http) throws Exception {  
     http 
      .csrf().disable() 
      .authorizeRequests() 
       .anyRequest().permitAll(); 
     } 
} 
Các vấn đề liên quan