问题描述
我在Spring安全身份验证方面存在一些问题。
在我的应用程序的任何地方一切都很好(CRUD操作运行良好),但登录尝试失败。
I have some issues with Spring security authentication.Everywhere in my application everything works great (CRUD operations work well), but login attempt fails.
这是我的代码(我在下面标记了userDAO是的注释null,这是验证失败的原因):
Here's my code (I marked below with comments where userDAO is null which is cause of failed authentication):
@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
我的SecurityConfig看起来像这样
My SecurityConfig looks like this
@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的,您可以在这里查看更多详细信息
I marked where i get NPE and i have no idea how to solve this. Whole Configuration is JavaBased and you can check it out out here for more detailsHERE
编辑:getUsers()在控制器中以这种方式调用:
getUsers() is invoked this way in controller:
@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
And in this case (when invoking userService.getUsers()) userDAO is not null
试图解决它像Bohuslav Burghardt建议的那样我得到了
Tried to fix it like Bohuslav Burghardt suggested and i got
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
in line
auth.userDetailsService(userService);
in lineauth.userDetailsService(userService);
推荐答案
使用Bohuslav的这段代码解决问题
Problem solved with this piece of code from 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.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
这篇关于使用UserDetailsService的Spring Security身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!