@EnableOAuth2Sso 时不支持请求方法 'POST'
Request method 'POST' not supported when @EnableOAuth2Sso
我在 Spring Boot 1.5.9 / Angular 5 应用程序上 @EnableOAuth2Sso
时收到 Request method 'POST' not supported
错误。
GET 请求工作正常,JSESSIONID
cookie 看起来在 front-end 上设置得很好。 Cookie 随所有请求传递并匹配。
在响应中 Header: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
Angular 服务:
public updateCompany( companyData: Company ) {
return this.http.post(this.url, companyData);
}
编辑:
我听从了下面@theLearner 的建议,但仍然想添加 CSRF (XSRF) 保护。这就是我最终这样做的方式:
在 app.module.ts 中将 HttpClientXsrfModule
添加到 imports
(我在 Angular 5)。
从根 CompanyApp
class 中删除 @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());
}
}
您需要做以下事情。
- 在应用程序属性中添加此配置:
security.oauth2.resource.filter-order=3
有关此的更多信息here。
由于您还没有发布 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
不小心使用它真的会搞乱整个安全配置
我在 Spring Boot 1.5.9 / Angular 5 应用程序上 @EnableOAuth2Sso
时收到 Request method 'POST' not supported
错误。
GET 请求工作正常,JSESSIONID
cookie 看起来在 front-end 上设置得很好。 Cookie 随所有请求传递并匹配。
在响应中 Header: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
Angular 服务:
public updateCompany( companyData: Company ) {
return this.http.post(this.url, companyData);
}
编辑:
我听从了下面@theLearner 的建议,但仍然想添加 CSRF (XSRF) 保护。这就是我最终这样做的方式:
在 app.module.ts 中将 HttpClientXsrfModule
添加到 imports
(我在 Angular 5)。
从根 CompanyApp
class 中删除 @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());
}
}
您需要做以下事情。
- 在应用程序属性中添加此配置:
security.oauth2.resource.filter-order=3
有关此的更多信息here。
由于您还没有发布 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
不小心使用它真的会搞乱整个安全配置