我在应用程序中使用了Spring security,并且看到Spring Security阻止了一些传出请求
我的spring配置看起来像这样:
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
.and()
.formLogin()
.failureHandler(authenticationFailureHandler())
.and()
.httpBasic()
.and()
.antMatcher("/**");
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService);
}
@Bean
public AuthenticationFailureHandler authenticationFailureHandler() {
return new SimpleUrlAuthenticationFailureHandler();
}
@Bean
public LogoutSuccessHandler logoutSuccessHandler() {
return new SimpleUrlLogoutSuccessHandler();
}
}
我看到我的休息服务请求
I/O error on POST request for "https://someserver.com/api/v1/public/createUser": Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
当我尝试在没有Spring Security的情况下调用此rest服务时(Spring安全性已禁用),它工作正常。
这是我的配置,如果RestTemplate
public CustomRestTemplateContructor() {
super();
this.restTemplate = new RestTemplate();
this.restTemplate.getMessageConverters().add(0, new
StringHttpMessageConverter(Charset.forName("UTF-8")));
}
我具有自定义RestTemplate实现的类如下:
@Configurable
public abstract class CustomRestTemplateContructor extends TestDataBase implements Runnable {
public static final Logger log = LoggerFactory.getLogger(CustomRestTemplateContructor.class);
private String testEnviroment;
@Autowired
private ITestDataService iTestDataService;
private static final Object lockForAddingValueToSharedStorage = new Object();
private String name;
private String profileId;
private final RestTemplate restTemplate;
protected ThreadLocal<WorkerExecutionStatusThreadLocal> workerExecutionStatus = new ThreadLocal<>();
public CustomRestTemplateContructor() {
super();
this.restTemplate = new RestTemplate();
this.restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
}
public ITestDataService getiTestDataService() {return iTestDataService; }
public void setiTestDataService(ITestDataService iTestDataService) { this.iTestDataService = iTestDataService; }
public String getTestEnviroment() { return testEnviroment; }
public void setTestEnviroment(String testEnviroment) { this.testEnviroment = testEnviroment; }
public void setName(String name) {
this.name = name;
}
public String getName() { return name; }
public String getProfileId() {
return profileId;
}
public void setProfileId(String profileId) {
this.profileId = profileId;
}
public RestTemplate getRestTemplate() { return restTemplate; }
然后我扩展CustomRestTemplateContructor以获取Spring RestTemplate的功能和其他额外功能。