2012-12-07 13 views
6

フォームベースの認証を以下のようにプログラムで追加する方法はありますか? 私は自分自身LdapLoginModuleを使用しています。最初は基本認証を使用していましたが、正常に機能しましたが、ログインページ(ロゴの表示など)をもっと制御したいのですがEmbedded Jetty - プログラムベースでフォームベースの認証を追加する

良いサンプルがありますか?

私は組み込みjetty v8.1.7を使用しています。私は埋め込まれた桟橋にweb.xmlを使用していません。 jettyサーバーはプログラムによって起動されます。

<login-config> 
    <auth-method>FORM</auth-method> 
    <realm-name>Test JAAS Realm</realm-name> 
    <form-login-config> 
     <form-login-page>/login.html</form-login-page> 
     <form-error-page>/error.jsp</form-error-page> 
    </form-login-config> 
</login-config> 

答えて

11

FormAuthenticatorを作成し、ServletContextHandlerのためにあなたのSecurityHandlerでこれを設定します。このコードは、2つのサーブレットを持つ簡単なサーバーを作成します。最初のサーブレットは、認証されたユーザー名へのhelloメッセージで応答します。 2番目のサーブレットは簡単なログインフォームを実装しています。

main[]にコードを貼り付けて実行する必要があります(クラスパスには、jetty-serverjetty-servletjetty-securityの次のジャーが必要です)。テストするには、ブラウザでhttp://localhost:8080を指すようにするには、応答がhello usernameになる前に、資格情報(ユーザー名/パスワード)を入力する必要があります。

Server server = new Server(8080); 
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY); 

context.addServlet(new ServletHolder(new DefaultServlet() { 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.getWriter().append("hello " + request.getUserPrincipal().getName()); 
    } 
}), "/*"); 

context.addServlet(new ServletHolder(new DefaultServlet() { 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.getWriter().append("<html><form method='POST' action='/j_security_check'>" 
     + "<input type='text' name='j_username'/>" 
     + "<input type='password' name='j_password'/>" 
     + "<input type='submit' value='Login'/></form></html>"); 
    } 
}), "/login"); 

Constraint constraint = new Constraint(); 
constraint.setName(Constraint.__FORM_AUTH); 
constraint.setRoles(new String[]{"user","admin","moderator"}); 
constraint.setAuthenticate(true); 

ConstraintMapping constraintMapping = new ConstraintMapping(); 
constraintMapping.setConstraint(constraint); 
constraintMapping.setPathSpec("/*"); 

ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler(); 
securityHandler.addConstraintMapping(constraintMapping); 
HashLoginService loginService = new HashLoginService(); 
loginService.putUser("username", new Password("password"), new String[] {"user"}); 
securityHandler.setLoginService(loginService); 

FormAuthenticator authenticator = new FormAuthenticator("/login", "/login", false); 
securityHandler.setAuthenticator(authenticator); 

context.setSecurityHandler(securityHandler); 

server.start(); 
server.join(); 
+0

これを有効にすることはできませんでした... – oshai

+0

ありがとうございました!これは私には役に立ちました。私はそれを機能させることができました。 – mwhidden

+2

これは役に立ちますが、私はあなたがそれをどうやって知っているのだろうと思っています。桟橋のドキュメンテーションは...無礼で、疎ではありません。例えば、 'ConstraintMapping'は完全に文書化されておらず、Googleはそれに関する情報をまったく提供していません。それを学ぶために内部のJettyのコードを読まなければなりませんか? – Timmmm

関連する問題