2

私は2つの構成を持っています。1 つ目は、(/api/**) からのすべてのリクエストが特定の IP からのみ送信されるようにすることです。

いいね フォロー中...

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

IP がデータベースに保存されているかどうかを確認する必要があります。保存されていない場合、アクセスは拒否されます。

残りは 2 番目の構成で処理されます。

@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();
    }
}
}

ご協力いただきありがとうございます。

4

1 に答える 1

0

以下で参照されているコードのように、for ループ内で httpsecurity オブジェクトを動的に構成できます。

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 とそれに対応する IP の動的リストが含まれていました。

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

これが役立つかどうか教えてください。

于 2018-02-07T12:17:06.813 に答える