美好的一天,
我正在努力学习 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/