Witam,
Skonfigurowałem JASS i FileRealm na serwerze GlassFish. Udaje się zalogować za pomocą basic auth. Nie udaje mi się skutecznie wylogować, przeglądarka ciągle pamięta moje ustawienia. Korzystam z basic authentication.
a) Co działa:
- Rozpoznanie roli po autoryzacji.
- Po zalogowaniu mogę wejść do zabezpieczonego zasobu (strony administracyjnej).
b) Co nie działa: - Wykonanie wylogowania (session invalidate) powoduje, że dane dalej są pamiętane i mogę znowu wejść do pamiętanego zasobu. Unieważnienie sesji działa tylko dla jednego redirecta. Później znowu wiadomo na jakim jestem użytkownikiem.
- Co zrobić, aby wylogowanie zaczęło działać naprawdę, a sesja obowiązywała tylko dla określonych zasobów?
Bardzo zależy mi na wskazówkach / wytłumaczeniu zjawiska. Poniżej załączam moją konfigurację.
Usługa do wyświetlenia loginu obecnego użytkownika i wylogowania się:
@Named("userService")
@RequestScoped
public class UserService {
public String getLogin() {
Principal principal = FacesContext.getCurrentInstance()
.getExternalContext().getUserPrincipal();
if (principal != null) {
return principal.getName();
} else {
return "anonymouse";
}
}
public String logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return "/index.xhtml?faces-redirect=true";
}
}
Akcje nawigacji:
@Named("navigator")
@RequestScoped
public class Navigator {
public String admin() {
return "/admin.xhtml?faces-redirect=true";
}
public String home() {
return "/index.xhtml?faces-redirect=true";
}
}
Mój web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
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">
<security-constraint>
<display-name>Admin panel</display-name>
<web-resource-collection>
<web-resource-name>allowed</web-resource-name>
<description/>
<url-pattern>/admin.jsf</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>administrator</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<display-name>Restful websercices</display-name>
<web-resource-collection>
<web-resource-name>allowed</web-resource-name>
<description/>
<url-pattern>/webresources/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>administrator</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>myfilerealm</realm-name>
</login-config>
<security-role>
<description/>
<role-name>administrator</role-name>
</security-role>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
</web-app>
Mój sun-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 Servlet 3.0//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_3_0-0.dtd">
<sun-web-app error-url="">
<security-role-mapping>
<role-name>administrator</role-name>
<group-name>admin</group-name>
</security-role-mapping>
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</sun-web-app>
admin.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1>WELCOME TO THE ADMINISTRATIVE PANEL</h1>
<h:form>
<h:panelGrid columns="2">
<h:commandButton value="Invalidate"
action="#{userService.logout()}" />
<h:commandButton value="Home"
action="#{navigator.home()}" />
</h:panelGrid>
</h:form>
</h:body>
</html>
index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1>HELLO ALL</h1>
<h:form>
<h:outputText value="#{userService.login}"
id="usrSrvId" />
<h:panelGrid columns="2">
<h:commandButton value="Admin"
action="#{navigator.admin()}" />
</h:panelGrid>
</h:form>
</h:body>
</html>