簡體   English   中英

模擬 java.lang.IllegalStateException: UserDetailsS​​ervice 是必需的

[英]Impersonate java.lang.IllegalStateException: UserDetailsService is required

我正在嘗試在 Spring 中使用SwitchUserFilter實現模擬,但出現錯誤。 沒有這個實現,項目運行良好。 此外,該項目使用的是 Java 注釋而不是 xml 配置,並且具有 SecureAuth 身份驗證。 而代碼中涉及到SecurityConfig類的部分是:

@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/**");
    }
}

錯誤:

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

我的網址停在:

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

如果您需要更多部分代碼,很高興分享。

您可以嘗試將userDetailsService實現設置為配置,就像這樣嗎?

我在你的配置中沒有看到:

auth.userDetailsService(userService);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM