Spring安全性自定义用户细节不起作用

时间:2013-05-20 00:38:21

标签: spring security

我正在尝试自定义userdetails,并使用汇编程序并在security xml中注册它。

 @Service("customUserDetailsService")
 public class CustomUserDetailsService implements UserDetailsService{

    @Autowired
    private UserService userService;

    @Autowired
    private Assembler assembler;


        @Override
    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {
    // TODO Auto-generated method stub
    UserDetails userDetails = null;

    UserEntity userEntity = userService.getUserByName(username);


    if (userEntity == null)

    throw new UsernameNotFoundException("user not found");

    return assembler.buildUserFromUser(userEntity);


}

汇编

@Service("assembler")
public class Assembler {


  @Transactional
  public UserDetails buildUserFromUser(UserEntity userEntity) {

     String username = userEntity.getUsername();
     String password = userEntity.getPassword();
     String name = userEntity.getName();
     boolean enabled = userEntity.getActive();
     boolean accountNonExpired = enabled;
     boolean credentialsNonExpired = enabled;
     boolean accountNonLocked = enabled;

     Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
     for(Role role : userEntity.getRoles()) {
             authorities.add(new SimpleGrantedAuthority(role.getName()));
     }


     return new CustomUser(username, password, enabled, accountNonExpired, 

     credentialsNonExpired, accountNonLocked, authorities, name);

 }}

security xml 2

    <authentication-manager>
    <authentication-provider user-service-ref='customUserDetailsService'>
    <password-encoder hash='md5' />
    </authentication-provider>
</authentication-manager>  

<beans:bean id='customUserDetailsService' 

    class='com.user.application.service.CustomUserDetailsService'></beans:bean>

    <context:component-scan base-package='com.user.application' />  

问题是CustomUserDetailsS​​ervice类未被识别或被调用。尝试登录时,应用程序会一直返回登录页面。

0 个答案:

没有答案