我正在通过一个春季教程。http://websystique.com/spring-security/spring-security-4-remember-memember-me-me-example-with-hibernate/
我无法理解从userdaoimpl.findbysso到userProfile的控制流量...您能帮我
下面给出的是Debug StackTrace
UserProfile.<init>() line: 18
NativeConstructorAccessorImpl.newInstance0(Constructor<?>, Object[]) line: not available [native method]
NativeConstructorAccessorImpl.newInstance(Object[]) line: 62
DelegatingConstructorAccessorImpl.newInstance(Object[]) line: 45
Constructor<T>.newInstance(Object...) line: 423
PojoInstantiator.instantiate() line: 124
PojoInstantiator.instantiate(Serializable) line: 136
PojoEntityTuplizer(AbstractEntityTuplizer).instantiate(Serializable, SessionImplementor) line: 737
SingleTableEntityPersister(AbstractEntityPersister).instantiate(Serializable, SessionImplementor) line: 4761
SessionImpl.instantiate(EntityPersister, Serializable) line: 1391
SessionImpl.instantiate(String, Serializable) line: 1379
CriteriaLoader(Loader).instanceNotYetLoaded(ResultSet, int, Loadable, String, EntityKey, LockMode, EntityKey, Object, List, SessionImplementor) line: 1618
CriteriaLoader(Loader).getRow(ResultSet, Loadable[], EntityKey[], Object, EntityKey, LockMode[], List, SessionImplementor) line: 1514
CriteriaLoader(Loader).getRowFromResultSet(ResultSet, SessionImplementor, QueryParameters, LockMode[], EntityKey, List, EntityKey[], boolean, ResultTransformer) line: 725
CriteriaLoader(Loader).processResultSet(ResultSet, QueryParameters, SessionImplementor, boolean, ResultTransformer, int, List<AfterLoadAction>) line: 952
CriteriaLoader(Loader).doQuery(SessionImplementor, QueryParameters, boolean, ResultTransformer) line: 920
CriteriaLoader(Loader).doQueryAndInitializeNonLazyCollections(SessionImplementor, QueryParameters, boolean, ResultTransformer) line: 354
CriteriaLoader(Loader).doList(SessionImplementor, QueryParameters, ResultTransformer) line: 2553
CriteriaLoader(Loader).doList(SessionImplementor, QueryParameters) line: 2539
CriteriaLoader(Loader).listIgnoreQueryCache(SessionImplementor, QueryParameters) line: 2369
CriteriaLoader(Loader).list(SessionImplementor, QueryParameters, Set<Serializable>, Type[]) line: 2364
CriteriaLoader.list(SessionImplementor) line: 126
SessionImpl.list(Criteria) line: 1682
CriteriaImpl.list() line: 380
CriteriaImpl.uniqueResult() line: 402
UserDaoImpl.findBySSO(String) line: 19
UserServiceImpl.findBySso(String) line: 22
代码(从代码的这一部分启动的流)
package com.websystique.springsecurity.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.websystique.springsecurity.model.User;
import com.websystique.springsecurity.model.UserProfile;
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService{
@Autowired
private UserService userService;
@Transactional(readOnly=true)
public UserDetails loadUserByUsername(String ssoId)
throws UsernameNotFoundException {
User user = userService.findBySso(ssoId);
System.out.println("User : "+user);
if(user==null){
System.out.println("User not found");
throw new UsernameNotFoundException("Username not found");
}
return new org.springframework.security.core.userdetails.User(user.getSsoId(), user.getPassword(),
user.getState().equals("Active"), true, true, true, getGrantedAuthorities(user));
}
private List<GrantedAuthority> getGrantedAuthorities(User user){
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for(UserProfile userProfile : user.getUserProfiles()){
System.out.println("UserProfile : "+userProfile);
authorities.add(new SimpleGrantedAuthority("ROLE_"+userProfile.getType()));
}
System.out.print("authorities :"+authorities);
return authorities;
}
}
实体类(控制到达代码的这一部分)
package com.websystique.springsecurity.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="USER_PROFILE")
public class UserProfile {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="TYPE", length=15, unique=true, nullable=false)
private String type = UserProfileType.USER.getUserProfileType();
@Column(name="TESTTYPE", length=15, unique=true, nullable=false)
private String testtype = UserProfileType.DBA.getUserProfileType();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTesttype() {
return testtype;
}
public void setTesttype(String testtype) {
this.testtype = testtype;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof UserProfile))
return false;
UserProfile other = (UserProfile) obj;
if (id != other.id)
return false;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override
public String toString() {
System.out.println("UserProfile [id=" + id + ", type=" + type + "]");
return "UserProfile [id=" + id + ", type=" + type + "]";
}
}
完整的项目可在GitHub中找到https://github.com/jaisonsteephen/enumdoubtclarification/blob/master/src/src/main/java/java/com/com/websystique/springsecurity/service/service/customuserdetailsssssersservice.java
解决问题的解决方案在此行中的AbstractDao
类中
public AbstractDao(){
this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}
调试那条线,让我知道您是否仍然无法弄清楚
我想,我得到了我的问题的答案。我特此分享我的答案。
在StackTrace中有一个线路标准加载器(LOADER).getrow。如果我们单击该行,我们可以看到每个持续条目都在尝试初始化。
else {
object = instanceNotYetLoaded(
rs,
i,
persisters[i],
descriptors[i].getRowIdAlias(),
key,
lockModes[i],
optionalObjectKey,
optionalObject,
hydratedObjects,
session
);
}
- 因此,在i的第一个循环中,用户填充实体会初始化。在第二个循环中用户实体初始化。
- 由于与该类相关的AM枚举也会被初始化。但是,由于用户类中没有枚举,因此我们没有看到控件来到那里。
如果我们在那里放了类似的枚举(如下面的代码段),我们可以看到控件来到那里。
@Column(name="FIRST_NAME", nullable=false)
private String firstName = UserType.USER.getUserType();