无法自动连线jparepository介面

时间:2019-04-18 11:03:24

标签: spring spring-security spring-data-jpa

Hi Guys iam使用spring创建了一个Web应用程序。.iam卡在一个点上,尽管尝试了几个小时却不知道如何继续。

这是springsecurity配置文件

package com.example.leave;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.example.leave.service.CustomUserDetailsService;


@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter{


    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Bean
    public CustomUserDetailsService createCustomUserDetailsService() {
        System.out.println("inside the create bean of userdetailservice");
        return new CustomUserDetailsService();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
//            .antMatchers("/resources/**", "/registration").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll()
            .and().rememberMe().key("uniqueAndSecret");    
    }


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

    private PasswordEncoder getPasswordEncoder() {
        return new PasswordEncoder() {

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                System.out.println("iam inside the password encoder matches "+encodedPassword+" raw password "+rawPassword.toString());
                return true;
//              return rawPassword.toString().equals(encodedPassword) ? true:false;
            }

            @Override
            public String encode(CharSequence rawPassword) {
                // TODO Auto-generated method stub
                return rawPassword.toString();
            }
        };
    }
}

当我在CustomUserDetailsS​​ervice中访问UserRepository时,我得到了对象

public class CustomUserDetailsService implements UserDetailsService{

 public CustomUserDetailsService() {
    System.out.println("inside customerdetails constructor");
}

@Autowired
private UserRepository userRepository;
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    System.out.println("inside loaduserbyusername "+userRepository)

到目前为止,userRepository的打印效果非常好。

但是当我自动连接另一个服务类中的userRepository时,它为null;

@Service
public class LeaveService {

    @Autowired
    private LeaveRepository leaveRepository;

    @Autowired
    private UserRepository userRepository;

    public void save(String empId, LocalDate fromDate, LocalDate toDate, String leaveType, String reason) {

        System.out.println("saving the data into leave trans table "+leaveRepository+"  "+userRepository);

请有人帮我解决这个问题。

0 个答案:

没有答案