2016-04-28 28 views
0

私は2つの設定をしています。最初のものは、(/ api/**)からのすべてのリクエストは、決められたIPから来なければならないということを達成したいと思います。次のようなSpring Security Java Config - リクエストURLの動的IPリスト

...

.authorizeRequests().antMatchers("/api/**").hasIpAddress("dynamic List of IPs"); 

そうでない場合はアクセスが拒否される、IPがデータベースに格納されているかどうかをチェックする必要があります。

そして、secound configが残りを処理します。

@EnableWebSecurity 

public class AppSecurityConfig { 

@Autowired 
protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
    auth.userDetailsService(new CustomUserDetailsService()).passwordEncoder(new Md5PasswordEncoder()); 

} 

@Configuration 
@Order(1) 
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .csrf().disable() 
       .headers().disable() 
       .authorizeRequests().antMatchers("/api/**").hasIpAddress("dynamic List of IPs"); 
    } 
} 

@Configuration 
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.sessionManagement().maximumSessions(1) 
       .expiredUrl("/error/expired.xhtml").and() 
       .invalidSessionUrl("/Anmeldung.xhtml?check=invalid"); 
     http 
       .csrf().disable() 
       .headers().disable() 
       .formLogin().loginPage("/Anmeldung/").loginProcessingUrl("/j_spring_security_check").successHandler(new CustomAuthenticationSuccessHandler()) 
       .failureUrl("/Anmeldung.xhtml?check=error").usernameParameter("j_username").passwordParameter("j_password") 
       .and() 
       .exceptionHandling().accessDeniedPage("/error/403.xhtml") 
       .and() 
       .logout().logoutUrl("/logout").logoutSuccessUrl("/Anmeldung.xhtml?check=logout").invalidateHttpSession(false).deleteCookies("JSESSIONID").permitAll(); 

     ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry interceptUrlRegistry = http.authorizeRequests(); 
     interceptUrlRegistry.antMatchers("/Administrator/*").hasAnyAuthority("ROLE_ADMIN"); 
     interceptUrlRegistry.antMatchers("/*").hasAnyAuthority("ROLE_USER"); 
     interceptUrlRegistry.antMatchers("/Anmeldung/index.xhtml").anonymous(); 
     interceptUrlRegistry.antMatchers("/template/*").denyAll(); 
     interceptUrlRegistry.antMatchers("/resources/**").permitAll(); 
    } 
} 
} 

ありがとうございました。

+0

そのサブネットがhttp://forum.spring.io/forum/spring-projects/security/95303-how-to-use-hasipaddress – HRgiger

+0

ねえ、リンクをありがとう、私は私と思えば素敵な議論でありますフィルターが必要です。リクエストIPアドレスがデータベースにある場合は、すべてのリクエスト(/ api/**)で確認できます。 – Sascha

+0

このような?はい、それは静的で、私はdynmaicのリストが必要です。http://stackoverflow.com/questions/28303097/spring-security-multiple-hasipaddress-antmatchers – HRgiger

答えて

0

httpsecurityオブジェクトは、以下で参照するコードのようにforループ内で動的に設定できます。

for (Entry<String, String> entry : hasmapObject) { 
       String url = entry.getKey().trim(); 
       String ips= entry.getValue().trim(); 
    http.authorizeRequests().and().authorizeRequests().antMatchers(url).hasIpAddress(ips); 
      } 

これは私のために働きました。 hashmapオブジェクトには、URLとそれに対応するipsの動的リストがアクセス権を与えていました。

"http.authorizeRequests()。and()" XMLでhttp子要素を設定するためにxml設定で使用するように、thisと()はインデントする必要があります。

これが役に立ったら教えてください。ここで

関連する問題