我在Spring-MVC应用程序之上使用Spring-Security。我有我自己的UserDAO,userDetailsService的实现。我试图通过从数据库检查(ofcourse)进行身份验证。我到达直到登录页面,一切似乎都做工精细,但当我登录,我得到一个错误:Spring-Security DAOAuthenticationProvider要求身份验证
错误:
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工作。我张贴下面的代码。
login服务:
@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;
你能否提供USER类? – Pracede 2014-10-08 17:01:42
@Pracede:添加...请检查编辑 – 2014-10-09 07:02:01
问题似乎是在私人String id = UUID.randomUUID()。toString();我认为这将工作,如果你有一个整数而不是字符串。但我没有找到为什么 – Pracede 2014-10-09 07:07:23