【问题标题】:Spring OAuth2 multi Server annotations configuration (resource & authorization)Spring OAuth2 多服务器注解配置(资源&授权)
【发布时间】:2015-11-19 07:48:27
【问题描述】:

我正在使用以下内容:

  • 弹簧 4.2
  • 弹簧安全 4.0.2
  • spring oauth2 2.0.7

我正在尝试配置一个服务器来处理:

  • 一般 MVC 内容(有些受保护,有些不受保护)
  • 授权服务器
  • 资源服务器

似乎资源服务器配置不限于 /rest/** 而是覆盖所有安全配置。即对受保护的 NON-OAuth 资源的调用未受到保护(即过滤器未捕获它们并重定向到登录)。

配置(为了简单起见,我删除了一些东西):

    @Configuration
    @EnableResourceServer
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter  {



        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources) {
            resources
                .resourceId(RESOURCE_ID)
                .tokenStore(tokenStore)
                .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                .requestMatchers()
                    .antMatchers("/rest/**")
                    .and()
                .authorizeRequests()
                    .antMatchers("/rest/**").access("hasRole('USER') and #oauth2.hasScope('read')");

        }

    }

@Configuration
@EnableWebSecurity
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();

    }
   @Bean
    protected AuthenticationEntryPoint authenticationEntryPoint() {
        OAuth2AuthenticationEntryPoint entryPoint = new OAuth2AuthenticationEntryPoint();
        entryPoint.setRealmName("example");
        return entryPoint;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth
            .authenticationProvider(mongoClientAuthenticationProvider)
            .authenticationProvider(mongoUserAuthenticationProvider)
            .userDetailsService(formUserDetailsService);
    }

    @Bean
    protected ClientCredentialsTokenEndpointFilter clientCredentialsTokenEndpointFilter() throws Exception{
        ClientCredentialsTokenEndpointFilter filter = new ClientCredentialsTokenEndpointFilter();
        filter.setAuthenticationManager(authenticationManagerBean());
        filter.afterPropertiesSet();
        return filter;
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
        .requestMatchers()
            .antMatchers("/account/**", "/account")
            .antMatchers("/oauth/token")
            .antMatchers("/login")
            .and()
        .authorizeRequests()
            .antMatchers("/account/**", "/account").hasRole("USER")
            .antMatchers("/oauth/token").access("isFullyAuthenticated()")
            .antMatchers("/login").permitAll()
            .and()
        .exceptionHandling()
            .accessDeniedPage("/login?authentication_error=true")
            .and()
        .csrf()
            .disable()
        .logout()
            .logoutUrl("/logout")
            .invalidateHttpSession(true)
            .and()
        .formLogin()
            .loginProcessingUrl("/login")
            .failureUrl("/login?authentication_error=true")
            .loginPage("/login")
        ;

        http.addFilterBefore(clientCredentialsTokenEndpointFilter(), BasicAuthenticationFilter.class);

    }

【问题讨论】:

    标签: spring spring-security spring-security-oauth2


    【解决方案1】:

    您正在使用多个HttpSecurity 配置。 Spring需要知道顺序。使用 @Order 注释您的 SecurityConfig

    @Configuration
    @EnableWebSecurity
    @Order(4)
    public class SecurityConfig  extends WebSecurityConfigurerAdapter{}
    

    注解@EnableResourceServer 创建一个带有硬编码Order(3 个)的WebSecurityConfigurerAdapter。由于 Spring 的技术限制,目前无法更改顺序,因此您必须避免在应用程序的其他 WebSecurityConfigurerAdapter 中使用 order=3(如果您忘记了,Spring Security 会通知您)。

    参考:

    http://docs.spring.io/spring-security/site/docs/3.2.x/reference/htmlsingle/#multiple-httpsecurity

    http://docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/config/annotation/web/configuration/EnableResourceServer.html

    【讨论】:

    • 我已经尝试过订单,但没有成功。您需要注意资源类 (EnableResourceServer) 没有扩展 WebSecurityConfigurerAdapter。另外,你将如何决定哪一个是第一个?我需要它们都匹配。
    • @EnableResourceServer 将使用 order=3 自动创建 WebSecurityConfigurerAdapter。
    【解决方案2】:

    解决方案是你应该使用follow lib(s)版本,否则你会遇到这个问题。希望这个帮助。你不能使用spring-security 4.0.2版本。 spring-security-acl-3.2.7.RELEASE.jar spring-security-config-3.2.7.RELEASE.jar spring-security-core-3.2.7.RELEASE.jar spring-security-oauth2-2.0.7.RELEASE.jar spring-security-taglibs-3.2.7.RELEASE.jar

    【讨论】:

    • 能否解释一下答案的原因和依据?
    猜你喜欢
    • 2016-05-21
    • 2014-07-09
    • 2016-12-12
    • 2018-08-29
    • 2015-05-14
    • 2022-08-03
    • 2021-10-29
    • 2019-02-22
    • 2022-11-23
    相关资源
    最近更新 更多