daw*_*los 6 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
Run Code Online (Sandbox Code Playgroud)
我的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
Run Code Online (Sandbox Code Playgroud)
我标记了我在哪里获得NPE,我不知道如何解决这个问题.整机配置JavaBased,你可以检查出来了这里查看更多详情 这里
编辑: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;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下(当调用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
Run Code Online (Sandbox Code Playgroud)
在行auth.userDetailsService(userService);
这是不正确的部分:
UserServiceImpl userService = new UserServiceImpl();
Run Code Online (Sandbox Code Playgroud)
当您自己实例化服务时,其自动连接的依赖项将始终如此null.您必须让Spring容器实例化它(已经通过标记服务来完成@Service),然后将其注入您的安全配置类,如下所示:
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userService;
}
Run Code Online (Sandbox Code Playgroud)
Bohuslav 的这段代码解决了问题
\n\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n @Autowired\n private UserService userService;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n也失踪了
\n\n@ComponentScan("com.gi")\nRun Code Online (Sandbox Code Playgroud)\n\n前
\n\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\nRun Code Online (Sandbox Code Playgroud)\n\n缺乏导致
\n\nError:(24, 13) java: method userDetailsService in class org.springframework.security.config.annotation.authentication.builders.Authentic\xe2\x80\x8c\xe2\x80\x8bationManagerBuilder 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\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
7395 次 |
| 最近记录: |