Corregido error al comprobar si el usuario tenía sesión iniciada en el
filtro de authorización.
This commit is contained in:
@@ -29,17 +29,16 @@ public class AuthorizationFilter implements Filter {
|
|||||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
HttpServletRequest reqt = (HttpServletRequest) request;
|
HttpServletRequest req = (HttpServletRequest) request;
|
||||||
HttpServletResponse resp = (HttpServletResponse) response;
|
HttpServletResponse resp = (HttpServletResponse) response;
|
||||||
HttpSession ses = reqt.getSession(false);
|
HttpSession ses = req.getSession(false);
|
||||||
|
|
||||||
String reqURI = reqt.getRequestURI();
|
String reqURI = req.getRequestURI();
|
||||||
if (reqURI.indexOf("/login.xhtml") >= 0 || reqURI.indexOf("/RegisterUser.xhtml") >= 0 || reqURI.indexOf("/RegisterUserResult.xhtml") >= 0
|
if (reqURI.indexOf("/login.xhtml") >= 0 || reqURI.indexOf("/RegisterUser.xhtml") >= 0 || reqURI.indexOf("/home.xhtml") >= 0 || reqURI.indexOf("/public/") >= 0
|
||||||
|| (ses != null && ses.getAttribute("username") != null) || reqURI.indexOf("/public/") >= 0 || reqURI.indexOf("/home.xhtml") >= 0
|
|| reqURI.contains("javax.faces.resource") || SessionUtils.isLogedIn(ses) == true)
|
||||||
|| reqURI.contains("javax.faces.resource"))
|
|
||||||
chain.doFilter(request, response);
|
chain.doFilter(request, response);
|
||||||
else
|
else
|
||||||
resp.sendRedirect(reqt.getContextPath() + "/login.xhtml");
|
resp.sendRedirect(req.getContextPath() + "/login.xhtml");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,12 +2,12 @@
|
|||||||
package managedbean.common;
|
package managedbean.common;
|
||||||
|
|
||||||
import javax.faces.context.FacesContext;
|
import javax.faces.context.FacesContext;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
import TO.LoggedUserTO;
|
import TO.LoggedUserTO;
|
||||||
|
|
||||||
|
|
||||||
public class SessionUtils {
|
public class SessionUtils {
|
||||||
public static final String SESSION_VAR_USERNAME = "userName";
|
public static final String SESSION_VAR_USERNAME = "userName";
|
||||||
public static final String SESSION_VAR_USERID = "userId";
|
public static final String SESSION_VAR_USERID = "userId";
|
||||||
@@ -15,13 +15,21 @@ public class SessionUtils {
|
|||||||
public static final String SESSION_VAR_USER = "loggedInUser";
|
public static final String SESSION_VAR_USER = "loggedInUser";
|
||||||
|
|
||||||
public static HttpSession getSession() {
|
public static HttpSession getSession() {
|
||||||
return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
|
FacesContext ctx = FacesContext.getCurrentInstance();
|
||||||
|
if (ctx != null)
|
||||||
|
return (HttpSession) ctx.getExternalContext().getSession(false);
|
||||||
|
else
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpServletRequest getRequest() {
|
public static HttpServletRequest getRequest() {
|
||||||
return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
|
return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static HttpSession getSession(ServletRequest request) {
|
||||||
|
return ((HttpServletRequest) request).getSession(false);
|
||||||
|
}
|
||||||
|
|
||||||
public static void CreateSession(LoggedUserTO usr) {
|
public static void CreateSession(LoggedUserTO usr) {
|
||||||
HttpSession ses = getSession();
|
HttpSession ses = getSession();
|
||||||
ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName());
|
ses.setAttribute(SessionUtils.SESSION_VAR_USERNAME, usr.getName());
|
||||||
@@ -36,10 +44,14 @@ public class SessionUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isLogedIn() {
|
public static boolean isLogedIn() {
|
||||||
if (getUserId() == "")
|
return isLogedIn(getSession());
|
||||||
return false;
|
}
|
||||||
else
|
|
||||||
|
public static boolean isLogedIn(HttpSession session) {
|
||||||
|
if (session != null && session.getAttribute(SessionUtils.SESSION_VAR_USERID) != null)
|
||||||
return true;
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUserName() {
|
public static String getUserName() {
|
||||||
|
|||||||
@@ -48,11 +48,7 @@ public class LoginMBean extends ManagedBeanBase {
|
|||||||
|
|
||||||
if (username != null && password != null) {
|
if (username != null && password != null) {
|
||||||
try {
|
try {
|
||||||
Properties props = System.getProperties();
|
usr = this.getRemoteManagerSystemAdmin().login(username, password);
|
||||||
Context ctx = new InitialContext(props);
|
|
||||||
SystemAdminFacadeRemote remoteManager = (SystemAdminFacadeRemote) ctx.lookup("java:app/MyHealth.jar/SystemAdminFacadeBean!ejb.systemAdmin.SystemAdminFacadeRemote");
|
|
||||||
|
|
||||||
usr = remoteManager.login(username, password);
|
|
||||||
|
|
||||||
if (usr != null) {
|
if (usr != null) {
|
||||||
loggedIn = true;
|
loggedIn = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user