Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring Security DAOAuthenticationProvider请求身份验证的ID_Spring Mvc_Spring Security - Fatal编程技术网

Spring mvc Spring Security DAOAuthenticationProvider请求身份验证的ID

Spring mvc Spring Security DAOAuthenticationProvider请求身份验证的ID,spring-mvc,spring-security,Spring Mvc,Spring Security,我在SpringMVC应用程序之上使用SpringSecurity。我有自己的UserDAO实现,userDetailsService。我试图通过从数据库中检查来进行身份验证(当然)。我一直到登录页面,似乎一切正常,但当我登录时,我发现一个错误: 错误: org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - An internal error occurred while tr

我在SpringMVC应用程序之上使用SpringSecurity。我有自己的UserDAO实现,userDetailsService。我试图通过从数据库中检查来进行身份验证(当然)。我一直到登录页面,似乎一切正常,但当我登录时,我发现一个错误:

错误:

org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - An internal error occurred while trying to authenticate the user.
org.springframework.security.authentication.InternalAuthenticationServiceException: Provided id of the wrong type for class com.WirTauschen.model.User. Expected: class java.lang.Integer, got class java.lang.String
    at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:110)
我不知道在哪里检索或使用用户id。我在下面发布代码

登录服务:

 @Service("userDetailsService")
public class LoginServiceImpl implements UserDetailsService{
    @Autowired private UserDao userDao;
    @Autowired private Assembler assembler;

    @Override
    @Transactional
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            UserDetails userDetails = null;

         User user = userDao.findByName(username);
            if(user == null) { throw new UsernameNotFoundException("Wrong username or password");} //Never specify which one was it exactly
        return assembler.buildUserFromUserEntity(user);
    }
}
汇编程序

@Service("assembler")
public class Assembler {
    @Transactional(readOnly = true)
    User buildUserFromUserEntity(com.WirTauschen.model.User userEntity){
        String username = userEntity.getUsername();
        String password = userEntity.getPassword();
      //  int id = userEntity.getId();
        boolean enabled = userEntity.isActive();
        boolean accountNonExpired = userEntity.isAccountNonExpired();
        boolean credentialsNonExpired = userEntity.isCredentialsNonExpired();
        boolean accountNonLocked = userEntity.isAccountNonLocked();

        Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

        User user1 = new User(username,password,enabled,accountNonExpired,credentialsNonExpired,accountNonLocked,authorities);
        return  user1;
        }
}

您的用户类似乎没有实现UserDetails,或者可能没有正确的getID方法返回类型

您能提供用户类吗?@Pracede:Added…请检查编辑问题似乎出在私有字符串id=UUID.randomUUID().toString()上;我想如果你有一个整数而不是字符串,它会工作的。但是我以前没有发现为什么我有int,但是我想实现一些其他的功能,所以我把它改成了String,同样的问题也存在。你找到解决方案了吗?我也有同样的问题。添加了实现UserDetails的user类,请看一看。我觉得无论您使用的是哪个JDBC框架,它都在制造一个问题,它期望bean用户的可序列化键是整数,而不是它的get字符串。所以问题来了。因此,您的spring sec登录不起作用。如果使用Hibernate,请将其映射到相应的列。并使用GeneratedValue等注释,以便提供getter setter方法。。。用户Bean的每个成员的
@Entity
@Table(name="registration")
public class User implements UserDetails{
    private static final GrantedAuthority USER_AUTH = new SimpleGrantedAuthority("ROLE_USER");

    @Id
    @Column(name="id")
    private String id=UUID.randomUUID().toString();
   // @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "my_entity_seq_gen")
    //@SequenceGenerator(name ="my_entity_seq_gen", sequenceName = "MY_ENTITY_SEQ")


    @OneToMany
    private Set<ProductBasic> productBasic;

    @Column(name = "email")
    private String email;

    @Column(name = "username")
    private String Username;

    @Column(name = "displayname")
    private String DisplayName;

    @Column(name = "password")
    private String password;

    @Column(name = "companyname")
    private String CompanyName;

    @Column(name = "firstname")
    private String FirstName;

    @Column(name = "middlename")
    private String MiddleName;

private String role="ROLE_USER";


    @Transient
    private final String PERMISSION_PREFIX = "ROLE_USER";
    @Transient
    private List<GrantedAuthority> authorities;
    public User() {
            this.authorities = new ArrayList<GrantedAuthority>();
            authorities.add(USER_AUTH);
    }

    public User(String Username, String password, String Role){
        this.Username = Username;
        this.password = password;
        this.role = Role;
        if((role == null) || role.isEmpty()){ role = "ROLE_USER";}

    }

 @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
@Id
@Column(name = "sortcanvasid")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sortcanvas_gen")
@SequenceGenerator(name = "sortcanvas_gen", sequenceName = "sortcanvas_seq")
private int sortCanvasId;