我正在开发基于Spring 4.3和Angular(TypeScript)4.3的客户端 - 服务器应用程序,在CORS场景中,在生产中,服务器和客户端位于不同的域上.客户端通过http请求请求REST服务器API.
1. REST和OAUTH配置:
服务器公开REST API:
@RestController
@RequestMapping("/my-api")
public class MyRestController{
@RequestMapping(value = "/test", method = RequestMethod.POST)
public ResponseEntity<Boolean> test()
{
return new ResponseEntity<Boolean>(true, HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
正如Spring文档中所解释的那样受Oauth2保护.显然我修改了上面的内容以适合我的应用程序.一切正常:我可以/my-api/test通过refresh_token和access_token 来保护Oauth2.Oauth2没问题.
2. CORS配置:
由于服务器位于与客户端相关的单独域上(服务器:10.0.0.143:8080,客户端:localhost:4200,正如我现在正在开发的那样),我需要在服务器端启用CORS:
@Configuration
@EnableResourceServer
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
...
@Autowired
SimpleCorsFilter simpleCorsFilter;
@Override
public void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.addFilterAfter(simpleCorsFilter, CorsFilter.class)
.csrf().disable() // notice that now csrf is disabled
... (the rest of http security configuration follows)... …Run Code Online (Sandbox Code Playgroud)