【问题标题】:Request method 'POST' not supported when @EnableOAuth2Sso@EnableOAuth2Sso 时不支持请求方法“POST”
【发布时间】: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 中添加 HttpClientXsrfModuleimports(我在 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


【解决方案1】:

您需要做以下事情。

  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 使用不小心,它真的会搞砸整个安全配置

【讨论】:

  • 我投了赞成票,但我的声誉太低,无法显示,再次感谢您。另一个旁注,即使我注释掉security.oauth2.resource.filter-order=3,它似乎也能正常工作我认为这可能是因为使用 Spring Boot Starter 版本 1.5.9 与旧版本...
  • 也许吧。这就是为什么我包含一个指向过滤器订单问题页面的链接,以便您查看详细信息。
猜你喜欢
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 2020-10-29
  • 2016-05-02
相关资源
最近更新 更多