2017-11-14 6 views
0

私は、Spring認証をSpring認証に利用するSpring Boot Web Appに取り組んでいます。現在のところ、適切なダッシュボードにリダイレクトされる3種類のユーザーがあり、直接URLを介して他のダッシュボードにアクセスしようとすると、アクセスが拒否されます。認証されていないユーザーは、サイトの残りの部分へのアクセスも拒否されます。テスト中に、ユーザーが正常に認証されると、認証されていないときと同じようにログインページにリダイレクトされるのではなく、テスト用に「/」またはlocalhost:8080への直接URLを使用する場合、 MongoDBインスタンスのテーブルの情報を持つJSONが返されます。悪いことに、URLにテーブル名を追加すると、テーブル全体がJSON形式で受け取られます。Spring Boot、Spring Security - MongoDBの直接URLクエリを防止する

例:http://localhost:8080/premios(現在はダミーの値を保持する)

{ 
    "_embedded" : { 
    "premios" : [ { 
     "title" : "hdlkfjgd", 
     "description" : "dflgkj", 
     "points" : 20, 
     "enabled" : false, 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/premios/5a013a974833be43fa38dc53" 
     }, 
     "premio" : { 
      "href" : "http://localhost:8080/premios/5a013a974833be43fa38dc53" 
     } 
     } 
    }, { 
     "title" : "dfdggd", 
     "description" : "dfgd", 
     "points" : 5, 
     "enabled" : false, 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/premios/5a0a11964833be69a480a901" 
     }, 
     "premio" : { 
      "href" : "http://localhost:8080/premios/5a0a11964833be69a480a901" 
     } 
     } 
    }, { 
     "title" : "alksksjlkakjf", 
     "description" : "sdlkfkjsdlfkj", 
     "points" : 5, 
     "enabled" : false, 
     "_links" : { 
     "self" : { 
      "href" : "http://localhost:8080/premios/5a0a12b24833be6a6e47a22a" 
     }, 
     "premio" : { 
      "href" : "http://localhost:8080/premios/5a0a12b24833be6a6e47a22a" 
     } 
     } 
    } ] 
    }, 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:8080/premios{?page,size,sort}", 
     "templated" : true 
    }, 
    "profile" : { 
     "href" : "http://localhost:8080/profile/premios" 
    }, 
    "search" : { 
     "href" : "http://localhost:8080/premios/search" 
    } 
    }, 
    "page" : { 
    "size" : 20, 
    "totalElements" : 3, 
    "totalPages" : 1, 
    "number" : 0 
    } 
} 

どのように私はこれを防ぐことができますか?これは、私がSpring Securityをどのように設定したのか、またはバックエンドのコントローラだけがクエリを作成できるようにするためにmLabで行う必要があるためですか?上記のプレミオURLは、私たちのコントローラの中で定義されたリクエストメソッドではありません。なぜそれが動作しているのかわかりません。ここでは、それが構成されています方法は次のとおりです。

WebSecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private SimpleAuthenticationSuccessHandler successHandler; 

    @Autowired 
    public void configureGlobal(
      AuthenticationManagerBuilder auth, 
      CustomUserDetailsService userDetailsService) throws Exception { 
     auth 
      .userDetailsService(userDetailsService); 
    } 

    @Override 
    protected void configure(HttpSecurity http) 
      throws Exception { 
     http 
      .csrf().disable() 
      .authorizeRequests() 
      .antMatchers("/css/**", "/js/**", "/images/**", "/script/**").permitAll() 
      .antMatchers("/signup").permitAll() 
      .antMatchers("/webapp/admin").hasRole("ADMIN") 
      .antMatchers("/webapp/sales").hasRole("SALES") 
      .antMatchers("/webapp/business").hasRole("USER") 
      .anyRequest().authenticated() 
      .and() 
      .formLogin() 
      .successHandler(successHandler) 
      .loginPage("/login") 
      .permitAll() 
      .and() 
      .logout() 
      .logoutRequestMatcher(new AntPathRequestMatcher(("/logout"))) 
      .logoutSuccessUrl("/login") 
      .permitAll(); 
    } 
} 

SimpleAuthenticationSuccessHandler.java

@Component 
public class SimpleAuthenticationSuccessHandler implements AuthenticationSuccessHandler{ 

    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, 
             HttpServletResponse httpServletResponse, 
             Authentication authentication) throws IOException, ServletException { 

     Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities(); 
     authorities.forEach(authority -> { 
      if(authority.getAuthority().equals("ROLE_ADMIN")) { 
       try { 
        redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/webapp/admin"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      else if(authority.getAuthority().equals("ROLE_SALES")) { 
       try { 
        redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/webapp/sales"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      else if(authority.getAuthority().equals("ROLE_USER")) { 
       try { 
        redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/webapp/business"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      else { 
       throw new IllegalStateException(); 
      } 
     }); 
    } 
} 

たぶん、それが成功ハンドラとは何かですか?私はSpring Bootを使用してWebアプリケーションを構築することに新しいので、どんな助けでも大歓迎です!同様の問題を持つ人のために

答えて

0

は、私は変更することで、私の問題を解決しました:

.anyRequest().authenticated() 

.anyRequest().denyAll(). 

に、ユーザーが認証されたら、彼らは私たちのWebアプリケーションのいずれかの要求を行うことができました。 denyAllを使用することで、アンタッチャーで指定されていないすべてのリクエストを防ぐことができます。

.antMatchers("/webapp/business").access("isFullyAuthenticated() and hasRole('USER')") 

.antMatchers("/webapp/business").hasRole("USER") 

そして、念のために、私は私達のログインページ「/ログイン」を「/」に任意の要求を再ルーティングすることを確認しました:私はまた、このようなantmatchersを変更しました。

関連する問題