gpt4 book ai didi

java - 使用 Java 配置禁用 csrf

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:36 25 4
gpt4 key购买 nike

美好的一天,

我正在努力学习 Spring。我目前正在做这个指南:http://spring.io/guides/gs/consuming-rest/

我已按照所有说明进行操作,但是,当我尝试运行该应用程序时,显示 403 Forbidden。

网上查了一下,原来是csrf保护的问题。因此,我继续在网上搜索如何禁用 csrf。这是我的 Java 配置:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
}
}

我的问题是,我该如何使用这个配置?具体应该插在代码的哪一部分?

这是教程中所述的其他 2 个类。它们都属于同一个包(你好)。

@JsonIgnoreProperties(ignoreUnknown = true)
public class Page {
private String name;
private String about;
private String phone;
private String website;

public String getName() {
return name;
}

public String getAbout() {
return about;
}

public String getPhone() {
return phone;
}

public String getWebsite() {
return website;
}
}

public class Application {

public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
Page page = restTemplate.getForObject("http://graph.facebook.com/pivotalsoftware", Page.class);
System.out.println("Name: " + page.getName());
System.out.println("About: " + page.getAbout());
System.out.println("Phone: " + page.getPhone());
System.out.println("Website: " + page.getWebsite());
}

}

最佳答案

WebSecurityConfig 类上添加 @Configuration,当您启动 Spring 应用程序时,它将被自动扫描。您无需编写任何代码。

这是带有@Configuration的代码

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable();
}
}

关于java - 使用 Java 配置禁用 csrf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29875589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com