我想在我的全栈应用程序(前端为Angular 6,后端为Spring Boot)中通过Google和Microsoft实现授权,但是遇到了问题。
我遵循此方案,该方案描述了应如何实现流程:
OAuth2授权流程:
问题出在步骤4中,前端应将授权代码发送到后端。 我只是不知道使用哪个端点以及如何在Spring Security配置中配置它。
该端点应获取授权代码,在OAuth提供者侧进行检查,请求访问并刷新令牌并记住授权用户。
也许有一个应该如何做的例子? Google的答案似乎不合适。
我已经做了:
在安全配置中添加了@ EnableOAuth2Sso批注。 但这不是我真正需要的,因为它可以在后端进行授权。 (从后端重定向到Google页面有效,但我需要从前端访问)
\n @ EnableOAuth2Sso\n 公共类SecurityConfiguration扩展了WebSecurityConfigurerAdapter {\n ...\n }\n
尝试自己配置Oauth2Login。
\n @Override\n 受保护的void configure(HttpSecurity http)抛出异常{\n http\n .authorizeRequests()。anyRequest()。authenticated()\n 。和()\n .oauth2Login()\n .clientRegistrationRepository(clientRegistrationRepository)\n .authorizedClientService(新的InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository));\n }\n
Gradle依赖项:
<pre>
compile 'org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.0.0.RELEASE'
compile 'org.springframework.security:spring-security-oauth2-jose'
compile 'org.springframework.security:spring-security-oauth2-client'
</pre>
我的期望:
后端上的某些端点,它接受授权代码。 例如:
网址:
localhost:8080/oauth2/authcode/google
<pre>
{
"authorization_code": "...."
}
</pre>
并返回访问令牌:
<pre>
{
"access_token": "...",
"expires": "..."
}
</pre>
中间发生了什么:后端交换授权代码以访问令牌和刷新令牌,并将其保存到自己的数据库中。
谢谢!