2016-04-28 84 views
0

我有兩種配置。第一個想要實現來自(/ api/**)的所有請求必須僅來自確定的ip。Spring Security Java Config - 請求URL的動態IP列表

像繼...

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

的IP是否被存儲在數據庫中應當檢查,否則訪問被拒絕。

而secound配置照顧其餘的。

@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

嘿,謝謝你的鏈接,但我想我需要一個過濾器或類似的。所以我可以檢查每個請求(/ api/**)如果請求的IP地址是在數據庫中。 – Sascha

+0

是這樣的? 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來授予訪問權限。 ()和()需要縮進,就像我們在xml配置中使用的那樣在XML中配置http子元素。

請讓我知道這是否有幫助。

相關問題