使用UserDetailsS​​ervice的Spring Security身份验证

时间:2015-03-29 22:20:14

标签: java spring spring-security

我在Spring安全身份验证方面存在一些问题。 在我的应用程序中,一切都很好(CRUD操作运行良好),但登录尝试失败。

这是我的代码(我在下面标注了userDAO为null的注释,这是验证失败的原因):

@Service
public class UserServiceImpl implements UserService, UserDetailsService {

    @Autowired
    UserDAO userDAO;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userDAO.getUserByUsername(username); //userDAO == null Causing NPE
        if (user == null)
            throw new UsernameNotFoundException("Oops!");

        List<SimpleGrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority(user.getRole()));

        return new org.springframework.security.core.userdetails
                .User(user.getLogin(), user.getPassword(), authorities);
    }

@Override
    public List<User> getUsers() {
        return userDAO.getUsers();//userDAO !=null
    }
//rest of code skipped

My SecurityConfig看起来像这样

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

 UserServiceImpl userService = new UserServiceImpl();

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService);
    }
//rest of code skipped

我标记了我获得NPE的地方,我不知道如何解决这个问题。整个配置是基于Java的,你可以在这里查看更多细节 HERE

编辑:在控制器中以这种方式调用getUsers():

@Controller
public class LoginController {
    @Autowired
    UserService userService;

 @RequestMapping(value = "/dashboard")
    public ModelAndView userDashboard(){
        ModelAndView modelAndView = new ModelAndView("Dashboard");
        List<User> userList = userService.getUsers();
        modelAndView.addObject("users", userList);
        return modelAndView;
    }

在这种情况下(调用userService.getUsers()时)userDAO不为null

试图解决它像Bohuslav Burghardt建议的那样,我得到了

 method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder cannot be applied to given types;
  required: T
  found: com.gi.service.UserService
  reason: inferred type does not conform to upper bound(s)
    inferred: com.gi.service.UserService
    upper bound(s): org.springframework.security.core.userdetails.UserDetailsService

排队 auth.userDetailsS​​ervice(userService);

2 个答案:

答案 0 :(得分:6)

这是不正确的部分:

UserServiceImpl userService = new UserServiceImpl();

当您自己实例化服务时,其自动连接的依赖项将始终为null。您必须让Spring容器实例化它(已经通过使用@Service标记服务来完成),然后将其注入安全配置类中,如下所示:

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userService;
}

答案 1 :(得分:2)

使用Bohuslav的这段代码解决了问题

public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserService userService;
}

也缺少

@ComponentScan("com.gi")

之前

public class SecurityConfig extends WebSecurityConfigurerAdapter {

缺乏导致

Error:(24, 13) java: method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.Authentic‌​ationManagerBuilder cannot be applied to given types; required: T found: com.gi.service.UserService reason: inferred type does not conform to upper bound(s) inferred: com.gi.service.UserService upper bound(s): org.springframework.security.core.userdetails.UserDetailsService