TomEE 8.0.8 Web プロファイルを使用しています。最小限の例を次に示します。
@WebServlet("/login")
@CustomFormAuthenticationMechanismDefinition(
loginToContinue = @LoginToContinue(loginPage = "/WEB-INF/views/login.jsp"))
public class LoginServlet extends HttpServlet {
@Inject SecurityContext securityContext;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("pass");
Credential credential = new UsernamePasswordCredential(username, password);
AuthenticationStatus authenticate =
securityContext.authenticate(req, resp, withParams().credential(credential));
}
}
予約済みページ (認証済みユーザーのみ)
@WebServlet("/protected")
@ServletSecurity(@HttpConstraint(rolesAllowed = {"admin"}))
public class ProtectedResource extends HttpServlet {
@Inject
Principal principal;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(principal.getName());
}
}
認証されていないユーザーとして "/protected" にアクセスすると、Web サーバーによってログイン ページにリダイレクトされます。そこで資格情報を送信すると、見たいページにリダイレクトされます。これは意図したとおりに機能します。
問題は、保護されたリソースにアクセスせずにログインするだけの場合です。「/login」に移動して資格情報を送信すると、セッションに保存されません。実際、「/protected」を開こうとすると、まだ認証されていないかのように動作します。
何が問題になる可能性がありますか? JSR 375 は、パラメーターを含む「processCallerInitiatedAuthentication」と「processContainerInitiatedAuthentication」について話していnewAuthentication
ますが、私はそれを機能させることができません。
更新:securityContext.authenticate(req, resp, authparams)
保護されたリソースまたはログインページ自体によって呼び出された場合、呼び出しは同様の結果をもたらすようです:Principal
セッションではなくリクエストに保存されます。
実際の違いは、 LoginServlet#doGet
TomEE フィルター (私はOpenEJBSecurityListener$RequestCapture
s だと思います) が を呼び出しAuthenticatorBase#authenticateJaspic
、特定の if ブランチ (902 行目) に入りPrincipal
、前述のチェックを行い、認証状態をセッションに保存した後にあります。これは、制約のあるリソースにアクセスしようとした後にログインした場合にのみ発生します。
今のところ、発信者が開始した認証プロセスを模倣するダミーの保護されたページを作成しますが、他に何ができるか知りたいです。
更新 2:のログは次のとおりですorg.apache.catalina.authenticator.AuthenticatorBase
。
保護されたリソースにアクセスした場合
## I open the protected page
02-Jan-2022 19:02:00.278 FINE [http-nio-8080-exec-74] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /myapp/protected
02-Jan-2022 19:02:00.278 FINE [http-nio-8080-exec-74] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling hasUserDataPermission()
02-Jan-2022 19:02:00.278 FINE [http-nio-8080-exec-74] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling authenticate()
02-Jan-2022 19:02:00.280 FINE [http-nio-8080-exec-74] org.apache.catalina.authenticator.AuthenticatorBase.invoke Failed authenticate() test
## I submit the form
02-Jan-2022 19:02:22.865 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request POST /myapp/doLogin
02-Jan-2022 19:02:22.865 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling authenticate()
02-Jan-2022 19:02:22.865 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.authenticateJaspic Authenticated user: null
02-Jan-2022 19:02:22.865 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.invoke Successfully passed all security constraints
02-Jan-2022 19:02:22.974 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /myapp/protected
02-Jan-2022 19:02:22.975 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling hasUserDataPermission()
02-Jan-2022 19:02:22.975 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling authenticate()
02-Jan-2022 19:02:22.976 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.authenticateJaspic Authenticated user: GenericPrincipal[admin(admin,user,)]
02-Jan-2022 19:02:22.976 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.register Authenticated 'admin' with type 'JASPIC'
02-Jan-2022 19:02:22.976 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.changeSessionID Session ID changed on authentication from [F169FEBAA4C7EF7FC32445F146BCAC9E] to [932FF01A3F035B80E93464E8CB25853B]
02-Jan-2022 19:02:22.976 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling accessControl()
02-Jan-2022 19:02:22.977 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Successfully passed all security constraints
ログインページに直接アクセスした場合
# If i access the login page directly
02-Jan-2022 19:04:20.245 FINE [http-nio-8080-exec-80] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /myapp/login
02-Jan-2022 19:04:20.246 FINE [http-nio-8080-exec-80] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling authenticate()
02-Jan-2022 19:04:20.290 FINE [http-nio-8080-exec-80] org.apache.catalina.authenticator.AuthenticatorBase.authenticateJaspic Authenticated user: null
02-Jan-2022 19:04:20.290 FINE [http-nio-8080-exec-80] org.apache.catalina.authenticator.AuthenticatorBase.invoke Successfully passed all security constraints
# I submit the form
02-Jan-2022 19:04:48.657 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request POST /myapp/login
02-Jan-2022 19:04:48.657 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling authenticate()
02-Jan-2022 19:04:48.657 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.authenticateJaspic Authenticated user: null
02-Jan-2022 19:04:48.657 FINE [http-nio-8080-exec-76] org.apache.catalina.authenticator.AuthenticatorBase.invoke Successfully passed all security constraints
02-Jan-2022 19:04:48.977 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Security checking request GET /myapp/images/logo.ico
02-Jan-2022 19:04:48.978 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Calling authenticate()
02-Jan-2022 19:04:48.978 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.authenticateJaspic Authenticated user: null
02-Jan-2022 19:04:48.978 FINE [http-nio-8080-exec-77] org.apache.catalina.authenticator.AuthenticatorBase.invoke Successfully passed all security constraints