Spring MVC以编程方式登录:没有MyUserDetailsS​​ervice类型的限定bean

时间:2015-07-09 16:02:43

标签: java spring spring-mvc spring-security

我有一个带弹簧安全性的Spring应用程序,它一切都很好用。当我尝试在注册时登录用户时,我无法让它工作......

我搜索了很多,发现我的主要是我的UserDetails对象,因此我需要一个方法,通过MyUserDetailsS​​ervice中的用户名返回它:

public class MyUserDetailsService implements UserDetailsService {

    private UserService userService;

    @Autowired(required=true)
    @Qualifier(value="userService")
    public void setUserService(UserService us){
        this.userService = us;
    }

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

        // Programmatic transaction management
        /*
        return transactionTemplate.execute(new TransactionCallback<UserDetails>() {

            public UserDetails doInTransaction(TransactionStatus status) {
                com.mkyong.users.model.User user = userDao.findByUserName(username);
                List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRole());

                return buildUserForAuthentication(user, authorities);
            }

        });*/

        Users user = userService.findByUserName(username);
        List<GrantedAuthority> authorities = buildUserAuthority(user.getUserRoleses());

        return buildUserForAuthentication(user, authorities);


    }

    // Converts com.mkyong.users.model.User user to
    // org.springframework.security.core.userdetails.User
    private User buildUserForAuthentication(Users user, List<GrantedAuthority> authorities) {
        return new User(user.getUsername(), user.getPassword(), user.isEnabled(), true, true, true, authorities);
    }

    private List<GrantedAuthority> buildUserAuthority(Set<UserRoles> userRoles) {

        Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

        // Build user's authorities
        for (UserRoles userRole : userRoles) {
            System.out.println("----------------------->"+userRole+"<---------------------------------------");
            setAuths.add(new SimpleGrantedAuthority(userRole.getRoles().getRole() ));
        }

        List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(setAuths);

        return Result;
    }

在注册后我的控制器上尝试通过这样做来记录用户:

             MyUserDetailsService myuds = new MyUserDetailsService();
             UserDetails ud = myuds.loadUserByUsername(user.getUsername());

//           Authentication authentication = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(),
//                      AuthorityUtils.createAuthorityList("ROLE_CONCORRENTE"));
//           SecurityContextHolder.getContext().setAuthentication(authentication);

             Authentication authentication = new UsernamePasswordAuthenticationToken(ud, null);
             SecurityContextHolder.getContext().setAuthentication(authentication);

我甚至将豆子自动装配:

   private MyUserDetailsService myUserDetailsService;
        @Autowired(required=true)
        @Qualifier(value="myUserDetailsService")
        public void setMyUserDetailsService(MyUserDetailsService userdetailsservice){
            this.myUserDetailsService = userdetailsservice;
        }

我的豆子:

<bean id="myUserDetailsService" class="com.setelog.spring.service.MyUserDetailsService" >
<property name="userService" ref="userService" />

    </bean>

1 个答案:

答案 0 :(得分:1)

此案例可能存在许多问题 1)你定义了spring将搜索bean的包 2)您是否已将此类定义为服务(例如,通过使用@service批注) 3)你提供了它的实施吗?