2017-04-20 1 views
10

봄에 SwitchUserFilter을 사용하여 가장을 구현하려고하는데 오류가 발생합니다. 이 구현 없이는 프로젝트가 제대로 실행됩니다. 또한이 프로젝트는 xml 구성이 아닌 Java 구성을 사용하며 SecureAuth 인증을 사용합니다. 그리고의 SecurityConfig 클래스에 코드에 관련된 부분은 다음과 같습니다가장을 java.lang.IllegalStateException : UserDetailsService가 필요합니다.

@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가 중지 :

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

당신이 경우 코드의 더 많은 부분, 공유의 즐거움이 필요합니다.

+0

@dur 내가 디버깅하고 평가할 때 예'super.userDetailsServiceBean()가'포함'분야에서 null'가' –

+0

을 delegate' 당신은 구성하지 않은 'UserDetailsService'이므로'null'입니다. 하나 구성해야합니다. – dur

+0

죄송합니다. 저는 Spring Security SAML에 대한 경험이 없으므로,'UserDetailsService'를 설정하는 방법을 모르겠습니다. 어쩌면이 [link] (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

여전히 작동하지 않습니다. 'configure (AuthenticationManagerBuilder auth)'에'auth.userDetailsService (userService);'를 추가하면이 질문에 나를 리디렉션하는 다른 오류'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 메소드를 구현하는 다른 클래스에서 내 UserDetailsService 구현을 선언합니다. – Erwin

관련 문제