Код: Выделить всё
...
Login
login.xhtml
FORM
jdbcRealm
/login.xhtml
/login-error.xhtml
USER
ADMIN
Faces Servlet
jakarta.faces.webapp.FacesServlet
1
Faces Servlet
*.xhtml
30
JSESSIONID
/
true
false verwenden -->
org.primefaces.extensions.DELIVER_UNCOMPRESSED_RESOURCES
false
primefaces.THEME
nova-light
Код: Выделить всё
...
Startleiste
index.xhtml
FORM
jdbcRealm
/login/login.xhtml
/login/login-error.xhtml
Protected Area
/*
ADMIN
USER
USER
ADMIN
Faces Servlet
jakarta.faces.webapp.FacesServlet
1
Faces Servlet
*.xhtml
30
JSESSIONID
/
true
false
org.primefaces.extensions.DELIVER_UNCOMPRESSED_RESOURCES
false
primefaces.THEME
nova-light
Код: Выделить всё
package login.web;
import java.io.IOException;
import java.io.Serializable;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import login.util.LoginService;
import login.util.entities.User;
@RequestScoped
@Named
public class LoginController implements Serializable {
private static final long serialVersionUID = -5346634778960677989L;
private static final String URL = "myServerURL:8080";
@Inject
private LoginService loginService;
private String username;
private String password;
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() {
FacesContext context = FacesContext.getCurrentInstance();
User user = loginService.findUserByUsername(username);
if (user != null && user.getPassword().equals(SHA256Util.hashPassword(password))) {
context.getExternalContext().getSessionMap().put("user", user);
try {
System.out.println("Login successful, redirecting to start page.");
context.getExternalContext().redirect(URL + "/Start");
} catch (IOException e) {
System.out.println("Redirect failed: " + e.getMessage());
e.printStackTrace();
}
} else {
System.out.println("Invalid username or password.");
context.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid username or password", null));
}
}
public void logout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("login.xhtml");
} catch (IOException e) {
System.out.println("Logout redirect failed: " + e.getMessage());
e.printStackTrace();
}
}
}
Код: Выделить всё
package startleiste.glassfish;
import java.io.IOException;
import java.security.Principal;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
@Named
@RequestScoped
public class LoginRedirectBean {
public void redirectToLogin() {
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
try {
Principal userPrincipal = externalContext.getUserPrincipal();
if (userPrincipal == null) {
System.out.println("User ist nicht authentifiziert, Umleitung zur Login-Seite");
externalContext.redirect("myServerURL/Login");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
План состоял в том, чтобы настроить центральную страницу входа -> после входа в систему пользователь переходит на страницу навигации. Если вы ранее обращались к странице навигации без входа в систему, приложение перенаправляет вас на страницу входа.
Я делал это в разных WAR-файлах
Login.war
Start. war
Если я успешно вошел в систему - Приложение мгновенно перенаправляет меня на Login.war
почему?
Подробнее здесь: https://stackoverflow.com/questions/786 ... are-portal