【发布时间】:2019-09-18 03:03:48
【问题描述】:
我有一个公开了一些端点的 Spring Boot 应用程序。我想从 React 应用程序向这些端点发出请求,但它一直给我 CORS 问题:
在以下位置访问 XMLHttpRequest 'localhost:9090/helios-admin/api/dashboard/clients?page=0&size=30' 来自原点“http://localhost:3000”已被 CORS 策略阻止: 跨源请求仅支持协议方案:http, 数据,铬,铬扩展,https。
所以我尝试对我的所有方法以及 Controller 类使用 @CrossOrigin 注释,但错误是相同的。
我的 react 应用中的 get 请求如下所示:
constructor() {
this.url = 'localhost:9090/helios-admin/api/dashboard/clients?page=0&size=30';
}
getProjectStatusById(projectId) {
Axios.get(this.url).then(res=>{
console.log(res.data);
})
}
缺少什么?
编辑 在我的 Spring Boot 应用程序中,我只有这个类来配置安全性:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
SysdataUserDetailsService sysdataUserDetailsService;
@Autowired
private JwtAuthEntryPoint jwtAuthEntryPoint;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(sysdataUserDetailsService)
.passwordEncoder(encoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests()
.antMatchers(PathConstants.USER_AUTH +"/**", PathConstants.HELIOS+"/dashboard/**").permitAll()
.antMatchers(HttpMethod.GET, "/"+PathConstants.PROCESS_DEFINITION+"/**").permitAll()
.antMatchers(HttpMethod.POST, "/"+PathConstants.PROCESS_DEFINITION+"/**").permitAll()
.antMatchers(HttpMethod.GET, "/"+PathConstants.PROCESS_INSTANCE+"/**").permitAll()
//.anyRequest().authenticated()
.anyRequest().permitAll()
.and()
.exceptionHandling().authenticationEntryPoint(jwtAuthEntryPoint).and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// custom jwt filter.
http.addFilterBefore(jwtAuthFilter(), UsernamePasswordAuthenticationFilter.class);
}
}
【问题讨论】:
-
你有字符串安全吗?
标签: reactjs spring-boot cors axios