2016-06-28 4 views
0

私は複数のユーザが共有しているSessionBeanというJava Spring Beanを持っています。新しいユーザーが自分のアプリケーションにアクセスすると、SessionBeanのインスタンスが作成されます。このBeanは、ユーザーのIDと権限情報を保持して、自分のアプリで何が表示されるかを決定します。ただし、2人目のユーザーが同時にアプリケーションにアクセスすると、1人目のユーザーがそれを使用しているときに、作成されたBeanが2番目のユーザーの資格情報によって上書きされ、両方のユーザーがこれらの上書きされた資格情報を使用します。どのように各ユーザのSessionBeanが他のユーザから独立しているようにするのですか?Spring - SessionBeanインスタンスがJavaの複数のユーザによって共有されています

MainController.java

package com.trac.controller; 

import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletRequest; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

import com.trac.verification.*; 
import com.trac.bean.SessionBean; 

@Controller 
public class MainController { 

private CompanyOrgVerification org = new CompanyOrgVerification(); 

@Autowired 
private SessionBean session; 
@Autowired 
private RacfGroupData racfGroup; 

//returns the agent search page 
@RequestMapping(URIConstants.WELCOME_PAGE) 
public ModelAndView welcome(HttpServletRequest request){ 

    this.session = new SessionBean(); 

    String userId = request.getHeader("x-user"); 
    System.out.println(userId.trim()); 
    session.setUserId(userId.trim()); 

    if(racfGroup.getRacfGroups(session.getUserId())){ 
     session.setPermission("INFOA", "U"); //Granting the default permissions to anyone with access to the application. 
     return new ModelAndView("index"); 
    }else{ 
     ModelAndView error = new ModelAndView(); 
     error.setViewName("error"); 
     return error; 

    } 
} 


//returns the agent profile page 
@RequestMapping(value = URIConstants.PROFILE_PAGE) 
public ModelAndView profile(HttpServletRequest request){ 
    Cookie[] cookies = request.getCookies(); 
    String profileEntityNo = ""; 
    for(int i=0; i<cookies.length; i++){ 

     String cookieName = cookies[i].getName().toUpperCase(); 

     switch(cookieName){ 
      case "ENTITYNO": profileEntityNo = cookies[i].getValue().toString().trim(); 
       break; 
      case "STATE": cookies[i].getValue().toString().trim(); 
       break; 
      case "NAME": cookies[i].getValue().replace("%20", " ").replace("%2C", ",").trim(); 
       break; 
      case "DISTRICT": cookies[i].getValue().toString().trim(); 
       break; 
     } 
    } 
    if(session.hasAccessToApp()){ //check if user is authorized to access page 
     int structureNo = 3; //company org structure number 
     String structureCd = "ORG"; 
     org.setSessionBean(session); 
     org.setProfilePrivileges(profileEntityNo, structureCd, structureNo); 
     this.setSessionBean(org.getSessionBean()); 

     ModelAndView profile = new ModelAndView(); 
     profile.setViewName("profile"); 
     profile.addObject("userId", session.getUserId()); 
     profile.addObject("privileges", session.getProfilePrivileges()); 
     profile.addObject("accessType", session.getAccessType()); 

     return profile; 
    }else{ 
     return new ModelAndView("error"); 
    } 
} 

public SessionBean getSessionBean(){ 
    return session; 
} 
public void setSessionBean(SessionBean session){ 
    this.session = session; 
} 
public RacfGroupData getRacfGroup(){ 
    return racfGroup; 
} 
public void setRacfGroup(RacfGroupData racfGroup){ 
    this.racfGroup = racfGroup; 
} 

} 

SessionBean.java

package com.trac.bean; 

import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 
import org.springframework.context.annotation.ScopedProxyMode; 


@Component 
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class SessionBean { 

private String userId; 
private String permission = ""; 
private final int grant_InfoA = 0; 
private final int grant_Trac = 1; 
private final int grant_Term = 2; 
private final int grant_Conf = 3; 
private final int grant_Mark = 4; 
private boolean[] profilePrivileges = new boolean [ 5 ]; //holds the user privileges for a profile 
private String[] accessType = new String[5]; 

public void setPermission(String permissionValue, String type){ 
    try{ 
     permission = permissionValue; 
     setPrivileges(type); 
    }catch(Exception e){ 
     System.out.println("Exception caught in SessionBean - setPermission. "+e.toString()); 
    } 
} 

public void setPrivileges(String type){ 
try{ 
     System.out.println("***Permission String: " + this.permission + "***"); 
    //Default Permission 
     if(permission.equals("INFOA")){ 
      profilePrivileges[grant_InfoA] = true; 
      accessType[grant_InfoA] = type; 
      System.out.println("***Granted access to the application***"); 
     } 
    //Subsidy Tab Permission 
     else if(permission.equals("TRAC")){ 
      profilePrivileges[grant_Trac] = true; 
      accessType[grant_Trac] = type; 
      System.out.println("***Granted access to Trac subsidy***"); 
     } 
    //Termination Tab Permission 
     else if(permission.equals("TERM")){ 
      profilePrivileges[grant_Term] = true; 
      accessType[grant_Term] = type; 
      System.out.println("***Granted access to termination info***"); 
     } 
    //Conference Tab Permission 
     else if(permission.equals("CONF")){ 
      profilePrivileges[grant_Conf] = true; 
      accessType[grant_Conf] = type; 
      System.out.println("***Granted access to conference info***"); 
     } 
    //Service/Transfer & Term Tabs Permission 
     else if(permission.equals("MARK")){ 
      profilePrivileges[grant_Mark] = true; 
      accessType[grant_Mark] = type; 
      System.out.println("***Granted access to Service/Transfer and Term tabs***"); 
     } 
     else{ 
     System.out.println("No privileges set. Permission string is: " 
      + permission); 
     } 
    }catch (Exception e){ 
     System.out.println("^^^^ Exception caught in SessionBean." 
      + "setPrivileges ^^^^\n" + e.toString()); 
    } 
} 

public String getUserId() { 
    return userId; 
} 
public void setUserId(String userId) { 
    this.userId = userId.toUpperCase(); 
} 
public boolean hasAccessToApp(){ 
    if(profilePrivileges[grant_InfoA] == true){ 
     return true; 
    }else{ 
     return false; 
    } 
} 

public boolean[] getProfilePrivileges(){ 
    return profilePrivileges; 
} 

public String[] getAccessType(){ 
    return accessType; 
} 

} 
+2

あなたは 'this.session = new SessionBean();'は何と思いますか? –

+0

複数のユーザーに個別の権限が与えられている権限アレイに問題がありました。ユーザーAはログインし、TRACは表示できますが、TERMまたはCONFは表示できないとします。ユーザBがログインしていて、TRACではなくTERMを見ることができる場合、セッションBeanの権限配列はTRACとTERMの両方で真となり、両方のユーザが必要ではないデータを見ることができます。 such.session = new SessionBean()を使用することにより、誰かがアプリケーションにアクセスしたときにいつでも新しいセッションを作成し、特権が共有されないようにしました。 – Karson074

+0

あなたのユースケースを1秒間忘れてしまいます。オブジェクトを自分で作成する場合、Springはどのように関与しますか? –

答えて

0

okが、これは本当に私が探していたソリューションではありませんが、それは仕事を得ました。私がやったことは、MainControllerクラスのprofile()関数にありました。私はwelcome()関数で行ったように、ヘッダーからuserIdを再取得し、セッションでリセットしました。私はその後、特権とアクセスタイプの配列をリセットし、MainControllerのprofile()関数でそれを呼び出す新しい関数をSessionBeanに作成しました。これにより、他のユーザー権限とのクロスコンタミネーションがなくても、ユーザーに対して正しい権限を引き出すことができました。

誰かがより良い解決策を持っていれば、私はそれらを聞いて嬉しいです。

関連する問題