【问题标题】:using spring boot app as resource server and google oauth2 as authorization server使用spring boot app作为资源服务器,google oauth2作为授权服务器
【发布时间】:2019-04-17 01:47:09
【问题描述】:

我有一个 web 应用程序,它使用 angular6 作为前端,使用 spring boot 作为其余的 api。

我的 Angular 应用程序在 localhost:4200 上运行,并且能够通过 google 成功进行身份验证,我可以看到 authToken 和 idToken。 (我在角度使用了angularx-social-login 插件)

现在,我想使用此身份验证令牌来保护我在 localhost:8080 上运行的 Spring Boot api 服务器。

我正在尝试类似于 (image source) 这里客户端是我的 Angular 应用程序,资源服务器是 Spring Boot 应用程序,授权服务器是 google

我尝试使用 EnableResourceServer 注释和许多不同的方法,但似乎没有任何效果。它总是通过检查内存令牌存储给我无效的令牌,甚至不尝试用谷歌验证正确的令牌。

我在 spring 应用程序和 Angular 应用程序中为 oauth2 配置使用相同的客户端 ID 和密码。

如何让 Spring Boot 应用程序在每个请求中使用 google 验证令牌?

我的应用程序.properties

server.servlet.context-path=/api

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:file:~/h2/testdb

logging.level.org.springframework.web=TRACE
logging.level.org.springframework.security=TRACE
logging.level.org.hibernate=TRACE

security.oauth2.resource.filter-order = 3
spring.security.oauth2.client.registration.google.client-id=#google-client-id
spring.security.oauth2.client.registration.google.client-secret=#google-client-secret    
security.oauth2.resource.user-info-uri= https://www.googleapis.com/oauth2/v3/userinfo # URI of the user endpoint.

这是我的资源服务器配置类

@Configuration
@EnableResourceServer
public class OAuth2ResourceServerConfig extends ResourceServerConfigurerAdapter {

    private static final String RESOURCE_ID = "rest_api";

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId(RESOURCE_ID).stateless(false);
    }
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .requestMatcher(new RequestHeaderRequestMatcher("Authorization"))
                .authorizeRequests().anyRequest().fullyAuthenticated();
    }

    @EnableGlobalMethodSecurity(prePostEnabled = true)
    protected static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration {
        @Override
        protected MethodSecurityExpressionHandler createExpressionHandler() {
            return new OAuth2MethodSecurityExpressionHandler();
        }
    }
}

【问题讨论】:

  • 你找到任何回应了吗?

标签: angular spring-boot google-oauth


【解决方案1】:

希望您已经找到了解决方案。否则看看这个article

附带说明:您将资源声明为有状态

resources.resourceId(RESOURCE_ID).stateless(false);

你试过无状态使用它吗?

resources.resourceId(RESOURCE_ID).stateless(true);

【讨论】:

  • 能否将链接文章的相关部分发布到您的帖子中,好吗?链接不会永远存在。
猜你喜欢
  • 2022-08-03
  • 2018-08-29
  • 2015-05-14
  • 2016-05-21
  • 2014-07-09
  • 1970-01-01
  • 2019-04-05
  • 2022-11-23
  • 2019-10-27
相关资源
最近更新 更多