1
工作在WildFly 9服務器上運行我的Java EE應用程序,我有一個自定義登錄模塊:JAAS註銷不會對自定義登錄模塊
public class MyLoginModule extends AbstractServerLoginModule {
private Principal identity;
@Override
public boolean login() throws LoginException {
// do something
identity = new SimplePrincipal("test");
subject.getPrincipals().add(identity);
// do something else
return true;
}
@Override
public boolean logout() throws LoginException {
subject.getPrincipals().remove(identity);
return true;
}
}
的login
方法按預期工作。但這與logout
方法不一樣。當我從Servlet
或網絡服務寫入類似request.getSession(false).invalidate();
的東西時,logout
方法已經達到了神祕的程度。
這裏我的配置文件:
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>customer-area</display-name>
<security-constraint>
<web-resource-collection>
<web-resource-name>restricted resources</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>*</role-name>
</auth-constraint>
</security-constraint>
<security-role>
<role-name>*</role-name>
</security-role>
<login-config>
<auth-method>MY-AUTH</auth-method>
</login-config>
</web-app>
的jboss-web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<security-domain>java:/jaas/MySecurityDomain</security-domain>
</jboss-web>
standalone.xml
<security-domain name="MySecurityDomain" cache-type="default">
<authentication>
<login-module code="mypackage.MyLoginModule" flag="required"/>
</authentication>
</security-domain>
ServletExtension
類:
public class MyServletExtension implements ServletExtension {
@Override
public void handleDeployment(final DeploymentInfo deploymentInfo, ServletContext servletContext) {
deploymentInfo.addAuthenticationMechanism("MY-AUTH", new AuthenticationMechanismFactory() {
@Override
public AuthenticationMechanism create(String mechanismName, FormParserFactory formParserFactory, Map<String, String> properties) {
return new MyAuthenticationMechanism();
}
});
}
}
AuthenticationMechanism
類:
public class MyAuthenticationMechanism implements AuthenticationMechanism {
@Override
public AuthenticationMechanismOutcome authenticate(HttpServerExchange exchange, SecurityContext securityContext) {
PasswordCredential credential = new PasswordCredential(new char[] {});
Account account = identityManager.verify("test", credential);
if (account != null) {
return AUTHENTICATED;
} else {
return NOT_AUTHENTICATED;
}
}
}
我錯過了什麼?
@Loc你是說'request.getSession(false).invalidate()'不是調用'logout'方法的方法嗎?在這種情況下,我還沒有理解。也許你可以啓發我。 – cheb1k4
可能與問題沒有直接關係+我可能錯了(我很久沒有使用JAAS了),但是在登錄模塊中保持特定於會話的狀態(即「Principal identity」)是否正確? –
@Loc這裏是一個例子,他說'request.getSession(false).invalidate()'是'logout'方法的觸發器:http://www.byteslounge.com/tutorials/jaas-logout-example。這不是我發現的唯一例子。 @NikosParaskevopoulos很好的問題。我認爲這是可能的,但也許我錯了。我會在稍後檢查。 – cheb1k4