2012-07-17 3 views
0

현재 나의 struts2 + 스프링 웹 어플리케이션에서 잘 작동하는 사용자 정의 userSessionInterceptor가 있습니다. 이제는 사용자 역할을 시행하기를 원합니다. 나는 많은 온라인 연구를 해왔다. 예외, sendRedirectstruts2 확인 권한 인터셉터를 확인하십시오.

가장 적절한 방법은 무엇입니까? 이미 사용자 세션에 사용자 역할이 저장되어 있으며 오류를 발견하고 발견하는 데 문제가 없습니다. 내가 결정할 필요가있는 것은 허가가 옳지 않을 때 그것에 대응하는 방법뿐입니다.

요격기에서 "xxx"를 반환하고 "xxx"동작으로 이동할 수 있음을 알고 있습니다. 그러나 내가 성취하고자하는 것은 사용자가 허락을받지 않은 것을 시도 할 때 메시지가 표시된다는 것입니다. 이전 페이지로 돌아가서 URL에 매개 변수를 추가 할 수 있다고 가정합니다.

이것에 대한 도움말?

감사합니다.

+0

그것을 할 "가장 적절한"방법은 없습니다; 그것은 당신의 필요에 달려 있습니다. 요청을 저장하는 것은 쉽습니다. 실제로 메시지를 처리하는 방법에 따라 메시지가 표시됩니다. '액세스 거부'페이지로 리디렉션하는 것이 가장 쉽습니다. –

+0

감사합니다. 정확히 말하면 permission error url 매개 변수를 사용하여 이전 페이지로 다시 리디렉션하는 가장 좋은 방법은 무엇입니까? – AbSoLution8

+0

응용 프로그램에서 보안 API를 허용하는 경우 스프링 보안 또는 Apache Shrio 중 하나를 선택하는 것이 좋습니다. 두 프레임 워크 모두 해당 요구 사항을 모두 처리합니다. –

답변

0

다음은 샘플 프로그램입니다.

RoleInterceptor.java

package com.sample.common.interceptor; 

import javax.servlet.http.HttpSession; 

import org.apache.struts2.ServletActionContext; 

import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionInvocation; 
import com.opensymphony.xwork2.interceptor.Interceptor; 
import com.opensymphony.xwork2.util.ValueStack; 

public class RoleInterceptor implements Interceptor { 

    private static final long serialVersionUID = 5031826078189685536L; 

    @Override 
    public void init() { 

    } 

    @Override 
    public void destroy() { 

    } 

    @Override 
    public String intercept(ActionInvocation actionInvocation) { 

     String result = null; 
     try { 
      ActionContext actionContext = actionInvocation 
        .getInvocationContext(); 

      ValueStack vStack = actionContext.getValueStack(); 
      HttpSession httpSession = ServletActionContext.getRequest() 
        .getSession(); 

      StringBuilder actionUrl = new StringBuilder(actionInvocation 
        .getProxy().getNamespace()); 
      actionUrl.append("/"); 
      actionUrl.append(actionInvocation.getProxy().getActionName()); 

      if (httpSession != null) { 

       boolean hasPermission = true; // if the role has the permission 

       if (hasPermission) { 

        result = actionInvocation.invoke(); 

       } else { 

        vStack.set("userMsg", 
          "You are not authorized to access this link"); 
        result = "user.unauthorized"; 
       } 
      } else { 

       vStack.set("userMsg", "Please login to your account"); 
       result = "user.login"; 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return result; 

    } 

} 

struts.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <package name="userRoleInterceptor"> 
     <interceptors> 
      <interceptor name="userRole" 
       class="com.sample.common.interceptor.RoleInterceptor" /> 
     </interceptors> 
    </package> 
    <package name="user" namespace="/user" extends="struts-default, json-default, userRoleInterceptor"> 
     <action name="user_details" method="getUserDetails" 
      class="com.sample.user.web.action.User"> 
      <interceptor-ref name="userRole"/> 
      <interceptor-ref name="store"> 
       <param name="operationMode">RETRIEVE</param> 
      </interceptor-ref> 
      <result name="success">userDetails.jsp</result> 
      <result name="input">/common/error.jsp</result> 
      <result name="busy" type="redirectAction" >/common/busy.jsp</result> 
      <result name="error" type="redirectAction" >/common/test.jsp</result> 
      <result name="user.login" type="redirectAction" >/common/login.jsp</result> 
      <result name="user.unauthorized" type="redirectAction" >/common/unauthorizedUser.jsp</result> 
     </action> 
    </package> 
</struts>