2012-09-03 2 views
0

내가 의도 한 방식이 아니며 완전히 잘못되었다는 것을 알고 있지만 명령은 명령이므로 다음을 수행해야합니다. 사용자가 매개 변수의 액세스 키를 사용하여 서블릿에 액세스하고, 다음과 같이하십시오 :서블릿을 통해 솔기 인증을하는 방법

http://myhost/my_app/servlet?accesskey=XXXXX 

서블릿은 키를 가져 와서 이음매로 사용자를 인증합니다. 나는 그것을 지금까지 할 수 없었습니다

답변

0

사용자 정의 서블릿을 작성해야하는지 또는 요청 매개 변수를 기반으로 로그인해야하는지 여부는 사용자의 질문에서 분명하지 않습니다. 가장 큰 차이점은 Seam이 사용자 정의 서블릿을 가로 챌 수 없으며 수동으로 이음 부 수명주기를 시작하지 않으면 (및 LifeCycle.endCall()을 사용하여 외부에서 호출 할 때 Seam 구성 요소를 사용할 수 있어야합니다.) SeamFilter). 그 외에도 두 가지 솔루션은 유사하게 작동합니다.

인증을 처리 할 구성 요소 만들기 :

@Name("myAuthenticator") 
public class MyAuthenticator implements Serializable { 

    // Seam's identity component 
    @In private transient Identity identity; 

    // When logged in, the user needs to have some roles, usually 
    // you assign these dynamically based on user name, type, etc., here 
    // I just initialize it to a fixed list of roles 
    ArrayList<String> roles = new ArrayList<String>(Arrays.toList(
      new String[] { "base", "admin" })); 

    // Access key (getters and setters omitted but are necessary) 
    private String accessKey; 

    public String doAuth() { 
     // Check accessKey validity (against an SSO service or 
     // in your DB or whatever), here we do a trivial check. 
     boolean userCanAccess = "ADMINKEY".equals(accessKey); 

     if (userCanAccess) { 
      identity.acceptExternallyAuthenticatedPrincipal(
        new SimplePrincipal("username")); 

      // Assign user roles 
      for (String role : roles) { 
       identity.addRole(role); 
      } 
      return "ok"; 
     } 
     return "ko"; 
    } 
} 

지금 (externalLogin.page.xml 말하자면,이에 대한 .xhtml 페이지를 만들 필요가 없습니다) 매개 변수를 통해 로그인을 처리 할 로그인 페이지 설명을 만듭니다 : 해당 페이지를 사용할 수있는 홈 지금

<page> 
    <!-- this sets the accessKey variable with the query parameter --> 
    <param name="accessKey" value="#{myAuthenticator.accessKey}" /> 

    <!-- this invokes our authentication action --> 
    <action execute="#{myAuthenticator.doAuth}" /> 

    <!-- navigation rules, these determine what to do if auth is ok or fails --> 
    <navigation from-action="#{myAuthenticator.doAuth}"> 
     <rule if-outcome="ko"> 
      <redirect view-id="/error.xhtml"> 
       <message severity="ERROR">Invalid Authentication Key</message> 
      </redirect> 
     </rule> 
     <rule if-outcome="ok"> 
      <redirect view-id="/home.xhtml"> 
       <message severity="INFO">Welcome!</message> 
      </redirect> 
     </rule> 
    </navigation> 
</page> 

그래서 같이 수행 할 수

http://localhost:8080/yourapp/externalLogin.seam?accessKey=XXXXXXXX 
사용자 정의 서블릿 (매우 가능성이 있지만, 그럼에도 불구하고)를 사용해야하는 경우

, 위의 구성 요소가 변경되지 않습니다, 그냥이 같은 서블릿 내에서 호출 :

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    // Start Seam lifecycle 
    LifeCycle.beginCall(); 
    // Get an instance of the authenticator component from Seam 
    MyAuthenticator auth = Component.getInstance("myAuthenticator"); 
    // Set access key in component and perform auth 
    auth.setAccessKey(req.getParameter("accessKey")); 
    String result = auth.doAuth(); 
    // End Seam lifecycle, no more component calls are needed. 
    LifeCycle.endCall(); 

    // Do something here with the result 
    if ("ok".equals(result)) { 
     resp.sendRedirect(...); 
    } 
} 
+0

기존 인증 방법을 사용하는 첫 번째 방법으로 사용하면 작동합니까? 나는 튜토리얼 기반의 Authenticator를 사용하고있다. 'http : // pastebin.com/mZHNT8kP' –

+0

짧은 대답은'아니오 '이고,'표준 '인증자는 실제로 인증을 수행하지 않으며, 사용자가 인증해야 하는지를 나타내는 부울을 반환한다. 아닙니다. 내가 준 인증 코드는 실제 인증을 수행합니다 ('identity.acceptExternallyAuthenticatedPrincipal()'호출). – EmirCalabuch

0

이 코드는 당신의 이음매 구성 요소에 액세스 할 수있게합니다.

Lifecycle.beginCall(); 

Authenticator authenticator = (Authenticator)Component.getInstance("authenticator"); 

LifeCycle.endCall(); 
+0

어떤 패키지 등 전반적인입니까? 그 클래스를 가지고있는 유일한 패키지는 org.jboss.seam.contexts, javax.faces.lifecycle과 org.hibernate.classic뿐입니다. 위에서 사용했던 Begin 메소드가 여러분의 코드에서 사용 했습니까? 또한 코드를 사용하여 어떻게 인증해야합니까? 이미 GET에서 매개 변수를 가져 왔지만 Authenticator 암호 변수에 전달하고 authenticate()하는 방법은 무엇입니까? –

+0

트린드가 대답 해 주시겠습니까? –

+0

org.jboss.seam.contexts – Trind

관련 문제