我在Spring-MVC应用程序之上使用Spring-Security。我有自己的UserDAO实现,userDetailsService。我试图通过检查数据库(ofcourse)进行身份验证。我到达登录页面,一切似乎都运行良好,但是当我登录时,我收到一个错误:
错误:
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。我发布下面的代码。
LoginService:
@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;
}
}
用户:
@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;
答案 0 :(得分:1)
似乎你的User类没有实现UserDetails更多它可能没有正确的getID方法返回类型