Witam! Mam w projekcie kod jak poniżej. Kiedy uruchomię projekt i wpiszę login oraz hasło dostaję komunikat *Musisz podać nazwę gracza, *Musisz podać hasło - tak jakbym nic nie wpisała. Niestety nie potrafię się doszukać błędu.
Oprócz Gracz.java i LoginBB.java mam jeszcze SecurityCheckFilter.java - chciałabym aby admin po zalogowaniu był przekierowany do innej strony niż user i z tym też nie umiem dojść do ładu. Proszę o pomoc
Gracz.java
package com.jsf.entities;
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
/**
* The persistent class for the gracz database table.
*
*/
@Entity
@Table(name="gracz", schema = "zagadkibazadb")
//@NamedQuery(name="Gracz.findAll", query="SELECT g FROM gracz g")
public class Gracz implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false, name="idgracz")
private Integer idgracz;
@Column(name="nazwa", nullable = false)
private String nazwa;
@Column(name="haslo", nullable = false)
private String haslo;
@Column(name="ostatni_wynik")
private Integer ostatniWynik;
@Temporal(TemporalType.DATE)
@Column(name="ostatnio_grano")
private Date ostatnioGrano;
/*@Column(name = "wynik_idwynik", nullable = false)
private String wynik_idwynik;*/
private HashSet<String> roles = new HashSet<String>();
public Gracz(String nazwa, String haslo) {
this.nazwa = nazwa;
this.haslo = haslo;
}
//bi-directional many-to-one association to Wynik
//@ManyToOne
@OneToMany(mappedBy="gracz", fetch=FetchType.EAGER)
private List<Wynik> wyniks;
//private Wynik wynik;
public Gracz() {
}
public Integer getIdgracz() {
return this.idgracz;
}
public void setIdgracz(Integer idgracz) {
this.idgracz = idgracz;
}
public String getNazwa() {
return this.nazwa;
}
public void setNazwa(String nazwa) {
this.nazwa = nazwa;
}
public String getHaslo() {
return this.haslo;
}
public void setHaslo(String haslo) {
this.haslo = haslo;
}
public Integer getOstatniWynik() {
return this.ostatniWynik;
}
public void setOstatniWynik(Integer ostatniWynik) {
this.ostatniWynik = ostatniWynik;
}
public Date getOstatnioGrano() {
return this.ostatnioGrano;
}
public void setOstatnioGrano(Date ostatnioGrano) {
this.ostatnioGrano = ostatnioGrano;
}
public List<Wynik> getWyniks() {
return this.wyniks;
}
public void setWyniks(List<Wynik> wyniks) {
this.wyniks = wyniks;
}
/* public Wynik getWynik() {
return this.wynik;
}
public void setWynik(Wynik wynik) {
this.wynik = wynik;
}*/
public HashSet<String> getRoles() {
return roles;
}
public void setRoles(HashSet<String> roles) {
this.roles = roles;
}
}
LoginBB.java
package com.jsfcourse.security;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
//import java.util.Date;
import com.jsf.dao.GraczDAO;
import com.jsf.entities.Gracz;
@ManagedBean
public class LoginBB {
private static final String PAGE_MAIN = "index.jsf";
private static final String PAGE_LOGIN = "gracz.jsf?faces-redirect=true";
///private static final String PAGE_ADMIN = "admin";
private static final String PAGE_STAY_AT_THE_SAME = null;
@EJB
GraczDAO graczDAO;
private String nazwa;
private String haslo;
public String getNazwa() {
return nazwa;
}
public void setNazwa(String nazwa) {
this.nazwa = nazwa;
}
public String getHaslo() {
return haslo;
}
public void setHaslo(String haslo) {
this.haslo = haslo;
}
public boolean validateData() {
boolean result = true;
FacesContext ctx = FacesContext.getCurrentInstance();
// check if not empty
if (nazwa == null || nazwa.length() == 0) {
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Musisz podać nazwę gracza", "null"));
}
if (haslo == null || haslo.length() == 0) {
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Musisz podać hasło ", "null"));
}
if (ctx.getMessageList().isEmpty()) {
result = true;
} else {
result = false;
}
return result;
}
public String doLogin() {
FacesContext ctx = FacesContext.getCurrentInstance();
Gracz gracz = null;
// 1. check parameters and stay if errors
if (!validateData()) {
return PAGE_STAY_AT_THE_SAME;
}
// 2. verify login and password - get User from "database"
gracz = getUserFromDatabase(nazwa, haslo);
// 3. if bad login or password - stay with error info
if (gracz == null) {
ctx.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Niepoprawny login lub hasło", null));
return PAGE_STAY_AT_THE_SAME;
}
/*if(nazwa == "Admin"){
HttpSession session = (HttpSession) ctx.getExternalContext()
.getSession(true);
session.setAttribute("Admin", gracz);
// and enter the system
return PAGE_ADMIN;
}*/
// 4. if login ok - save User object in session
HttpSession session = (HttpSession) ctx.getExternalContext()
.getSession(true);
session.setAttribute("Gracz", gracz);
// and enter the system
return PAGE_LOGIN;
}
public Gracz getGracz() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
return (Gracz) session.getAttribute("Gracz");
}
public String doLogout(){
HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
.getExternalContext().getSession(true);
//Invalidate session
// - all objects within session will be destroyed
// - new session will be created (with new ID)
session.invalidate();
return PAGE_MAIN;
}
// simulate finding user in database
private Gracz getUserFromDatabase(String nazwa, String haslo) {
Gracz g = null;
if (nazwa.equals("nazwa") && haslo.equals("haslo")) {
g = new Gracz(nazwa, haslo);
g.setNazwa("nazwa");
// g.setOstatniWynik("#{gracz.ostatniWynik}");
g.setHaslo("haslo");
// assumed system roles
/* g.getRoles().add("role1");
g.getRoles().add("role2");
g.getRoles().add("admin");*/
}
return g;
}
}
SecurityCheckFilter.java
public class SecurityCheckFilter implements Filter {
private ServletContext servletContext;
String publicRes;
String loginPage;
String resRes;
private static final String FACES_REDIRECT_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<partial-response><redirect url=\"%s\"></redirect></partial-response>";
/**
* Initialization of the filter object (overrides the parent method). The
* method loads parameters specifying paths of public resources and login
* page. Parameters should be set for the filter in web.xml as
* "publicResource" and "loginPage". If the parameters are not set "/public"
* and "/ligin.jsf" are assumed as the relevant values.
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig config) throws ServletException {
servletContext = config.getServletContext();
// get defined public resources, if not - assume "/public" subfolder
// (default)
publicRes = config.getInitParameter("publicResource");
if (publicRes == null) {
publicRes = "/public";
}
publicRes = config.getInitParameter("restrictedResource");
if (resRes == null) {
resRes = "/restricted";
}
loginPage = config.getInitParameter("loginPage");
if (loginPage == null) {
loginPage = "/login.jsf";
}
}
/**
* The filtering method (overrides the parent method). The method retrieves
* a User object from session. If the conditions of valid connection are
* fulfilled then the request is passed further. In other case the user is
* forwarded to a login page. The approach works also for JSF AJAX requests.
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
// load user object from session
HttpSession session = request.getSession();
Gracz g = (Gracz) session.getAttribute("gracz");
boolean haslo = false;
if (g == null) { // no data - check if call for public resources or
// login page
String path = request.getServletPath();
if (path.startsWith(publicRes) || path.startsWith(loginPage)) {
haslo = true;
}
} else {
haslo = true;
}
// IMPORTANT: other checking based on remote host or port could be
// performed above for higher level of security
// if the request is not valid (client is not logged in)
if (!haslo) {
// if AJAX request then redirect to application root
if ("partial/ajax".equals(request.getHeader("Faces-Request"))) {
res.setContentType("text/xml");
res.setCharacterEncoding("UTF-8");
res.getWriter().printf(FACES_REDIRECT_XML,
request.getContextPath() + "/");
} else {
// if other (regular) request then forward to the defined login
// page
servletContext.getRequestDispatcher(loginPage).forward(request,
response);
}
} else { // if request is valid (client is logged in) then
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
}
}
index.xhtml
<h:panelGrid columns="6" cellpadding="0" style="margin-left: 4em;">
<!-- <div id="content2">-->
<h:outputLabel for="nazwa" value=" Login: " styleClass="logowanie_napisl"/>
<h:inputText id="nazwa" value="#{loginBB.nazwa}" styleClass="logowanie_login" />
<h:outputLabel for="haslo" value=" Hasło: " styleClass="logowanie_napish"/>
<h:inputSecret id="haslo" value="#{loginBB.haslo}" styleClass="logowanie_haslo" />
<h:messages id="msg" />
<h:form>
<h:commandButton value="Zaloguj" action="#{loginBB.doLogin()}" styleClass="button1" update="msg"/>
</h:form>