2016-12-09 67 views
0

我有一個問題,我的Spring Security春季安全HTTP FormLogin

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private UsuarioService usuarioService; 

    @Autowired 
    private PasswordEncoder passwordEncoder; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 

     // @formatter:off 

     http 
       .authorizeRequests() 
        .antMatchers(
          "/", 
          "/index/**", 
          "/register", 
          "/doregister", 
          "/login", 
          "/dologin", 
          "/logout-success", 
          "/confirmregister", 
          "/invaliduser", 
          "/expiredtoken", 
          "/userstatuslog", 
          "/verifyemail") 
        .permitAll() 
         .anyRequest() 
          .authenticated() 
      .and() 
       .authorizeRequests() 
        .antMatchers(
          "/login", 
          "logout") 
        .permitAll() 
      .and() 
       .authorizeRequests() 
        .antMatchers(
          "/static/css/**", 
          "/css/custom.css", 
          "/js/**", 
          "/images/**" 
          ) 
         .permitAll() 
      .and() 
       .authorizeRequests() 
        .antMatchers(
          "/forbidden", 
          "/edit-profile-about", 
          "/doeditprofileabout",      
          "/profile" 
          ) 
        .hasRole("USER") 
      .and() 
       .authorizeRequests() 
        .antMatchers(
          "/controlpanel", 
          "/forbidden", 
          "/edit-profile-about", 
          "/doeditprofileabout",      
          "/profile" 
          ) 
        .hasRole("ADMIN") 
      .and() 
       .formLogin() 
        .loginPage("/login") 
         .defaultSuccessUrl("/") 
          .permitAll() 
      .and() 
       .logout() 
        .deleteCookies("remove") 
         .invalidateHttpSession(true) 
          .logoutUrl("/logout") 
       .logoutSuccessUrl("/logout-success") 
        .logoutRequestMatcher(new AntPathRequestMatcher("/logout")); 

     // @formatter:on 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 

     auth.userDetailsService(usuarioService).passwordEncoder(passwordEncoder); 
    } 

每次,我登錄它發送我的引導文件...

本地主機:8080/CSS /引導-3.3.7-DIST/CSS/bootstrap.min.css

或...

本地主機:8080/CSS/custom.css

或...

本地主機:8080/JS/jQuery的2.1.4.min.js

但不到同一個(它似乎是隨機的! :-)

然後有時CSS有效,有時不。在登錄表單中它沒有樣式,但有時只有,因爲當我運行應用程序時,它具有樣式。而且當我登錄時,但不是當我註銷或嘗試再次登錄時。因此,當用戶正確登錄時,它又具有樣式。

當我試圖訪問「/ controlpanel」與用戶(與ROLE_USER)它允許訪問(它不應該),但是當我與用戶(Role_ADMIN)它做的很好。

回答

0

當你的靜態資源由Spring Security的過濾它發生。

嘗試都忽略了這些資源:

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

定義模式,以滿足您的所有位置。

+0

謝謝,但它不起作用。我在這些路徑中包含靜態,CSS,js,圖像,但它不起作用。你知道我可以查看的任何文檔嗎?再次感謝。 – Mike

+0

這是什麼意思,它不工作?在加載頁面時(在Webdeveloper工具中,選項卡網絡中),當您查看讀取什麼資源時,是否會爲您的靜態內容文件看到「HTTP 401」?還是其他什麼?添加更多細節。 – Artegon

+0

謝謝,它現在有效 – Mike

0

問題是您的css文件不能被訪客訪問。所以登錄後,你會被重定向回去。由於瀏覽器緩存,有時登錄頁面中的css會有效。

嘗試改變你的配置

http 
    .authorizeRequests() 
     .antMatchers(
       "/", 
       "/index/**", 
       "/register", 
       ...).permitAll() 
     .antMatchers(
       "/login", 
       "logout").permitAll() 
     .antMatchers(   // You need to add all css file here 
       "/static/css/**", 
       "/css/custom.css", 
       "/css/bootstrap-3.3.7-dist/css/bootstrap.min.cs‌​‌​‌​s", 
       "/js/**", 
       "/images/**").permitAll() 
     .antMatchers(
       "/forbidden", 
       "/edit-profile-about", 
       "/doeditprofileabout",      
       "/profile").hasRole("USER") 
     .antMatchers(
       "/controlpanel", 
       "/forbidden", 
       "/edit-profile-about", 
       "/doeditprofileabout",      
       "/profile").hasRole("ADMIN") 
     .anyRequest().authenticated() 
    .and() 
     ... 
+0

嗨,它仍然顯示localhost:8080/css/bootstrap-3.3.7-dist/css/bootstrap.min.css登錄後...感謝 – Mike

+0

你知道任何文件,我可以看看?再次感謝。 – Mike

+0

嗨,它仍然顯示登錄後localhost:8080/css/bootstrap-3.3.7-dist/css/bootstrap.min.cs ...它沒有顯示任何錯誤。謝謝 – Mike