Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OAuth2与Google-CORS错误(角度和弹簧引导)_Java_Angular_Spring Security_Spring Security Oauth2 - Fatal编程技术网

Java OAuth2与Google-CORS错误(角度和弹簧引导)

Java OAuth2与Google-CORS错误(角度和弹簧引导),java,angular,spring-security,spring-security-oauth2,Java,Angular,Spring Security,Spring Security Oauth2,我对CORS错误有问题。我确实请求Google oAuth2,但我得到一个CORS错误: 我想获得谷歌认证并生成一个JWT令牌。当我不使用客户端的时候,一切都很好。当我发送角度请求时,这是CORS的问题。我允许所有类型的COR。为什么我会犯这个错误 Access to XMLHttpRequest at 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=1020159669873-d9r3

我对CORS错误有问题。我确实请求Google oAuth2,但我得到一个CORS错误: 我想获得谷歌认证并生成一个JWT令牌。当我不使用客户端的时候,一切都很好。当我发送角度请求时,这是CORS的问题。我允许所有类型的COR。为什么我会犯这个错误

Access to XMLHttpRequest at 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=1020159669873-d9r35ssmnejud852bam87d8gqtcj5qf1.apps.googleusercontent.com&scope=openid%20profile%20email&state=8nizHP1X2z9sA8m0vqM4Lzd6VT24R15eSw5flteTywM%3D&redirect_uri=http://localhost:8080/oauth2/callback/google' (redirected from 'http://localhost:8080/oauth2/authorization/google')
from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Cross-Origin Read Blocking (CORB) blocked cross-origin response https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=1020159669873-d9r35ssmnejud852bam87d8gqtcj5qf1.apps.googleusercontent.com&scope=openid%20profile%20email&state=8nizHP1X2z9sA8m0vqM4Lzd6VT24R15eSw5flteTywM%3D&redirect_uri=http://localhost:8080/oauth2/callback/google with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
我的请求:

googleLogin():可观察{
返回this.http.get
(environment.baseUrl+'/oauth2/authorization/google')
.管道(抽头)(响应=>{
setItem('access\u token',response.accessToken);
}));
}
//...
public onGoogleLogin():void{
this.authService.googleLogin().subscribe();
}
//...
CORS配置:

@覆盖
公共作废添加公司标志(公司注册处){
登记处
.addMapping(“/**”)
.允许的来源(“*”)
.allowedMethods(“HEAD”、“OPTIONS”、“GET”、“POST”、“PUT”、“PATCH”、“DELETE”)
.maxAge(MAX_AGE_SECS);
}
安全配置:

@覆盖
受保护的无效配置(HttpSecurity http)引发异常{
http
.cors()
.及()
.csrf()
.disable()
.例外处理()
.authenticationEntryPoint(未经授权的处理程序)
.及()
.会议管理()
.sessionCreationPolicy(sessionCreationPolicy.STATELESS)
.及()
.授权请求()
.antMatchers(“/”,
“/favicon.ico”,
“/***.png”,
“/***.gif”,
“/***.svg”,
“/***.jpg”,
“/***.html”,
“/***.css”,
“/***.js”)
.permitAll()
.antMatchers(“/api/v1/oauth0/**”)
.permitAll()
.antMatchers(“/api/v1/oauth2/**”)
.permitAll()
.anyRequest()
.authenticated()
.及()
//włączenie obslugi oauth2
.oauth2Login()
.successHandler(此.successHandler)
.redirectionEndpoint()文件
.baseUri(“/oauth2/callback/*”)
.及()
.userInfoEndpoint()
.OID客户服务(CustomOID客户服务);
http.addFilterBefore(jwtAuthenticationFilter(),UsernamePasswordAuthenticationFilter.class);
}
成功处理程序:

@Autowired
私有用户存储库用户存储库;
@自动连线
私有JwtTokenProvider tokenProvider;
私有最终静态字符串URL=”http://localhost:8080/api/v1/oauth2/authenticate";
@凌驾
验证成功时的公共无效(
HttpServletRequest请求,
HttpServletResponse,
身份验证)引发IOException、ServletException{
if(response.isCommitted()){
返回;}
DefaultOidcUser oidcUser=(DefaultOidcUser)身份验证。getPrincipal();
系统输出打印LN(oidcUser);
Map attributes=oidcUser.getAttributes();
字符串email=attributes.get(“email”).toString();
User User=userRepository.findByEmail(email).orelsetrow(
()->new ResourceNotFoundException(“用户”、“电子邮件”、电子邮件)
);
String token=tokenProvider.generateToken(用户);
字符串重定向URL=UriComponentsBuilder.fromUriString(URL).queryParam(“令牌”,令牌)
.build().toUriString();
getRedirectStrategy().sendRedirect(请求、响应、重定向URL);
}
}
控制器:

@RestController
@请求映射(“/api/v1/oauth2”)
公共类OAuth2控制器{
@GetMapping(“/authenticate”)
公共响应身份验证器(@RequestParam字符串令牌){
返回ResponseEntity.ok(新的JwtAuthenticationResponse(令牌));
}
}

在本例中,您无法获取令牌,因为您需要进行实际重定向。有两种方法可以绕过RFC中详述的这一要求

  • 在弹出窗口中启动授权流,并通过浏览器中提供的
    postMessage()
    API将服务器收到的令牌从弹出窗口传回webapp
  • 保存状态,不管它是什么,重定向到将启动授权流的服务器,在令牌交换为授权后,重定向回webapp,使用令牌作为查询字符串参数。然后使用它并恢复状态

  • OAuth2需要实际的重定向才能工作:顺便说一句,错误并不是来自您的服务器,而是GoogleI将寻找一些示例代码并尝试这样做。谢谢你抽出时间。如果我成功了,我会告诉你的