これが私の問題を解決した方法です。:) 保護したいページは cPanel フォルダー内にあります。これは私の LoginAdmin Bean です。
@ManagedBean(name = "loginAdmin")
@SessionScoped
public class LoginAdmin implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String password;
boolean loggedIn;
public boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void login(ActionEvent actionEvent) {
FacesMessage msg = null;
if (username.equals("Administrator") && password.equals("store1")) {
try {
msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome",
username);
FacesContext.getCurrentInstance().getExternalContext()
.redirect("/eHUB/cPanel/index.xhtml");
loggedIn = true;
} catch (IOException e) {
e.printStackTrace();
}
} else {
msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error",
"Invalid User Name or Password");
loggedIn = false;
}
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void logout(ActionEvent actionEvent) throws IOException {
((HttpSession) FacesContext.getCurrentInstance().getExternalContext()
.getSession(false)).invalidate();
loggedIn = false;
FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
}
}
これは私のフィルターコードです:
@WebFilter("/cPanel/*")
public class RestrictFilter implements Filter {
private FilterConfig fc;
public RestrictFilter() {
}
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
LoginAdmin loginAdmin = (LoginAdmin) request.getSession().getAttribute("loginAdmin");
String loginURL = request.getContextPath() + "/login.xhtml";
if(loginAdmin != null && loginAdmin.isLoggedIn()){
chain.doFilter(req, res);
}
else{
response.sendRedirect(loginURL);
}
}
public void init(FilterConfig fConfig) throws ServletException {
this.fc = fConfig;
}
}
これは完全に機能しています。この投稿に反対票を投じてください。もう一度ありがとう。:)