2013-06-20 1 views
0

Я следую ссылке http://docs.oracle.com/cd/E19798-01/821-1841/bncby/index.html, чтобы узнать аутентификацию на основе форм. Я сделал то, что требуется для проверки подлинности на основе форм, но я всегда беру false при проверке роли. Вот моя конфигурация. Что мне не хватает?GlassFish Form Based Authenticaion

AutBean.java

public void login(){ 
     HttpServletRequest request = getHttpServletRequest(); 
     boolean intutRole=request.isUserInRole("TutorialUser"); 
     System.out.println("intutRole:"+intutRole); 
     System.out.println(request.getContentLength()); 
    } 

    protected HttpServletRequest getHttpServletRequest(){ 
     FacesContext fc = getFacesContext(); 
     ExternalContext ec = fc.getExternalContext(); 
     HttpServletRequest request = (HttpServletRequest)ec.getRequest(); 
     return request; 
    } 

    protected FacesContext getFacesContext(){ 
     FacesContext fc = FacesContext.getCurrentInstance(); 
     return fc; 
    } 

enter image description here
web.xml

<!-- Form Based Authentication --> 
    <security-constraint> 
     <display-name>Constraint1</display-name> 
     <web-resource-collection> 
      <web-resource-name>wrcoll</web-resource-name> 
      <description/> 
      <url-pattern>/*</url-pattern> 
     </web-resource-collection> 
     <auth-constraint> 
      <description/> 
      <role-name>TutorialUser</role-name> 
     </auth-constraint> 
    </security-constraint> 


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

    <security-role> 
     <description/> 
     <role-name>TutorialUser</role-name> 
    </security-role> 

ответ

0

После добавления призывающую request.login (имя пользователя, пароль), он работает.

public void login() { 
    HttpServletRequest request = getHttpServletRequest(); 
    try { 
     request.login(username, password);   
    } catch (ServletException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    boolean intutRole = request.isUserInRole("TutorialUser"); 
    System.out.println("intutRole:" + intutRole); 
    System.out.println(request.getContentLength()); 
} 

Убедитесь, что вы используете Servlet 3.0. HttpServletRequest не имеет прокси-сервера, который используется для сервлетов. Таким образом, вы должны добавить следующую зависимость в pom.xml

<dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
</dependency>