从spring security oauth 2迁移

j91ykkif  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(601)

我有一个 Spring 启动认证微服务。它使用了oauth2 spring cloud starter依赖项,现在已经不推荐使用了。

buildscript {
  dependencies {
    classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.9.RELEASE"
  }
}

dependencies {
  implementation "org.springframework.boot:spring-boot-starter-actuator"
  implementation "org.springframework.boot:spring-boot-starter-data-jpa"
  implementation "org.springframework.boot:spring-boot-starter-web"
  implementation "org.springframework.cloud:spring-cloud-starter-oauth2:2.1.5.RELEASE"
}

架构取自此处:https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql
它也有一个习惯 user_details table。jpa类正在实现 UserDetails . 我还提供了 UserDetailsService 在我的自定义表中查找用户。
oauth配置非常先进:
authorizationserverconfiguration—配置oauth的位置:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableAuthorizationServer
class AuthorizationServerConfiguration : AuthorizationServerConfigurerAdapter() {
    @Autowired private lateinit var authenticationManager: AuthenticationManager
    @Autowired private lateinit var dataSource: DataSource

    @Autowired
    @Qualifier("customUserDetailsService")
    internal lateinit var userDetailsService: UserDetailsService

    @Autowired
    private lateinit var passwordEncoder: BCryptPasswordEncoder

    override fun configure(endpoints: AuthorizationServerEndpointsConfigurer) {
        endpoints
                .tokenStore(JdbcTokenStore(dataSource))
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
    }

    override fun configure(clients: ClientDetailsServiceConfigurer) {
        // This one is used in conjunction with oauth_client_details. So like there's one app client and a few backend clients.
        clients.jdbc(dataSource)
    }

    override fun configure(oauthServer: AuthorizationServerSecurityConfigurer) {
        oauthServer.passwordEncoder(passwordEncoder)
    }
}

WebSecurity配置-以上类需要:

@Configuration
class WebSecurityConfiguration : WebSecurityConfigurerAdapter() {
  @Bean // We need this as a Bean. Otherwise the entire OAuth service won't work.
  override fun authenticationManagerBean(): AuthenticationManager {
    return super.authenticationManagerBean()
  }

  override fun configure(http: HttpSecurity) {
    http.sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
  }
}

resourceserverconfiguration—要配置对终结点的访问,请执行以下操作:

@Configuration
@EnableResourceServer
class ResourceServerConfiguration : ResourceServerConfigurerAdapter() {
  override fun configure(http: HttpSecurity) {
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and().cors().disable().csrf().disable()
        .authorizeRequests()

        .antMatchers("/oauth/token").authenticated()
        .antMatchers("/oauth/user/**").authenticated()

        .antMatchers("/oauth/custom_end_points/**").hasAuthority("my-authority")

        // Deny everything else.
        .anyRequest().denyAll()
  }
}

这几行给了我很多。
用户信息终结点(由微服务使用)
客户端(如移动前端)可以使用以下方式进行身份验证: POST oauth/token 并提供 grant_type=password 以及用户名和密码。
服务器可以使用“oauth/authorize”进行授权
基本的身份验证支持与不同的权力也可以作为我可以填写用户名+密码到 oauth_client_details 表格:

select client_id, access_token_validity, authorities, authorized_grant_types, refresh_token_validity, scope from oauth_client_details;
     client_id     | access_token_validity |          authorities          |          authorized_grant_types           | refresh_token_validity |  scope
-------------------+-----------------------+-------------------------------+-------------------------------------------+------------------------+---------
 backend           |                864000 | mail,push,app-register        | mail,push,client_credentials              |                 864000 | backend
 app               |                864000 | grant                         | client_credentials,password,refresh_token |                      0 | app

如果还没有oauth令牌,应用程序就会使用它。
其他微服务也使用此功能来保护其端点—例如在本例中:

@Configuration @EnableResourceServer class ResourceServerConfig : ResourceServerConfigurerAdapter() {
  override fun configure(http: HttpSecurity) {
    http.authorizeRequests()
        // Coach.
        .antMatchers("/api/my-api/**").hasRole("my-role")
        .antMatchers("/registration/**").hasAuthority("my-authority")
  }
}

他们的设置非常简单:

security.oauth2.client.accessTokenUri=http://localhost:20200/oauth/token
security.oauth2.client.userAuthorizationUri=http://localhost:20200/oauth/authorize
security.oauth2.resource.userInfoUri=http://localhost:20200/oauth/user/me
security.oauth2.client.clientId=coach_client
security.oauth2.client.clientSecret=coach_client

前三个属性直接进入我的授权服务器。最后两个属性是我在 oauth_client_details table。当我的微服务想要与它使用的另一个微服务对话时:

val details = ClientCredentialsResourceDetails()
details.clientId = "" // Values from the properties file.
details.clientSecret = "" // Values from the properties file.
details.accessTokenUri = "" // Values from the properties file.
val template = OAuth2RestTemplate(details)
template.exchange(...)

现在我的问题是-如何使用SpringBoot获得SpringSecurity的内置支持?我想从不推荐使用的软件包中迁移出来,并保留所有令牌,这样用户以后仍然可以登录。

bqf10yzr

bqf10yzr1#

我们还运行了一个spring安全授权服务器并对此进行了研究。目前,在spring中没有替代authorization server组件的工具,而且似乎也没有实现一个组件的时间表。最好的选择是查看现有的auth组件,如keydepot或nimbus。或者有托管服务,如okta或auth0。
保留现有的令牌将是一个挑战,因为您需要将它们导入到新的解决方案中。我们当前的令牌是不透明的,而较新的auth解决方案倾向于使用jwt的某些版本,因此根据您的令牌,保留它们甚至可能不是一种选择。
现在我们考虑接受新旧代币一段时间,直到旧代币的生存时间结束,此时我们将完全转移到新的基础设施。

相关问题