2016-12-09 7 views
0

私は私の春のセキュリティ春のセキュリティ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); 
    } 

毎回に問題がある、私はそれがブートストラップファイルに私を送り、ログインしない...

はlocalhost:8080/CSS /ブートストラップ-3.3.7-DIST/CSS/bootstrap.min.css

か...

はlocalhost:8080/CSS /のcustom.css

か...

はlocalhost:8080/JS/jqueryの-2.1.4.min.js

ではなく、同じものに(ランダムに見える! :-)

時々、のCSSが機能しないことがあります。ログインフォームにはスタイルはありませんが、アプリを実行するとスタイルがあるため、ときどきしかありません。私はログインするときも、ログアウトするときやログインをやり直すときはしません。したがって、ユーザーが正しくログインすると、スタイルが再び反映されます。

ユーザーが「ROLE_USER」で「/ controlpanel」にアクセスしようとしたときにアクセスが許可されているはずですが、ユーザー(Role_ADMIN)と操作するとうまく動作します。

答えて

0

にあなたの設定を変更してください。

は全くこれらのリソースを無視するようにしてください:

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

は、すべての場所を満たすためにパターンを定義します。

+0

ありがとう、それは動作しません。私はこれらのパスに静的なCSS、JS、イメージを入れていますが、動作しません。あなたは私が見てみることができる任意のドキュメントを知っていますか?再度、感謝します。 – Mike

+0

それは動作しないとはどういう意味ですか?ページがロードされている間に読み込まれるリソース(Web Developerツールの「Network」タブ)を見ると、静的コンテンツ・ファイルの「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

関連する問題