2015-02-07 4 views
6

스프링 보안 XML 구성에서 스프링 보안의 Java 구성으로 이동 중.스프링 부트, 스프링 보안 오버라이드 UserDetailsService

내 클래스에서 WebSecurityConfigurerAdapter를 확장하는 SecurityConfiguration입니다. 그러나 문제는 userDetailsService가 보안 필터, 특히 UsernamePasswordAuthenticationFilter에 의해 사용되고 있지 않다는 것입니다. 스타트 업을 살펴보면 스프링 부트가 기본 InMemoryUserDetailsManager를 생성하기 전에 생성되지 않은 것으로 보입니다.

@Configuration 
@EnableWebMvcSecurity 
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 

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

     http.userDetailsService(userDetailsService); 

    } 
} 

는 또한 ApplicationUserDetailsService 주입 된 사용자 정의를 사용하여이 클래스의 userDetailsServiceBean와 UserDetailsService도를 무시하는 것을 시도했다. 나는 authenticationManagerBean를 오버라이드 (override) 할 때

@Bean(name="myUserDetailsBean") 
@Override 
public UserDetailsService userDetailsServiceBean() { 
    return userDetailsService; 
} 

@Override 
public UserDetailsService userDetailsService() { 

    return userDetailsService; 
} 

그러나, 봄 부팅 구성을 초기화하기 전에 내 구성을 호출하는 것 같습니다하지만 오류 (아래) UsernamePasswordAuthenticationFilter을 초기화 할 때 순환 참조가 있음을 던졌습니다. UsernamePasswordAuthenticationFilter에 들어가는 것을 정의해야하기 때문에 실제로 authenticationManagerBean을 재정의해야 할 필요가 있습니까?

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

..

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter]: Circular reference involving containing bean 'securityBeansConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'usernamePasswordAuthenticationFilter' threw exception; nested exception is java.lang.IllegalArgumentException: successHandler cannot be null 
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
... 70 common frames omitted 

아이디어? UserDetailsService도를 무시하는 간단한 방법이

답변

0

안녕하세요

import com.dog.care.domain.User; 
import com.dog.care.repository.UserRepository; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 
import org.springframework.stereotype.Component; 
import org.springframework.transaction.annotation.Transactional; 

import javax.inject.Inject; 
import java.util.Optional; 

@Component("userDetailsService") 
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService { 

private final Logger log = LoggerFactory.getLogger(UserDetailsService.class); 

@Inject 
private UserRepository userRepository; 

@Override 
@Transactional 
public UserDetails loadUserByUsername(final String login) { 
    log.debug("Authenticating {}", login); 
    String lowercaseLogin = login.toLowerCase(); 
    Optional<User> userFromDatabase = userRepository.findOneByLogin(lowercaseLogin); 
    return userFromDatabase.map(user -> { 
     if (!user.getActivated()) { 
      throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated"); 
     } 
     return new CustomUserDetails(user); 
    }).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the database")); 
} 
} 

이 중요하다 @Component ("UserDetailsService의")

감사 알렉산다르

관련 문제