Sha*_*man 6 java security authentication session java-ee
我已经实现了一个自定义HttpAuthenticationMechanism子类来使用Soteria/Java EE安全框架提供身份验证.我的身份验证工作正常.但是,我注意到当我调用HttpMessageContext.setRegisterSession(String, Set<String>)创建Java EE时Session,行为不是我所期望的.我期望经过身份验证的身份与网络相关联Session,并且我AuthenticationMechanism的validateRequest(HttpServletRequest req, HttpServletResponse res, HttpMessageContext ctx)方法不会在后续请求中被调用.然而,我观察的是validateRequest(),即使用户已经成功验证,也会在每个请求上调用.
我可以使用@AutoApplySession我的AuthenticationMechanism类上的注释获得我想要的行为,但这不是我想要的行为.我想根据提供的凭证类型选择是否创建会话.
我对setRegisterSession()方法的理解不正确吗?或者这是Soteria中的一个错误?
@AutoApplySession是 Soteria ( JSR 375 )中执行此操作的新方法。如果它不适合您的需求(因为您需要记住经过身份验证的身份或根据其他一些凭据信息在同一 HTTP 会话期间对所有请求重新进行身份验证),则validateRequest无论您是否调用HttpMessageContext' ssetRegisterSession方法与否。HttpMessageContext.setRegisterSession将使容器记住凭据,但不会自动重用它们,您仍然需要通过执行与 Soteria 相同的操作来使容器重用身份验证身份AutoApplySessionInterceptor。因此,在实现的类中,HttpAuthenticationMechanism您应该在方法中执行实际身份验证逻辑之前添加以下代码validateRequest:
Principal userPrincipal = request.getUserPrincipal();
if (userPrincipal != null) {
httpMessageContext.getHandler().handle(new Callback[] {
new CallerPrincipalCallback(httpMessageContext.getClientSubject(), userPrincipal) }
);
return AuthenticationStatus.SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
另外,请参阅Arjan Tijms 的回答。虽然它是关于 JASPIC 而不是 Soteria,但在这种情况下我认为它是相关的。希望这可以帮助。