1
Good afternoon, I am with a system, I was able to filter in case the user is not logged in to the session, redirect to login screen. Only now I’m trying to do the opposite, in case the user is logged in and he tries to access the login screen, redirect to the start screen.
Follow my code below
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Awake</display-name>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>loginAdm.xhtml</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception- type>
<location>/loginAdm.xhtml</location>
</error-page>
<!-- login filter -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>br.com.awake.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
Loginfilter.java
package br.com.awake;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import br.com.awake.model.AdministradorModel;
public class LoginFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
AdministradorModel user = null;
HttpSession sess = ((HttpServletRequest) request).getSession(false);
if (sess != null){
user = (AdministradorModel) sess.getAttribute("usuarioLogado");
}
if (user == null) {
String contextPath = ((HttpServletRequest) request)
.getContextPath();
((HttpServletResponse) response).sendRedirect(contextPath
+ "/loginAdm.xhtml");
} else {
chain.doFilter(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException {
}
Sessioncontext.java
package br.com.awake;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import br.com.awake.model.AdministradorModel;
public class SessionContext {
private static SessionContext instance;
public static SessionContext getInstance(){
if (instance == null){
instance = new SessionContext();
}
return instance;
}
private SessionContext(){
}
private ExternalContext currentExternalContext(){
if (FacesContext.getCurrentInstance() == null){
throw new RuntimeException("O FacesContext não pode ser chamado fora de uma requisição HTTP");
}else{
return FacesContext.getCurrentInstance().getExternalContext();
}
}
public AdministradorModel getUsuarioLogado(){
return (AdministradorModel) getAttribute("usuarioLogado");
}
public void setUsuarioLogado(AdministradorModel usuario){
setAttribute("usuarioLogado", usuario);
}
public void encerrarSessao(){
currentExternalContext().invalidateSession();
}
public Object getAttribute(String nome){
return currentExternalContext().getSessionMap().get(nome);
}
public void setAttribute(String nome, Object valor){
currentExternalContext().getSessionMap().put(nome, valor);
}