0

I am going through one spring tutorial. http://websystique.com/spring-security/spring-security-4-remember-me-example-with-hibernate/

I am not able to understand the flow of control from UserDaoImpl.findBySSO to UserProfile... Could you please help me

Given below is the 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  

Code(flow initiated from this portion of code)

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;
    }

}

Entity class(Control reaches to this portion of code)

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  + "]";
    }


}

Complete project is available in gitHub https://github.com/jaisonsteephen/EnumDoubtClarification/blob/master/src/main/java/com/websystique/springsecurity/service/CustomUserDetailsService.java

jaison
  • 83
  • 2
  • 8

2 Answers2

0

The solution to your problem is in AbstractDao class at this line

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

Debug that line and let me know if you still cant figure out

Mudassar
  • 3,135
  • 17
  • 22
  • Thanks for the reply. I put breakpoint in the line specified... but control is not hitting there – jaison Jun 22 '16 at 09:28
0

I think, I got the answer for my question. I am hereby sharing my answer.

In stacktrace there is a line CriteriaLoader(Loader).getRow. If we click on that line, we can see that each persistent entry is trying to get initialized.

        else {
            object = instanceNotYetLoaded(
                    rs,
                    i,
                    persisters[i],
                    descriptors[i].getRowIdAlias(),
                    key,
                    lockModes[i],
                    optionalObjectKey,
                    optionalObject,
                    hydratedObjects,
                    session
                );
        }
  • So in the first loop of i, UserProfile entity get initialized. In the second loop User entity initialized.
  • Since there is am enum associated with that class it also get initialized. But since there is no Enum in User class we are not seeing the control coming there.

If we put a similar enum (like below code snippet)there also, we can see the control comes to there.

@Column(name="FIRST_NAME", nullable=false)
private String firstName = UserType.USER.getUserType();
martinez314
  • 12,162
  • 5
  • 36
  • 63
jaison
  • 83
  • 2
  • 8