2017-04-20 1 views
10

私は春にSwitchUserFilterを使用して偽装を実装しようとしていますが、エラーが発生しています。プロジェクトはこの実装がなくてもうまくいく。また、プロジェクトでは、xml構成ではなくJava構成を使用しており、SecureAuth認証を持っています。そしてSecurityConfigクラスにコードに関わる部分がある:あなたの場合偽装java.lang.IllegalStateException:UserDetailsS​​erviceが必要です

http://localhost:8080/switch?j_username=angel_cuenca 

@Configuration 
@ComponentScan(basePackages = {"com.project.*"}) 
@EnableWebMvcSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true) 
@PropertySource("classpath:app.properties") 
@Import({TransactionManagersConfig.class, MailConfig.class}) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private SwitchUserFilter switchUserFilter; 

    @Autowired 
    protected AuthenticationSuccessHandler authenticationSuccessHandler; 

    @Bean 
    public UserDetailsService userDetailsServiceBean() { 
    try { 
     return super.userDetailsServiceBean(); 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
    } 

    @Bean 
    public SwitchUserFilter switchUserFilter() { 
    SwitchUserFilter switchUserFilter = new SwitchUserFilter(); 
    switchUserFilter.setUserDetailsService(userDetailsServiceBean()); 
    switchUserFilter.setUsernameParameter("username"); 
    switchUserFilter.setSwitchUserUrl("/switch"); 
    switchUserFilter.setExitUserUrl("/exit"); 
    switchUserFilter.setTargetUrl("/"); 

    return switchUserFilter; 
    } 

    //more beans 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .headers().disable(); 
     http //SAML CONFIG 
       .httpBasic() 
       .authenticationEntryPoint(samlEntryPoint()).and() 
       .addFilterBefore(metadataGeneratorFilter(), ChannelProcessingFilter.class) 
       .addFilterAfter(samlFilter(), BasicAuthenticationFilter.class); 
     http //DISABLE CROSS-SITE REQUEST FORGERY 
       .csrf() 
       .disable(); 
       //Impersonate Interceptor 
     http 
       .addFilterAfter(switchUserFilter(), FilterSecurityInterceptor.class); 
     http 
       .authorizeRequests() 
       .antMatchers("/impersonate").permitAll() 
       .antMatchers("/api/**").permitAll() 
       .antMatchers("/#/**").permitAll() 
       .antMatchers("/switch").permitAll() 
       .antMatchers("/login").permitAll() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin().loginPage("/index") 
       .permitAll().successHandler(authenticationSuccessHandler); 
     http 
       .logout().logoutSuccessUrl(env.getProperty("realm.url.restart")); 
     http 
       .exceptionHandling().accessDeniedPage("/error?code=403&error=Access Denied&detail=You are not authorized to access."); 

    }  

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

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .authenticationProvider(samlAuthenticationProvider()); 
    } 

    @Override 
    public void configure(WebSecurity webSecutity) throws Exception { 
     webSecutity 
       .ignoring().antMatchers("/resources/**"); 
    } 
} 

コンプリートクラスSecurityConfig.java

エラー:

java.lang.IllegalStateException: UserDetailsService is required. 
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:393) 
    at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.attemptSwitchUser(SwitchUserFilter.java:209) 
    at org.springframework.security.web.authentication.switchuser.SwitchUserFilter.doFilter(SwitchUserFilter.java:155) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at 

私のURLは上で停止しますコードのより多くの部分、共有する喜びが必要です。

+0

@durはい]を、私はデバッグと 'super.userDetailsS​​erviceBean(評価時)' 'あなたが設定していなかった分野' delegate' –

+0

でnull'なのでが含まれています'UserDetailsS​​ervice'なので、' null'です。設定する必要があります。 – dur

+0

申し訳ありませんが、私はSpring Security SAMLに関する経験がないので、 'UserDetailsS​​ervice'の設定方法はわかりません。多分この[リンク](https://www.dontpanicblog.co.uk/2014/05/07/saml-based-single-sign-on-sosoin-spring-security-applications/)が役立ちます。 – dur

答えて

0

thisのような設定にuserDetailsServiceの実装を設定しようとしますか?

私はあなたの設定で表示されていない。

auth.userDetailsService(userService); 
+0

まだ動作しません。 'auth.userDetailsS​​ervice(userService);'を 'configure(AuthenticationManagerBuilder auth)'に追加すると、別のエラー 'switch java.lang.StackOverflowError'が返ってきます。これは私をこの質問にリダイレクトします(http://stackoverflow.com/questions/30766106/spring-security-java-lang-stackoverflowerror-exception-after-all-providers)、このエラーの解決策が削除されました。最近追加されたuserDetailService。だから私は知らない.. –

+0

確かに。ここには、私の現在のコード(https://github.com/angelcuenca/stackoverflow/blob/master/SecurityConfig.java)の528行目が表示されています。何が問題なのでしょうか? –

+0

あなたが提供するリンクと同じ問題だと思います。私は、loadUserByUsernameメソッドを実装する別のクラスでUserDetailsS​​ervice実装を宣言します。 – Erwin

関連する問題