2015-02-07 14 views
6

Praca nad przejściem od konfiguracji xml bezpieczeństwa wiosennego do Java Config w Spring Security.Wiosenne uruchamianie, nadpisanie zabezpieczenia sprężyny UserDetailsService

W mojej klasie SecurityConfiguration, która rozszerza WebSecurityConfigurerAdapter. Jednak problem polega na tym, że parametr userDetailsService nie jest używany przez filtry bezpieczeństwa, w szczególności właściwość UsernamePasswordAuthenticationFilter. Spojrzałem na startup i wygląda na to, że nie jest on tworzony, zanim Spring boots stworzy domyślny InMemoryUserDetailsManager.

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

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

     http.userDetailsService(userDetailsService); 

    } 
} 

Próbowałem również przesłonić userDetailsServiceBean i userDetailsService w tej klasie przy użyciu niestandardowych wstrzykuje ApplicationUserDetailsService.

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

@Override 
public UserDetailsService userDetailsService() { 

    return userDetailsService; 
} 

Jednak gdy próbuję zastąpić authenticationManagerBean wygląda na to wywołuje moją konfigurację przed inicjuje konfiguracja rozruchu wiosna ale zgłasza błąd (poniżej), że istnieje odwołanie cykliczne podczas inicjalizacji UsernamePasswordAuthenticationFilter. Czy naprawdę potrzebuję zastąpić uwierzytelnianieManagerBean, ponieważ muszę zdefiniować, co idzie do UsernamePasswordAuthenticationFilter.

@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 

Pomysły?

Odpowiedz

0

Hi istnieje prosty sposób zastąpić UserDetailsService

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

Jest to ważne: @Component ("userDetailsService")

Dzięki Aleksandar

Powiązane problemy