我想调用后端(java/springboot)在我的应用程序上创建一个登录功能。我从前端(角)到后端调用jwtoken。在后端生成没有问题,前端接收到好的jwtoken。
https://www.example.com/api/v1/authenticate
答复:
jwtToken: "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdG9ja21hbmFnZXIiLCJleHAiOjE2NDMzODc1NjQsImlhdCI6MTY0MzM2OTU2NH0.mYl10_v5Dm3S6K0lM5uBOLs1x2rYwC76ZkJ9w6-QOTw1tE7xjiKxMIOiiU6QgwsKxqtDmz31aCEA4ePY2IzkUQ"到现在为止还好
Request URL: https://www.example.com/api/v1/authenticate
Request Method: POST
Status Code: 200 (from service worker)
Referrer Policy: strict-origin-when-cross-origin但是,我想让用户在调用的头上进行身份验证。
https://www.example.com/api/v1/users/find
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdG9ja21hbmFnZXIiLCJleHAiOjE2NDMzODc1NjQsImlhdCI6MTY0MzM2OTU2NH0.mYl10_v5Dm3S6K0lM5uBOLs1x2rYwC76ZkJ9w6-QOTw1tE7xjiKxMIOiiU6QgwsKxqtDmz31aCEA4ePY2IzkUQ
Payload : {username: "example", password: "password"}不幸的是,有一个CORS错误:
Access to fetch at 'https://www.example.com/api/v1/users/find' from origin 'https://www.example.com/api/v1/users/find' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.我需要帮助,请:sweat_smile:
发布于 2022-01-29 11:51:02
我通过更新我的后端来解决这个问题;-)我在configure()方法中添加了configure();
@EnableWebSecurity
@RequiredArgsConstructor
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf()
.disable()
.authorizeRequests()
.mvcMatchers("/api/v1/authenticate")
.permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint(this.jwtAuthenticationEntryPoint)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
httpSecurity.cors();
httpSecurity.addFilterBefore(this.jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
}
}后端现在可以getHeaders,所以授权。
发布于 2022-01-29 07:57:56
我假设您正在使用命令"ng serve“在开发模式下运行您的应用程序,如果是这样的话,为了避免这个CORS问题,您可以通过代理支持调用后端(java/springboot) REST。请阅读以下部分的角度文件,以了解更多关于代理。
https://stackoverflow.com/questions/70895175
复制相似问题