2015-08-18 3 views
0

У меня проблема с пружинным автоуведомлением. Я пытаюсь использовать Autowire для ввода репозитория, который, кстати, уже работает, когда я ввожу его в контроллер, в службу аутентификации. Для получения дополнительной информации я добавляю соответствующий код и ошибку.Весна не удается автоподтвердить репозиторий

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <display-name>Restful Web Application</display-name> 


    <context-param> 
     <param-name>contextClass</param-name> 
     <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
    </context-param> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      enterprise.util.SpringSecurityConfig 
     </param-value> 
    </context-param> 

    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>REQUEST</dispatcher> 
    </filter-mapping> 


    <listener> 
     <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
     </listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>restEnterprise</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>restEnterprise</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 




</web-app> 

restEnterpise-servlet.xml - EmployeeRepository в dbService

<beans xmlns="http://www.springframework.org/schema/beans" 

    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <context:component-scan base-package="enterprise.service" /> 
    <context:component-scan base-package="enterprise.controller" /> 
    <context:component-scan base-package="enterprise.util" /> 
    <context:component-scan base-package="dbService" /> 

    <mvc:annotation-driven /> 

</beans> 

SpringSecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { 

    private final TokenAuthenticationService tokenAuthenticationService; //handles adding auth token to response and checking for auth header in requests 
    private final EmployeeDetailsService employeeDetailsService; 
    public SpringSecurityConfig() { 
     super(true); 
     tokenAuthenticationService = new TokenAuthenticationService("tooManySecrets"); 
     employeeDetailsService = new EmployeeDetailsService(); 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web 
     .debug(true); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .exceptionHandling().and() 
       .anonymous().and() 
       .servletApi().and() 
       .headers().cacheControl().and().and() 
       .authorizeRequests() 

       // Allow anonymous logins 
       .antMatchers("/auth/**").permitAll() 

       // All other request need to be authenticated 
       .anyRequest().authenticated().and() 

       // Custom Token based authentication based on the header previously given to the client 
       .addFilterBefore(new StatelessAuthenticationFilter(tokenAuthenticationService), UsernamePasswordAuthenticationFilter.class); 
    } 

//http://docs.spring.io/spring-security/site/docs/current/apidocs/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.html 

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

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

    @Bean 
    @Override 
    public EmployeeDetailsService userDetailsService() { 
     return employeeDetailsService; 
    } 

//-------------- 

    @Bean 
    public TokenAuthenticationService tokenAuthenticationService() { 
     return tokenAuthenticationService; 
    } 
} 

EmployeeDetailsService.java

@Component 
public class EmployeeDetailsService implements org.springframework.security.core.userdetails.UserDetailsService { 

    @Autowired 
    private EmployeeRepository employeeRep; 

    @Override 
    public final UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
     Employee employee = employeeRep.findByLogin(username); 
     if (employee == null) { 
      throw new UsernameNotFoundException("Employee not found"); 
     } 
     List<GrantedAuthority> authorities = buildUserAuthority(employee.getRole()); 
     return buildUserForAuthentication(employee, authorities); 
    } 

    // Converts Employee to 
    // org.springframework.security.core.userdetails.User 
    private User buildUserForAuthentication(Employee user, 
     List<GrantedAuthority> authorities) { 
     return new User(user.getLogin(), user.getPassword(), 
      true, true, true, true, authorities); 
    } 

    private List<GrantedAuthority> buildUserAuthority(String userRole) { 

     Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>(); 

     setAuths.add(new SimpleGrantedAuthority("ROLE_" + userRole.toUpperCase())); 

     List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths); 

     return Result; 
    } 

} 

Ошибка при развертывании

2015-08-18 10:15:34,389 ERROR [org.jboss.as.controller.management-operation]  (management-handler-thread - 1) JBAS014613: Operation ("redeploy") failed - address: ([("deployment" => "enterprise.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.undertow.deployment.default-server.default-host./enterprise" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./enterprise: Failed to start service 
    Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private dbService.dao.EmployeeRepository enterprise.service.EmployeeDetailsService.employeeRep; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [dbService.dao.EmployeeRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 

Как я уже сказал - это autowire работает, когда я использую его в контроллере, но не в EmployeeDetailsService.

+0

Где класс, который реализует/продолжается 'dbService.dao.EmployeeRepository'? – Aakash

+0

Является ли 'dbService.dao.EmployeeRepository' аннотированным как репозиторий? – Jens

+0

Почему вы используете новый экземпляр EmployeeDetailsService? Использование new создает экземпляр вне весеннего контейнера – jozzy

ответ

1

Вместо того чтобы создавать экземпляр EmployeeService с использованием нового, вы должны позволить контейнеру с пружиной выполнить экземпляр и проводку.

Вы можете autowire свой EmployeeService в

@AutoWired 
private final EmployeeDetailsService employeeDetailsService; 

и удалить новый Instantiation из конструктора, а также удалить @Bean аннотацию из userDetailsService() метода

Обновлено

Если у вас есть главная пружина контекст xml импортирует его в класс конфигурации, как показано ниже:

@Configuration 
@ImportResource("restEnterpise-servlet.xml") 
@EnableWebSecurity 
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter 

Если у вас нет каких-либо пружинные XML, вы можете добавить componentscan аннотацию, чтобы обнаружить пружинные компоненты @ComponentScan

+0

Когда я исправляю, я получаю ошибку инициализации «ERROR Context»: org.springframework.beans.factory.BeanCreationException: ошибка при создании bean-компонента с именем «springSecurityConfig»: не удалось выполнить инъекцию автоуведомленных зависимостей; (...) Не удалось создать поле autowire: private enterprise.service.EmployeeDetailsService enterprise.util.SpringSecurityConfig.employeeDetailsService; (...) Не найден квалифицированный компонент типа [enterprise.service.EmployeeDetailsService] для зависимостей: (...) 'Кажется, что Г. Трубач прав. Это проблема с загрузкой. –

+0

Спасибо вам большое! Ваше обновление и оригинальное сообщение помогли мне решить эту проблему –

0

Если Repository расширяет JpaRepository, то вам нужно использовать @Resource вместо @Autowired в вашем EmployeeDetailsService

+0

Репозиторий расширяет CRUDRepository –

0
This link will be helpfull

.

Ваши услуги объявлены в restEnterpise-servlet.xml и вы хотите использовать EmployeeRepository. Но настройки безопасности загружены до конфигурации сервлета. Таким образом, вам нужно создать еще одну конфигурацию Spring, где будет объявлено все сканирование компонентов и которое будет загружено до конфигурации безопасности. Например spring-config.xml

<beans xmlns="http://www.springframework.org/schema/beans" 

    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 

    <context:component-scan base-package="enterprise.service" /> 
    <context:component-scan base-package="enterprise.controller" /> 
    <context:component-scan base-package="enterprise.util" /> 
    <context:component-scan base-package="dbService" /> 

    <mvc:annotation-driven /> 

</beans> 

web.xml В

<context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring-config.xml 
      enterprise.util.SpringSecurityConfig 
     </param-value> 
    </context-param> 

Я надеюсь, что это поможет.

+0

Да, может быть. Проблема в том, что когда я использую ваше решение, я получаю '2015-08-18 12: 06: 49,467 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (нить службы службы MSC 1-1). Аннотированные классы не найдены для указанного класса/пакета [/ WEB-INF/spring-beforeSecurityLoad.xml'. Вы знаете, почему он ничего не может найти? –

+0

Что находится в 'spring-beforeSecurityLoad.xml'? Попытайтесь прочитать это [сообщение] (http://stackoverflow.com/questions/8075790/how-to-register-spring-configuration-annotated-class-instead-of-applicationcont). –

+0

Я просто скопировал и вставил компонентные проверки из конфигурации сервлета. –

Смежные вопросы