Note that there are some explanatory texts on larger screens.

plurals
  1. POFilters Help - Getting 2 filters to work with each other
    primarykey
    data
    text
    <p>I have written 2 filters 1 for a normal user and 1 for a admin yet you have to be admin to login. Here is the source for both of my filters:</p> <pre><code>public class newFilter implements Filter { String UUIDInDB; String UUIDInCookie; public void init(FilterConfig filterConfig) throws ServletException { //To change body of implemented methods use File | Settings | File Templates. } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse res = (HttpServletResponse) servletResponse; Cookie[] cookies = req.getCookies(); UUIDInCookie = getCookieValue(cookies,"pubweb", "noCookie"); if(UUIDInCookie.equals("noCookie")){ Cookie cookie = new Cookie("pubweb","noCookie"); cookie.setMaxAge(1); res.addCookie(cookie); res.sendRedirect("../Login.jsp"); return ; } checkDatabase(); if(UUIDInCookie.equals(UUIDInDB)){ filterChain.doFilter(servletRequest, servletResponse); System.out.println("Is allowed thorugh"); } else if(UUIDInCookie.equals("noCookie")){ res.sendRedirect("../Login.jsp"); System.out.println("Isn't allowed thorugh"); } else { res.sendRedirect("../Login.jsp"); System.out.println("Isn't allowed thorugh"); } } public void destroy() { //To change body of implemented methods use File | Settings | File Templates. } public void checkDatabase(){ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } /* The next lines allow you to change the username and password for the password. */ String username = "username"; String password = "password"; /* The following line is the url. This can be changed to bring in to line with the database. */ String dbURL = "jdbc:mysql://localhost/hpsgdb?user=" + username + "&amp;password=" + password; /* This line connects to the database to the information presented earlier. */ java.sql.Connection myConnection = null; try { myConnection = DriverManager.getConnection(dbURL); System.out.println("Connected to Database."); } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } /* The next line creates a query on the database. The query is that you want exacuted is on the next line. */ Statement stat = null; try { stat = (Statement) myConnection.createStatement(); } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (NullPointerException e){ e.printStackTrace(); } try { ResultSet rs; rs = stat.executeQuery("SELECT * from uuid where uuid='" + UUIDInCookie + "';"); System.out.println("Executed Query."); int count = 0; while(rs.next()) UUIDInDB = rs.getString("uuid") ; System.out.println(UUIDInDB); rs.close(); myConnection.close(); } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (NullPointerException e){ e.printStackTrace(); } } public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) throws IOException { int length = cookies.length; System.out.println(length); try{ for(int i=0; i&lt;length; i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) { System.out.println(cookies.length); return(cookie.getValue()); } else { return defaultValue; } } } catch (NullPointerException e){ e.printStackTrace(); HttpServletResponse res = null; res.sendRedirect("../Login.jsp"); } return(defaultValue); } } </code></pre> <p>Other Filter:</p> <pre><code>public class adminFilter implements Filter { String UUIDInDB; String UUIDInCookie; int role; public void destroy() { } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) servletRequest; HttpServletResponse res = (HttpServletResponse) servletResponse; Cookie[] cookies = req.getCookies(); UUIDInCookie = getCookieValue(cookies,"pubweb", "noCookie"); // role = Integer.parseInt(getCookieValue(cookies,"pubwebRole", "2")); if(UUIDInCookie.equals("noCookie")){ Cookie cookie = new Cookie("pubweb","noCookie"); cookie.setMaxAge(1); res.addCookie(cookie); res.sendRedirect("../Login.jsp"); return ; } checkDatabase(); if(UUIDInCookie.equals(UUIDInDB) &amp;&amp; role == 1){ chain.doFilter(servletRequest, servletResponse); } else if(UUIDInCookie.equals("noCookie")){ res.sendRedirect("../Login.jsp"); } else if (role == 2){ res.sendRedirect("/"); } else { res.sendRedirect("../Login.jsp"); } } public void init(FilterConfig config) throws ServletException { } public void checkDatabase(){ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } /* The next lines allow you to change the username and password for the password. */ String username = "username"; String password = "password"; /* The following line is the url. This can be changed to bring in to line with the database. */ String dbURL = "jdbc:mysql://localhost/hpsgdb?user=" + username + "&amp;password=" + password; /* This line connects to the database to the information presented earlier. */ java.sql.Connection myConnection = null; try { myConnection = DriverManager.getConnection(dbURL); System.out.println("Connected to Database."); } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } /* The next line creates a query on the database. The query is that you want exacuted is on the next line. */ Statement stat = null; try { stat = (Statement) myConnection.createStatement(); } catch (SQLException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (NullPointerException e){ e.printStackTrace(); } try { ResultSet rs; rs = stat.executeQuery("SELECT * from uuid where uuid='" + UUIDInCookie + "';"); System.out.println("Executed Query."); int count = 0; while(rs.next()) { UUIDInDB = rs.getString("uuid") ; role = rs.getInt("role"); } System.out.println(UUIDInDB); System.out.println("Role =" + role); rs.close(); myConnection.close(); } catch (SQLException e) { e.printStackTrace(); } catch (NullPointerException e){ e.printStackTrace(); } } public static String getCookieValue(Cookie[] cookies, String cookieName, String defaultValue) throws IOException { int length = cookies.length; System.out.println(length); try{ for(int i=0; i&lt;length; i++) { Cookie cookie = cookies[i]; if (cookieName.equals(cookie.getName())) { System.out.println(cookies.length); return(cookie.getValue()); } else { return defaultValue; } } } catch (NullPointerException e){ e.printStackTrace(); HttpServletResponse res = null; res.sendRedirect("../Login.jsp"); } return(defaultValue); } } </code></pre> <p>HEre is my web xml file:</p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;SecurityFilter&lt;/filter-name&gt; &lt;filter-class&gt;filters.newFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;SecurityFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/add/addAuthor.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addAuthor&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addConference.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addConference&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addJournal.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addJournal&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addWorkshop.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addWorkshop&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/index.jsp&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;filter&gt; &lt;filter-name&gt;AdminFilter&lt;/filter-name&gt; &lt;filter-class&gt;filters.adminFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;!-- &lt;filter-mapping&gt; &lt;filter-name&gt;AdminFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/add/addAuthor.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addAuthor&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addConference.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addConference&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addJournal.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addJournal&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addWorkshop.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addWorkshop&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/index.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addConfJour.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addConfJourn&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addUser.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addUser&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addTag.jsp&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/addTag&lt;/url-pattern&gt; &lt;url-pattern&gt;/add/indexAdmin.jsp&lt;/url-pattern&gt; &lt;/filter-mapping&gt;--&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;AdminFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/add/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>Thanks in Advance Dean</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload