2012-04-12 113 views
3

我被@BalusC以下答案JSF 2.0: How to get the URL that is entered in the browser's address bar以限制用户未登录谁的网页过滤器实现JSF - 为受限制的页面

筛选:

public class RestrictPageFilter implements Filter{ 
    FilterConfig fc; 

    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
     fc=filterConfig; 
    } 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest httpreq = (HttpServletRequest) request; 
     HttpServletResponse httpres = (HttpServletResponse) response; 
     if (httpreq.getUserPrincipal() == null) { 
      httpreq.getSession().setAttribute("from", httpreq.getRequestURI()); 
      httpres.sendRedirect("/pages/login.xhtml"); 
     } else { 
      chain.doFilter(request, response); 
     } 
    } 

    @Override 
    public void destroy() { 
     // TODO Auto-generated method stub 
    } 
} 

的web.xml:

<security-constraint> 
    <web-resource-collection> 
     <web-resource-name>Admin pages</web-resource-name> 
     <url-pattern>/admin/*</url-pattern> 
     <url-pattern>/restricted/*</url-pattern> 
     <http-method>GET</http-method> 
      <http-method>POST</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>ADMIN</role-name> 
    </auth-constraint> 
    </security-constraint> 

    <security-constraint> 
    <web-resource-collection> 
     <web-resource-name>User pages</web-resource-name> 
     <url-pattern>/restricted/*</url-pattern> 
     <http-method>GET</http-method> 
      <http-method>POST</http-method> 
    </web-resource-collection> 
    <auth-constraint> 
     <role-name>ADMIN</role-name> 
     <role-name>USER</role-name> 
    </auth-constraint> 
    </security-constraint> 

    <!--login-config> 
    <auth-method>FORM</auth-method> 
    <realm-name>jdbc-realm</realm-name> 
    <form-login-config> 
     <form-login-page>/pages/login.xhtml</form-login-page> 
     <form-error-page>/pages/error.xhtml</form-error-page> 
    </form-login-config> 
    </login-config--> 

    <filter> 
     <filter-name>RestrictPageFilter</filter-name> 
     <filter-class>gov.denis.chanceryweb5.filter.RestrictPageFilter</filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>RestrictPageFilter</filter-name> 
     <url-pattern>/restricted/*</url-pattern> 
    </filter-mapping> 

与GlassFish的web.xml

<glassfish-web-app> 
<security-role-mapping> 
    <role-name>ADMIN</role-name> 
    <group-name>ADMIN</group-name> 
    </security-role-mapping> 
    <security-role-mapping> 
    <role-name>USER</role-name> 
    <group-name>USER</group-name> 
    </security-role-mapping> 

境界GlassFish中的GUI控制台: enter image description here

当访问我的web应用程序,浏览器我认为这出于某种原因?为什么?

enter image description here

+1

指定不知道这是否与“认证”的事情,但你的过滤器不应该与* .xhtml所有页面映射(它会在每一页上被调用包括索引)。你应该只将它映射到我认为索引不是的受限页面上。 – Fallup 2012-04-12 15:55:21

+0

因此我应该将“login.xtml”移出我想限制的页面? – Melissaa 2012-04-12 15:57:09

+1

登录页面应该超出安全限制。你的数据库表是什么样的?您已经拥有了它们,并且正在尝试将它适配到GlassFish中的JDBCRealm? – rbento 2012-04-12 15:59:40

回答

1

你看到用BASIC认证方法相关的对话框。

您目前已将web.xml文件的login-config元素注释掉......以便配置未被应用。

GlassFish的3台服务器有一个默认的登录,配置,用于在用户部署的应用程序指定安全性约束,但不指定登录配置...

有效的登录,配置为您的应用程序实际上看起来像这样

<login-config> 
    <auth-method>BASIC</auth-method> 
    <realm-name>file</realm-name> 
    </login-config> 

默认的登录配置,那么在glassfish3/glassfish/domains/<your domain name here>/config/default-web.xml

+0

感谢您的回答。我注释了登录配置,因为回答问题我链接说我需要用过滤器来替换它。所以它错了吗? – Melissaa 2012-04-13 06:10:21

+0

@Melissaa:我从来不知道BalusC提供的答案是不幸的是,每台服务器都允许在不受规范限制的区域“增加值”,这是TC和GF不同的地方,如果你希望GlassFish的行为类似于Tomcat(在你可以从config/default-web.xml中删除默认的login-config元素 – vkraemer 2012-04-13 16:25:27

+0

我注释掉了默认的login-config,但它看起来像'request.login(name,pass)'不工作没有web.xml中的login-config – Melissaa 2012-04-17 09:00:10