0

我正在学习一个春季教程。http://websystique.com/spring-security/spring-security-4-remember-me-example-with-hibernate/

我无法理解从 UserDaoImpl.findBySSO 到 UserProfile 的控制流程...你能帮帮我吗

下面给出的是调试堆栈跟踪

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/main/java/com/websystique/springsecurity/service/CustomUserDetailsS ​​ervice.java

4

2 回答 2

0

您的问题的解决方案AbstractDao在这一行的课堂上

public AbstractDao(){
        this.persistentClass =(Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
    }

调试该行,如果您仍然无法弄清楚,请告诉我

于 2016-06-22T07:44:57.783 回答
0

我想,我得到了我的问题的答案。我特此分享我的答案。

在 stacktrace 中有一行 CriteriaLoader(Loader).getRow。如果我们单击该行,我们可以看到每个持久条目都在尝试初始化。

        else {
            object = instanceNotYetLoaded(
                    rs,
                    i,
                    persisters[i],
                    descriptors[i].getRowIdAlias(),
                    key,
                    lockModes[i],
                    optionalObjectKey,
                    optionalObject,
                    hydratedObjects,
                    session
                );
        }
  • 所以在 i 的第一个循环中,UserProfile 实体被初始化。在第二个循环中初始化用户实体。
  • 由于存在与该类关联的枚举,因此它也会被初始化。但由于 User 类中没有 Enum,我们没有看到控件出现在那里。

如果我们在那里也放一个类似的枚举(如下面的代码片段),我们可以看到控件到了那里。

@Column(name="FIRST_NAME", nullable=false)
private String firstName = UserType.USER.getUserType();
于 2016-06-22T10:03:50.620 回答