1
我在很多帖子中發現了這個問題,但是我找不到解決方案。我有一個基於領域和兩個角色映射USERS和ADMINS的身份驗證。 的登錄工作正常,但如果我嘗試使用javaee IsUserInRole總是返回false jsf
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
if(externalContext.getRemoteUser() != null && externalContext.isUserInRole("USERS")){
return true;
}
的isUserInRole始終返回false即使我登錄項。我該如何解決這個問題?
這是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>TravelDreamWeb</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>authJdbcRealm</realm-name>
<form-login-config>
<form-login-page>/index.xhtml</form-login-page>
<form-error-page>/loginError.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admins Pages</web-resource-name>
<description />
<url-pattern>/admins/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>ADMINS</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Users Pages</web-resource-name>
<description />
<url-pattern>/users/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>USERS</role-name>
</auth-constraint>
</security-constraint>
</web-app>
,這是與GlassFish的application.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-application PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Java EE Application 6.0//EN"
"http://glassfish.org/dtds/glassfish-application_6_0-1.dtd">
<glassfish-application>
<security-role-mapping>
<role-name>USERS</role-name>
<group-name>USERS</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>ADMINS</role-name>
<group-name>ADMINS</group-name>
</security-role-mapping>
<realm>authJdbcRealm</realm>
</glassfish-application>
感謝您的回答。僅供參考,對「爲什麼」的進一步解釋可能對其他用戶有所幫助。 – Phlume
謝謝,這個作品很棒! – gcc
@xavi抱歉,但現在第一次成功後,它不起作用。現在,如果我使用isUserInRole,它將返回false,甚至getRemoteUser。我無法理解,因爲它有效,我沒有改變任何東西。 – gcc