1

I have used join faces to integrate jsf and spring boot and added admin-faces which works fine. However I have an issue when a user log out from jsf homepage to login such that a user can still click browser back button and navigate back to the home page. I have followed this and does not seem to work for me. Here is my code

@ComponentScan(basePackages = {"com.mypackage*"})
@EnableAutoConfiguration
@EnableWebMvc
public class SpringApp{

    private @Autowired
    AutowireCapableBeanFactory beanFactory;
    
    @Bean
    public FilterRegistrationBean myFilter() {

        System.out.println("registred");//getting called on start

        FilterRegistrationBean registration = new FilterRegistrationBean();
        NoCacheFilter myFilter = new NoCacheFilter();
        registration.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST);
        beanFactory.autowireBean(myFilter);
        registration.setFilter(myFilter);
        registration.addUrlPatterns("/*");

        return registration;
    }

}

and here is my NoCache filter

public class NoCacheFilter implements Filter {
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest reqq = (HttpServletRequest) request;
            HttpServletResponse response = (HttpServletResponse) res;
           if (!reqq.getRequestURI().startsWith(reqq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
        response .setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setDateHeader("Expires", 0); // Proxies.
    }
    
            System.out.println("filterig called");//getting called 
    
            chain.doFilter(req, res);
        }
    
    
    }

and here is my managed bean

  @Getter
    @Setter
    @Scope(value = "session")
    @Component(value = "login")
    public class LoginManagedBean {
    
        @Autowired
        private LoginService loginService;
        
 

  public void logOut() throws IOException {
        System.out.println("log out");
        FacesContext.getCurrentInstance().getExternalContext().redirect(getLogoutPath("login"));
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        session.invalidate();


}

public String getLogoutPath(String url) {
    ServletContext servletContext = (ServletContext) FacesContext
            .getCurrentInstance().getExternalContext().getContext();
    return servletContext.getContextPath() + "/" + url + ".xhtml?faces-redirect=true";
}
        
         public String login()
        {
            System.out.println("username:"+getUsername());
            Users u=loginService.login(getUsername(),getPassword());
    
            if(u!=null)
            {
             
                return "home.xhtml?faces-redirect=true";
            }
            else{
                String msg = "Invalid Username / Password!!";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, ""));
            }
            return "";
        }
    }

and here is my homepage jsf which is calling the log out

<h:form>
                <p:growl id="messages"/>

                <p:menubar>
 <p:submenu label="Account" icon="fa fa-address-card bg_primary" style="position: absolute; right: 6px;">
               
                        <p:menuitem value="Log Out" action="#{login.logOut}" icon="fa fa-power-off bg_primary"/>//to call managed bean
                    
                    </p:submenu>
                      </h:menubar>
                  </h:form>

and here is my application.properties

server.servlet.context-path=/MyContext
joinfaces.primefaces.theme=admin
joinfaces.jsf.project-stage=development
joinfaces.primefaces.csp=true
joinfaces.primefaces.font-awesome=true
joinfaces.myfaces.clear-input-when-submitted-value-is-null-or-empty=true
Nemuga Dev
  • 144
  • 1
  • 7

0 Answers0