2016-03-21 18 views
0

ユーザーを認証するために追加の制限を加える必要があります。私のユーザモデルは、フィールド 'アクティブ'を持っています。ユーザーが登録したがメールからのハッシュを使用してアカウントをアクティブ化していない場合はfalseです。今、Userがアクティブでない場合でも、彼はOauthからaccess_tokenを取得します。 これはどのように設定する必要がありますか? 私はSpringSecurityInterceptorについて考えていましたが、Spring SecurityとOAuth2を混同しているかどうかはわかりません。 これは私のSpringOAuth2.0構成です:Spring OAuth2追加のパーマネント

@Configuration 
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter { 

    private static final String RESOURCE_ID = "restservice"; 

@Configuration 
@EnableResourceServer 
protected static class ResourceServerConfiguration extends 
     ResourceServerConfigurerAdapter { 


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

    @Override 
    public void configure(HttpSecurity http) throws Exception { 
     http 
       .requestMatcher(new OrRequestMatcher(
         new AntPathRequestMatcher("/rest/**") 
       )) 
       .authorizeRequests() 

       .anyRequest().access("#oauth2.hasScope('read')"); 
    } 

} 


@Configuration 
@EnableAuthorizationServer 
protected static class AuthorizationServerConfiguration extends 
     AuthorizationServerConfigurerAdapter { 

    private TokenStore tokenStore = new InMemoryTokenStore(); 

    @Autowired 
    @Qualifier("authenticationManagerBean") 
    private AuthenticationManager authenticationManager; 

    @Autowired 
    UserDetailsService userDetailsService; 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) 
      throws Exception { 
     endpoints 
       .tokenStore(this.tokenStore) 
       .authenticationManager(this.authenticationManager) 
       .userDetailsService(userDetailsService) 
       .pathMapping("/oauth/token", "/rest/oauth/token"); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients 
       .inMemory() 
       .withClient("clientapp") 
       .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit") 
       .authorities("USER") 
       .scopes("read", "write", "trust") 
       .resourceIds(RESOURCE_ID) 


        .secret("123456"); 
     } 

    } 

} 

も何かアドバイスが参考になる

@Configuration 
@Order(2147483640) 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    DataSource dataSource; 

    @Autowired 
    UserDetailsService userDetailsService; 

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

     http 
       .authorizeRequests() 
       .antMatchers("/user/**").authenticated() 
       .anyRequest().permitAll() 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .usernameParameter("email") 
       .passwordParameter("password") 
       .defaultSuccessUrl("/user/") 
       .successHandler(successHandler()) 
       .and() 
       .logout() 
       .logoutUrl("/logout") 
       .logoutSuccessUrl("/") 
       .and() 
       .rememberMe() 
       .tokenRepository(persistentTokenRepository()) 
       .tokenValiditySeconds(86400) 
       .and() 
       .csrf().disable(); 
    } 

    @Bean 
    public AuthenticationSuccessHandler successHandler() { 
     return new UserLoginSuccessHandler(); 
    } 

    @Bean 
    public PersistentTokenRepository persistentTokenRepository() { 
     JdbcTokenRepositoryImpl tokenRepositoryImpl = new JdbcTokenRepositoryImpl(); 
     tokenRepositoryImpl.setDataSource(dataSource); 
     return tokenRepositoryImpl; 
    } 

    @Bean 
    public SpringSecurityDialect securityDialect() { 
     return new SpringSecurityDialect(); 
    } 

    @Autowired 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService) 
       .passwordEncoder(new BCryptPasswordEncoder()); 
    } 

    @Override 
    @Bean 
    public AuthenticationManager authenticationManagerBean() throws Exception { 

     return super.authenticationManagerBean(); 
    } 
} 

セキュリティ春。

答えて

0

あなたが正しく理解していれば、認証サーバーはアクティブ化されていないユーザーのアクセストークンを許可したくありませんか?

UserDetailsService.loadUserByUsernameにはUsernameNotFoundExceptionがありますが、ユーザーが存在する場合は有効にしますが、有効にすることはできません。

+0

ありがとう、私はすでにUserDetailsインターフェイスでそれを行っています。今、私のUserエンティティはこのインタフェースを実装しています。ユーザーがアクティブかどうか、またアカウントの有効期限が切れていないかどうかを確認する方法があります。 – rpieniazek

+0

あなたはそうです、それははるかに優れています! – Tom

関連する問題