有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpring安全性会阻止提交请求。连接超时

我在应用程序中使用了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 Security被禁用)时,它可以正常工作

这是我的配置模板

 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; }

然后我扩展了CustomRestTemplateContractor,以获得Spring RestTemplate的功能和其他额外功能


共 (0) 个答案