我已经使用连接面来集成 jsf 和 spring boot,并添加了工作正常的管理面。但是,当用户从 jsf 主页注销以登录时,我遇到了一个问题,这样用户仍然可以单击浏览器后退按钮并导航回主页。我遵循了这一点,似乎对我不起作用。这是我的代码
@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;
}
}
这是我的 NoCache 过滤器
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);
}
}
这是我的托管豆
@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 "";
}
}
这是我的主页 jsf,它正在调用注销
<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>
这是我的 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