Hallo zusammen...
Probiere nun schon seit fast 2 wochen zu checken ob der user eingeloggt ist oder nicht und bekomms einfach nicht hin.
ich habe ein LoginBean, LoginBackingBacking und ein LoginChecker in welchem der PhaseListener drin ist.
Mein Problem ist, dass ich trotz meines schon vorhandenen codes noch ohne eingeloggt zu sein auf die restlichen Seiten welche nur (ausser register) eingeloggt besuchen zu sein sollen.
index.xhtml
LogginBackingBean
LoginBean
LoginChecker
Könnt ihr mir da weiterhelfen das zu schaffen, wäre dankbar :rtfm:
Probiere nun schon seit fast 2 wochen zu checken ob der user eingeloggt ist oder nicht und bekomms einfach nicht hin.
ich habe ein LoginBean, LoginBackingBacking und ein LoginChecker in welchem der PhaseListener drin ist.
Mein Problem ist, dass ich trotz meines schon vorhandenen codes noch ohne eingeloggt zu sein auf die restlichen Seiten welche nur (ausser register) eingeloggt besuchen zu sein sollen.
index.xhtml
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>JSF Notenverwalter</title>
<link type="text/css" rel="stylesheet" href="themes/bluesky/skin.css" />
</h:head>
<h:body>
<center>
<p:panel header="Login Form" style="width: 350;">
<h:form>
<h:panelGrid columns="2" cellpadding="2">
<h:outputLabel for="LoginName" value="#{msgs.accountname}" />
<h:panelGroup>
<p:inputText id="LoginName" value="#{loginBean.loginName}"
required="true" size="15"
binding="#{loginBackingBean.login}"/>
</h:panelGroup>
<h:outputLabel for="Password" value="#{msgs.password}" />
<h:panelGroup>
<p:password id="Password" required="true" size="15" value="#{loginBean.password}"
validator="#{loginBackingBean.validatePw}" label="Password"
feedback="false" />
</h:panelGroup>
</h:panelGrid>
<h:commandButton id="loginButton" action="#{loginBean.goHome}" value="#{msgs.loginb}"/>
<h:commandButton value="#{msgs.cancel}" type="reset"/>
<br />
<h:outputLabel value="#{msgs.notRegistered}" for="registerButton" />
<br />
<p:button id="registerButton" value="#{msgs.register}" outcome="register.xhtml"/>
</h:form>
</p:panel>
<div><p:messages ></p:messages></div>
</center>
</h:body>
</html>
LogginBackingBean
Java:
// **** JSF Notenverwalter LoginBackingBean **** //
// **** Author: Thomas Steinbrüchel **** //
package beans;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.NamingException;
@ManagedBean
@SessionScoped
public class LoginBackingBean {
public LoginBackingBean(){}
private UIInput login;
private String userName;
private String vorname;
private String nachname;
private String password;
// Validator_Methode: Überprüft ob in DB User mit diesem Passwort existiert
public void validatePw(FacesContext context, UIComponent component, Object value) throws SQLException, NamingException {
userName = (String) login.getLocalValue();
password = value.toString();
try {
String sql = "SELECT name, vorname FROM user WHERE user.username = '" + userName + "' AND user.password = '" + password +"'";
ResultSet rs = dataBaseBeans.DatabaseManager.getDatebaseManager().sendQuery(sql);
if ( rs.next() ) {
nachname = rs.getString("name");
vorname = rs.getString("vorname");
}
else {
System.out.println("login failed " + login);
FacesMessage message = new FacesMessage("Login inkorrekt!");
context.addMessage(component.getClientId(context),message);
message.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(message);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
System.out.println("login success " + userName);
}
// Methode setzt die loginID welche zur Validation gebraucht wird. Wird an "userid" aus login.xhtml gebunden
public void setLogin(UIInput login) {
this.login = login;
}
public UIInput getLogin() {
return login;
}
public String getUserName(){
return this.userName;
}
public String getNachname() {
return nachname;
}
public String getVorname() {
return vorname;
}
public String getPassword() {
return password;
}
}
LoginBean
Java:
package beans;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.apache.catalina.Session;
import dataBaseBeans.DatabaseManager;
@ManagedBean
@SessionScoped
public class LoginBean {
ResultSet rs;
public String loginName;
public String password;
public String newPassword;
public String newPasswordReap;
public String oldPassword;
public int klasse;
public String className;
public int role;
public String roleName;
public int userID;
public String sessionID;
boolean loggedIn = false;
@ManagedProperty(value="#{loginBackingBean}")
private LoginBackingBean loginBackingBean;
public LoginBean() {}
public String goLogout() {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
loggedIn = false;
return "index.xhtml?faces-redirect=true";
}
// Go to changePassword.xhtml
public String goChangePaswword() {
if(loggedIn) {
return "changePassword.xhmtl";
}
else {
return "index.xhmtl";
}
}
// Method to change the users password
public String changePassword() {
if(newPassword.equals(newPasswordReap)) {
String sql = "UPDATE user SET password = '" + newPassword + "' WHERE user.username = '" + loginName + "' AND user.password = '" + password +"' ";
try {
DatabaseManager.getDatebaseManager().sendUpdate(sql);
password = newPassword;
System.out.println("Password Change Successful");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return "home.xhtml";
} else {
return "changePassword.xhtml";
}
}
// Go to profil.xhtml
public String goProfilDetails() {
if(loggedIn) {
return "profil.xhmtl";
}
else {
return "index.xhmtl";
}
}
// the goHome Method
public String goHome() throws SQLException {
String sql = "SELECT * FROM user WHERE user.username = '" + loginName + "' AND user.password = '" + password +"'";
try {
rs = DatabaseManager.getDatebaseManager().sendQuery(sql);
if(rs.next()) {
klasse = Integer.valueOf(rs.getString("klasse_fk"));
role = Integer.valueOf(rs.getString("role_fk"));
if(role == 1) {
roleName = "Student";
}
if(role == 2) {
roleName = "Teacher";
}
if(role == 3) {
roleName = "Admin";
}
if(klasse == 1) {
className = "5IA09";
}
if(klasse == 2) {
className = "5IS09";
}
userID = Integer.valueOf(rs.getString("user_pk"));
setUserID(userID);
loggedIn = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return "home.xhtml";
}
public void setLoginBackingBean(LoginBackingBean loginBackingBean) {
this.loginBackingBean = loginBackingBean;
}
public String getLoginName() {
return loginName;
}
public String getName() {
return loginBackingBean.getNachname();
}
public String getVorname() {
return loginBackingBean.getVorname();
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public boolean isLoggedIn() {
return this.loggedIn;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getOldPassword() {
return null;
}
public void setOldPassword(String oldPassword) {
this.oldPassword = oldPassword;
}
public String getNewPassword() {
return null;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
public String getNewPasswordReap() {
return null;
}
public void setNewPasswordReap(String newPasswordReap) {
this.newPasswordReap = newPasswordReap;
}
public int getKlasse() {
return klasse;
}
public void setKlasse(int klasse) {
this.klasse = klasse;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
}
LoginChecker
Java:
package beans;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;
//Diese Klasse muss nicht editiert werden
public class LoginChecker implements PhaseListener {
private static final long serialVersionUID = 1L;
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
public void beforePhase(PhaseEvent event) {
}
public void afterPhase(PhaseEvent event) {
FacesContext fc = event.getFacesContext();
//nur index.xhtml und register.xhtml darf man ohne eingeloggt zu sein besuchen.
boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("index") > -1 ? true : false;
boolean registerPage = fc.getViewRoot().getViewId().lastIndexOf("register") > -1 ? true : false;
if ((!loginPage && !loggedIn(fc)) && (!registerPage && !loggedIn(fc))) {
//redirect zurück zu index.xhtml da nicht eingeloggt
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "/index");
}
}
//überprüft ob schon eingeloggt
/*private boolean loggedIn() {
LoginBean lb = (LoginBean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("loginBean");
if (lb.getLoginName().length() > 0){
return true;
}
return false;
}*/
private boolean loggedIn(FacesContext context) {
LoginBean loginBean = (LoginBean)((HttpServletRequest) (context.getExternalContext().getRequest())).getSession().getAttribute("loginBean");
if(loginBean != null) {
if(loginBean.isLoggedIn()) {
return true;
}
}
return false;
}
}
Könnt ihr mir da weiterhelfen das zu schaffen, wäre dankbar :rtfm: