Spring Security+OAuth2问题

来源:11-11 数据库版本的授权服务器

开辟者

2023-03-23

一个资源服务器随便也有几百个接口吧

资源服务器配置:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
             .authorizeHttpRequests((authorize) -> authorize
                     .requestMatchers(HttpMethod.GET, "/api/get1").hasAuthority("SCOPE_api.get1")
                     .requestMatchers(HttpMethod.POST, "/api/post1").hasAuthority("SCOPE_api.post1")
                     .requestMatchers(HttpMethod.GET, "/api/get2").hasAuthority("SCOPE_api.get2")
                     .requestMatchers(HttpMethod.POST, "/api/post2").hasAuthority("SCOPE_api.post2")
                     .requestMatchers(HttpMethod.GET, "/api/get3").hasAuthority("SCOPE_api.get3")
                     .requestMatchers(HttpMethod.POST, "/api/post3").hasAuthority("SCOPE_api.post3")
                     // ...
                     .requestMatchers(HttpMethod.GET, "/api/get100").hasAuthority("SCOPE_api.get100")
                     .requestMatchers(HttpMethod.POST, "/api/post100").hasAuthority("SCOPE_api.post100")
                     .anyRequest().authenticated()
             )
             .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);

        return http.build();
    }

}

授权服务器:

@Configuration(proxyBeanMethods = false)
public class AuthorizationServerConfig {

    // ...

    @Bean
    public RegisteredClientRepository registeredClientRepository(JdbcTemplate jdbcTemplate) {
        RegisteredClient registeredClient = RegisteredClient.withId(UUID.randomUUID().toString())
                .clientId("messaging-client")
                .clientSecret("{noop}secret")
                .clientAuthenticationMethod(ClientAuthenticationMethod.CLIENT_SECRET_BASIC)
                .authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
                .authorizationGrantType(AuthorizationGrantType.REFRESH_TOKEN)
                .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS)
                .redirectUri("http://127.0.0.1:8080/login/oauth2/code/messaging-client-oidc")
                .redirectUri("http://127.0.0.1:8080/authorized")
                .redirectUri("https://www.baidu.com")
                .scope(OidcScopes.OPENID)
                .scope(OidcScopes.PROFILE)
                .scope("api.get1")
                .scope("api.post1")
                .scope("api.get2")
                .scope("api.post2")
                .scope("api.get3")
                .scope("api.post3")
                // ...
                .scope("api.get100")
                .scope("api.post100")
                .clientSettings(ClientSettings.builder().requireAuthorizationConsent(false).build())
                .build();

        JdbcRegisteredClientRepository registeredClientRepository = new JdbcRegisteredClientRepository(jdbcTemplate);
        registeredClientRepository.save(registeredClient);

        return registeredClientRepository;
    }


    // ...

}

这样一来获取 access_token 会非常恐怖呀,再说,这还只是一个资源服务器,要是多个,这个 scope 不会更多, access_token 会很大很大呀,用户一多那这个 registeredClient 表也非常大呀,这样正常吗?是怎么做的吗?

写回答

1回答

接灰的电子产品

2023-03-24

首先 scope 的数量不会很大,多说也就百八十个,你可以看看 GitHub

registerclient 和用户数没什么关系啊,这个是指的比如某个 app 或者网站应用,一般除非你做到微信登录这个级别,n 多第三方使用你登录才会有量级的问题,否则在一般公司,这个量级最多也就百八十个,没什么问题啊

如果涉及到量级的问题,自然可以通过缓存和分库来处理,但这个和安全没关系

0
0

Spring Security+OAuth2 精讲,打造企业级认证与授权

一站式掌握主流安全框架与行业解决方案,从容应对各种安全难题。

1042 学习 · 316 问题

查看课程