2016-11-12 13 views
1

dashboard.htmlというページへのアクセスを認証されていないユーザーに制限しようとしています。これまでのところ、私は成功しなかった。ここでのマイWebSecurityConfigurerAdapter:私はそれは私がログインしようとするたびに、無限のリダイレクトループが発生し、このように設定しているときはいつでもSpringブートRESTを使用して.htmlページへのアクセスを制限する方法

@Configuration 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private CustomAuthenticationSuccessHandler authenticationSuccessHandler; 

    @Autowired 
    private CustomAuthenticationFailureHandler authenticationFailureHandler; 

    @Autowired 
    private CustomUserDetailsService userDetailsService; 

    @Autowired 
    private TokenAuthenticationService tokenAuthenticationService; 

    @Bean 
    public BCryptPasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder builder) throws Exception { 
     builder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/index.html", "/", 
         "/login.html","/signup.html", "/videos/**", 
         "/login", "/logout", "/images/**", "/fonts/**", 
         "/css/**", "/js/**", "/pages/**", "/sass/**" 
         ).permitAll() 
       .and() 
      .authorizeRequests() 
       .antMatchers("/dashboard/**", "/dashboard.html/**").authenticated()   
       .and() 
      .authorizeRequests() 
       .anyRequest().authenticated() 
       .and() 
      .addFilterBefore(new StatelessLoginFilter("/login", tokenAuthenticationService, userDetailsService, authenticationManager()), UsernamePasswordAuthenticationFilter.class) 
      .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class) 
      .formLogin() 
       .loginPage("/login.html") 
       .loginProcessingUrl("/login") 
       .usernameParameter("email") 
       .passwordParameter("password") 
       .successHandler(authenticationSuccessHandler) 
       .failureHandler(authenticationFailureHandler) 
       .and() 
      .logout() 
       .logoutSuccessUrl("/") 
       .deleteCookies("JSESSIONID") 
       .permitAll() 
       .and() 
      .csrf() 
       .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) 
       .and() 
      .addFilterAfter(new CsrfTokenFilter(), CsrfFilter.class); 
    } 
} 

。ブラウザはdashboard.htmlにナビゲートしようとしますが、制限されています。これにより、有効なトークンがあるため、ダッシュボードにリダイレクトしようとするログインページにリダイレクトされます。

私はそれは以下のように設定している場合は、誰もがdashboard.htmlにアクセスし、希望されていない/dashboardエンドポイントに電話をかけることができます:

  http 
       .authorizeRequests() 
        .antMatchers("/index.html", "/", 
          "/login.html","/signup.html", "/videos/**", 
          "/login", "/logout", "/images/**", "/fonts/**", 
          "/css/**", "/js/**", "/pages/**", "/sass/**", 
          "/dashboard/**", "/dashboard.html/**").permitAll() 
        .and() 
       .authorizeRequests() 
        .anyRequest().authenticated() 

私のログインはJWTトークンを使用し、SecurityContextを設定するには、以下のフィルタを使用していますプレースホルダ:

私はSecurityContextHolder.getContext().setAuthentication(userAuthentication);という行を使って認証を設定しています。これは完全に正常に動作します。ユーザーが、ユーザーから送信された信任状と一致するDB内に見つかった場合、セキュリティー・コンテキストを使用して、ユーザーに関連付けられたさまざまなデータを検索できます。

私の質問:がどのようにページdashboard.htmlを制限し、認証されていないユーザー(SecurityContextHolder内部の認証オブジェクトのないもの)に/dashboardエンドポイントに呼び出しをすることができますか?

+0

許可をすべて "許可する"ことをお確かめください。おそらくすべてが許可されます。 – kuhajeyan

答えて

3

カスタムRequestMatcherdenyAllと組み合わせて使用​​できます。まず、カスタム照合:

public class PermittedPagesMatcher implements RequestMatcher { 

    @Override 
    public boolean matches(HttpServletRequest httpServletRequest) { 
     if (matchesToPaths(httpServletRequest,"/index.html", "/", "/login.html","/signup.html", "/videos/**", "/login", "/logout", "/images/**", "/fonts/**", "/css/**", "/js/**", "/pages/**", "/sass/**", "/dashboard/**", "/dashboard.html/**")) { 
      return true; 
     } 

     if (matchesToPaths(httpServletRequest, "/dashboard/**", "/dashboard.html/**")) { 
      return httpServletRequest.getUserPrincipal() == null; 
     } 

     return false; 
    } 

    private boolean matchesToPaths(HttpServletRequest httpServletRequest, String... paths) { 
     for (String p : paths) { 
      if (new AntPathRequestMatcher(p).matches(httpServletRequest)) { 
       return true; 
      } 
     } 
     return false; 
    } 
} 

このカスタムRequestMatcherは、デフォルトのページのすべてに許可のページへのリクエストをフィルタリングし、要求が認証されていない場合は、ダッシュボードにのみ使用可能です。

第二に、マッチャを組み合わせて、denyAll()

http 
      .authorizeRequests() 
      .requestMatchers(new PermittedPagesMatcher()) 
      .permitAll() 
     .and() 
      .antMatchers("/dashboard/**", "/dashboard.html/**") 
      .denyAll() 
     .and() 
      .authorizeRequests() 
      .anyRequest() 
      .authenticated() 

denyAll()性を保証、デフォルトでは誰もが、このページへのアクセスを許可されていないこと。

注意:許可と拒否の順序は重要です!

+0

認証されていない間はアクセスできないようにします。この設定では、認証トークンを持っていなくてもまだアクセスできます。 return文を以下のように変更しました: 'return httpServletRequest.getUserPrincipal()!= null;'これは、認証されたユーザがいる場合にのみtrueを返すべきです。今私は同じ問題に取り組んでいます。誰も 'dashboard.html'にアクセスすることはできません。たとえそれが認証されたとしても。認証された/ログインしたユーザーだけがdashboard.htmlにアクセスできるようにします。私は誰もがそれにアクセスすることができますか、誰にも今すぐアクセスすることはできません。 –

+0

私は混乱しています...引用:_ [...]呼び出しを非認証ユーザーに制限するにはどうしたらいいですか?_認証されたユーザーのみを許可する場合は、/ "dashboard/**"あなたの元のコードのhttp.authorizeRequests()。antMatchers(....)から '' /dashboard.html/** ''を実行すると良いです。 – Schrieveslaach

+0

オリジナルの設定に問題があります。何らかの理由で、私が認証されているかどうかにかかわらず、その設定で '/ dashboard.html'にアクセスすることができません。私は 'httpServletRequest.getUserPrincipal()!= null;'の値を出力してtrueを返します。つまり、認証されたユーザーがいるのですが、何らかの理由で '/ dashboard.html'にアクセスできません。 –

関連する問題