【发布时间】:2018-02-27 23:05:49
【问题描述】:
当我在 Spring Boot 1.5.9 / Angular 5 应用程序上 @EnableOAuth2Sso 时收到 Request method 'POST' not supported 错误。
GET 请求工作正常,JSESSIONID cookie 看起来在前端设置得很好。 Cookie 正在与所有请求和匹配项一起传递。
在响应头中:Status Code: 405Allow: GET, HEAD
这是我的第一个 Stack Overflow 问题,我已经完成了所有通常的调查,但似乎无法深入了解这个问题。对于我在询问/格式化此问题时的任何疏忽,我提前道歉。
@SpringBootApplication
@EnableOAuth2Sso
@EnableOAuth2Client
public class CompanyApplication {
public static void main(String[] args) {
SpringApplication.run(CompanyApplication.class, args);
}
}
相关控制人
@RestController
@RequestMapping("api")
public class CompanyController {
@Autowired
CompanyRepository companyRepository;
@Autowired
ContactRepository contactRepository;
@PostMapping("companies")
public Company createCompany(@Valid @RequestBody Company company) {
logger.info("*** Starting POST request of company name: {}", company.getName());
company = updateContacts(company); // pass updated contact info into the Contact DB
companyRepository.save(company);
logger.info("*** Successful POST request of company: {}, ID: {},", company.getName(), company.getId());
return company;
}
配置设置:
security.oauth2.client.clientId=myID
security.oauth2.client.clientSecret=mySecret
security.oauth2.client.accessTokenUri=https://myserver.com/connect/token
security.oauth2.client.userAuthorizationUri=https://myserver.com/connect/authorize
security.oauth2.client.scope=openid,profile,email
security.oauth2.resource.userInfoUri=https://myserver.com/connect/userinfo
角服务:
public updateCompany( companyData: Company ) {
return this.http.post(this.url, companyData);
}
编辑:
我遵循了下面@theLearner 的建议,但仍想添加 CSRF (XSRF) 保护。这就是我最终这样做的方式:
在 app.module.ts 中添加 HttpClientXsrfModule 到 imports(我在 Angular 5 上)。
从根 CompanyApp 类中删除 @EnableOAuth2Sso。
配置如下:
@Configuration
@EnableOAuth2Sso
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests().anyRequest().authenticated().
and().
csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
}
【问题讨论】:
-
收到
Request method 'POST' not supported时访问的是什么url/api? -
api/companies - 这是正在生成的完整请求标头:
POST /api/companies HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 278 Accept: application/json, text/plain, */* Origin: http://localhost:8080 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36 Content-Type: application/json Referer: http://localhost:8080/auth/companies Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Cookie: JSESSIONID=41CF1089A5BF001351F378643685EFB2 -
您是否尝试过在 spring 安全配置中禁用 csrf 保护?
-
我有,而且它有效,问题是“一切”都有效——即使是没有附加 cookie 的请求......
-
你的意思是说除了 POST 请求外一切正常?还是禁用 csrf 让一切正常?
标签: spring-security spring-security-oauth2