需要帮助将OAuth2安全性添加到Spring Boot微服务

时间:2019-12-27 03:04:05

标签: java spring-boot spring-security oauth-2.0

我创建了一系列的Spring Boot微服务,并大致遵循了我发现的教程here。我已经成功完成了我的项目,并且没有进行任何保护其余端点或用户界面的尝试。但是最终我需要使用LDAP保护所有内容,并且需要根据用户角色有条件地允许访问用户界面部分。暂时,在不涉及LDAP的情况下,我已经尝试了许多教程,介绍如何将内存身份验证与OAuth2结合使用,但我不明白实现这一目标所需要做的是什么。

我创建了一个github repository,其中包含两个分支。第一个分支称为no-authentication,它演示了在开始尝试添加身份验证之前我项目的简化版本。自述文件描述了项目构成,但本质上是一个配置服务器,一个服务注册表,一个带有两个未经身份验证的端点的剩余资源以及一个访问这两个端点的用户界面模块。

这个想法是,第一个端点最终将需要一个组成员身份(“ USER”),另一个端点需要另一个组成员身份(“ ADMIN”)。但是我还没有走。

第二个分支称为oauth-server。该分支添加了另一个名为oauth2-server的spring boot模块,该模块包含我的用户界面转发到以进行身份​​验证的代码。

我希望看到一个登录页面,但是当我输入有效的凭据(用户/用户或管理员/管理员)时,页面上会显示错误:

OAuth Error
error="invalid_grant", error_description="Invalid redirect: http://localhost:8084/authtest/login does not match one of the registered values."

我是这个领域的新手,我真的只是通过各种尝试来使自己动起来。您可以直接查看代码,并且需要精通才能了解设置。大致设置身份验证的工作方式。

需要认证的ui称为authentication-test-ui。可以在其resources/bootstrap.yml中找到其配置,其其余配置来自控制中心/配置服务器模块的类路径,源位于resources/config-repo/authentication-test-ui.yml。这是安全性配置:

gateway-server: 'http://localhost:8901/authserver'

security:
  oauth2:
    client:
      client-id: ui
      client-secret: uisecret
      scope: ui
      access-token-uri: ${gateway-server}/oauth/token
      user-authorization-uri: ${gateway-server}/oauth/authorize
      pre-established-redirect-uri: http://localhost:8084/authtest
    resource:
      user-info-uri: ${gateway-server}/userInfo

UI的应用程序类使用@ EnableOAuth2Sso注释。

授权服务器位于模块services / oauth2-server中。这用@EnableAuthorizationServer注释,并遵循与ui类似的配置模式(在其自己的模块中具有bootstrap.yml,在配置服务器上具有yml)。该代码执行内存中身份验证,如下所示:

   @Override
   public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
            .withClient("ui")
            .secret(passwordEncoder.encode("uisecret"))
            .authorizedGrantTypes("authorization_code", "implicit", "password", "client_credentials", "refresh_token")
            .scopes("ui")
            .redirectUris(
                  "http://localhost:8084/authtest"
            );
   }

安全配置如下:

   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http
            .requestMatchers()
            .antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
            .and()
            .authorizeRequests()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll();
   }

   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth
            .inMemoryAuthentication()
            .passwordEncoder(passwordEncoder())
            .withUser("admin").password(passwordEncoder().encode("admin")).roles("USER", "ADMIN")
            .and()
            .withUser("user").password(passwordEncoder().encode("user")).roles("USER");
   }

我很可能没有仅仅通过查看我粘贴的代码片段就提供了正确的信息来理解问题,因此查看项目可能是唯一获得完整信息的唯一方法。

如果有人可以帮助我,我将不胜感激。我已经进行了将近一个星期的时间,感觉我离目标不会太远。我真的在寻找获得认证的最简单方法。

1 个答案:

答案 0 :(得分:0)

您将重定向uri配置为http:// localhost:8084 / authtest,但在调用时将重定向uri指定为http:// localhost:8084 / authtest / login,因此失败。