【问题标题】:java Spring @EnableResourceServer and @EnableWebSecurityjava Spring @EnableResourceServer 和 @EnableWebSecurity
【发布时间】:2023-03-19 17:03:08
【问题描述】:

我有 RESTful 带有 @EnableResourceServer 并扩展 ResourceServerConfigurerAdapter 的 spring 资源服务器

documentations 中说:

...为了使用这个过滤器,你必须在你的应用程序的某个地方@EnableWebSecurity,或者在你使用这个注解的地方,或者在其他地方。

但是当我到达public @interface EnableResourceServer 时,我看到了ResourceServerConfiguration extends WebSecurityConfigurerAdapter

问题: 那么纯 RESTful API 需要什么?

  1. @EnableWebSecurity 在任何 @Config
  2. 扩展WebSecurityConfigurerAdapter?
  3. 1 + 2
  4. 都没有

我的配置

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class HGResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
        .cors().disable()
        .csrf().disable()
        .formLogin().disable()
        .httpBasic().disable()
        .jee().disable()
        .logout().disable()
        .rememberMe().disable()
        .servletApi().disable()
        .x509().disable()
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        .and().authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
        .and().authorizeRequests().antMatchers(Url.API_ERROR_LOGS_FRONTEND).permitAll()
        .and().authorizeRequests().antMatchers(Url.API_REGISTER_PATH).permitAll()
        .and().authorizeRequests().antMatchers(Url.API_VERIFY_EMAIL_PATH).permitAll()
        .and().authorizeRequests().antMatchers(Url.API_RESET_PASSWORD_PATH).permitAll()
        .and().authorizeRequests().antMatchers(Url.API_CONFIRM_RESET_PASSWORD_PATH).permitAll()
        .and().authorizeRequests().anyRequest().authenticated();
    }

    @Primary
    @Bean
    public RemoteTokenServices tokenService() {
        RemoteTokenServices tokenService = new RemoteTokenServices();
        tokenService.setCheckTokenEndpointUrl("http://localhost:8081/oauth/check_token");
        tokenService.setClientId("client");
        tokenService.setClientSecret("secret");
        return tokenService;
    }


    //disable default user creation
    @Bean
    public UserDetailsService userDetailsService() throws Exception {
        return new InMemoryUserDetailsManager();
    }

    //password encoder
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

【问题讨论】:

    标签: java spring spring-boot spring-security spring-security-oauth2


    【解决方案1】:

    不,启用 EnableWebSecurity 是隐式的。

    我不推荐使用WebSecurityConfigurerAdapter,你会遇到一些麻烦: Correctly configure spring security oauth2

    【讨论】:

    • @EnableResourceServer 旨在用作 Restful Api。如果您不进行任何配置,您的 WS 将只能由经过身份验证的用户访问。在你的情况下可能没问题。但是你可以有一些公开的并且每个人都可以访问的 WS。在这种情况下,您需要像在问题中那样实现 ResourceServerConfigurerAdapter 以允许访问这些 WS。
    • 那么选项4是推荐的吗?
    • 是的,只使用 EnableResourceServer 和 ResourceServerConfigurerAdapter。
    【解决方案2】:

    Spring Boot makes @EnableWebSecurtiy implicit,否则是必需的。

    您可以通过查看OAuth2 resource server example 来证明这一点。如果你去掉那里的@EnableWebSecurity注解,你会发现Spring Security Filter Chain没有连线。

    您仍然可以扩展 WebSecurityConfigurerAdapter 以将一般 Web 应用程序安全问题与特定于资源服务器配置的问题分开。这在技术上不是必需的,但可以用于a cleaner separation of concerns

    【讨论】:

    • 在提供的示例中,应用服务器同时作为身份验证和资源服务器。就我而言,它只是资源服务器。
    猜你喜欢
    • 2017-10-28
    • 2017-08-13
    • 2016-08-31
    • 2017-11-24
    • 2018-08-16
    • 2016-07-20
    • 2020-07-20
    • 2019-01-05
    • 2015-06-25
    相关资源
    最近更新 更多