代码之家  ›  专栏  ›  技术社区  ›  Daniel Donev

java Spring@EnableResourceServer和@EnableWebSecurity

  •  4
  • Daniel Donev  · 技术社区  · 7 年前

    我有 宁静的 spring资源服务器 @EnableResourceServer 和扩展 ResourceServerConfigurerAdapter

    在里面 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();
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Oreste Viron    7 年前

    否,启用EnableWebSecurity是隐式的。

    我不建议使用WebSecurity配置适配器,您会遇到一些问题: Correctly configure spring security oauth2

        2
  •  1
  •   jzheaux Andrii Pischanski    7 年前

    Spring Boot makes @EnableWebSecurtiy implicit ,但在其他方面是必需的。

    你可以通过看看这个来证明这一点 OAuth2 resource server example 。如果您删除 @EnableWebSecurity 注释中,您会发现Spring安全过滤器链没有连接。

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