@ EnableOAuth2Sso时不支持请求方法'POST'

问题描述 投票:2回答:1

我在Spring Boot 1.5.9 / Angular 5应用程序上使用Request method 'POST' not supported时收到@EnableOAuth2Sso错误。

GET请求工作正常,JSESSIONID cookie看起来好像在前端设置得很好。 Cookie正在通过所有请求和匹配。

在响应标题:Status Code: 405 Allow: 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上)。从root @EnableOAuth2Sso类中删除CompanyApp。配置如下:

@Configuration
@EnableOAuth2Sso
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.
                authorizeRequests().anyRequest().authenticated().
                and().
                csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }
}
spring-security spring-security-oauth2
1个回答
0
投票

您需要做以下事情。

  1. 在应用程序属性中添加此配置:

security.oauth2.resource.filter-order=3

有关更多信息,请访问here

  1. 由于您还没有发布Spring安全配置,因此不确定它现在是怎样的。但它应该是这样的: @Configuration @EnableOAuth2Sso public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() // or replace this with tour csrf token repository .authorizeRequests() .anyRequest().authenticated(); }

This SO post解释如果没有小心使用@EnableOauth2Sso它真的会搞乱整个安全配置

© www.soinside.com 2019 - 2024. All rights reserved.