安全配置类中的springdetails服务的Spring注入失败

时间:2015-07-19 12:33:17

标签: spring spring-security

我是Spring安全性的新手,我正在尝试使用java配置它但是当我尝试将UserDetailsS​​ervice注入安全配置类时,我得到一个404错误页面但是当我将它注入控制器时注入工作。我正在使用spring版本4.1.6和spring security 4.0.0

这是我的安全配置类

@Service("loginService")
public class LoginService implements UserDetailsService{

@Autowired
UserRepository userRepository;

@Transactional
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    SiteUser user = userRepository.findByUsername(username);

    Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
     SimpleGrantedAuthority userAuthority = new SimpleGrantedAuthority("ROLE_USER");
     SimpleGrantedAuthority adminAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
    User u = null;
    if(user == null) 
        throw new UsernameNotFoundException("No such User: " + username);
    else
    {

        if (user.getRole().equals("USER"))
               authorities.add(userAuthority);

        else if (user.getRole().equals("ADMIN")) 
        {
               authorities.add(userAuthority);
               authorities.add(adminAuthority);
        }
        u = new User(user.getUsername(), user.getPassword(), authorities);
    }

    return u;
}

}

这是UserDetailsS​​ervice类

{{1}}

项目的其余部分可用 here

2 个答案:

答案 0 :(得分:1)

解决方案是添加      @ComponentScan(&#34; com.ashken *&#34)  在securityconfig类之上

答案 1 :(得分:0)

我发现在SecurityConfig中简单地将UserDetailsS​​ervice的实现注册为bean不那么麻烦:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
UserRepository userRepository;

@Bean
public UserDetailsService userDetailsService() {
  return new UserDetailsService() {

    @Transactional
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

      SiteUser user = userRepository.findByUsername(username);

      Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>();
      SimpleGrantedAuthority userAuthority = new SimpleGrantedAuthority("ROLE_USER");
      SimpleGrantedAuthority adminAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
      User u = null;
      if(user == null) {
        throw new UsernameNotFoundException("No such User: " + username);
      } else {

        if (user.getRole().equals("USER")) {
          authorities.add(userAuthority);
        } else if (user.getRole().equals("ADMIN")) {
          authorities.add(userAuthority);
          authorities.add(adminAuthority);
        }
        u = new User(user.getUsername(), user.getPassword(), authorities);
      }

      return u;
    }

  };
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .csrf().disable()
        .authorizeRequests()
            .antMatchers("/").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/")
            .usernameParameter("username")
            .passwordParameter("password")
            .defaultSuccessUrl("/userlist")
            .failureUrl("/")
            .permitAll()
            .and()
        .logout()
            .permitAll();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    /*auth
        .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");*/
    auth.userDetailsService(loginService).passwordEncoder(passwordEncoder());
}

@Override
public void configure(WebSecurity web) throws Exception {

            web
              .ignoring()
                 .antMatchers("/resources/**");

          }


@Bean
public Md5PasswordEncoder passwordEncoder(){
    Md5PasswordEncoder encoder = new Md5PasswordEncoder();
    return encoder;
}
}