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

编程入门 行业动态 更新时间:2024-10-27 02:20:59
本文介绍了使用UserDetailsS​​ervice的Spring Security身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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的,您可以在这里查看更多详细信息 HERE

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 details HERE

编辑: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.userDetailsS​​ervice(userService);

in line auth.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.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

更多推荐

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

本文发布于:2023-10-28 20:00:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537629.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   ervice   UserDetailsS   Security   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!