如何检查Jersey web服务中经过身份验证的用户的角色



环境: Jersey 1.17 with GlassFish 3.1.2.2, JDK 6u31.

web.xml安全段:

<security-constraint>
    <display-name>SSL transport</display-name>
    <web-resource-collection>
        <web-resource-name>Secure Area</web-resource-name>
        <description></description>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>USER</role-name>
        <role-name>ADMIN</role-name>
    </auth-constraint>
    <user-data-constraint>
        <description></description>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>myRealm</realm-name>
</login-config>

sun-web.xml :

<security-role-mapping>
    <role-name>USER</role-name>
    <group-name>USER</group-name>
</security-role-mapping>
<security-role-mapping>
    <role-name>ADMIN</role-name>
    <group-name>ADMIN</group-name>
</security-role-mapping>

我正在使用注入来获取SecurityContext实例:

@Context
private SecurityContext sec;

现在我应该能够使用SecurityContext#isUserInRole(String role)方法检查web服务方法中的角色,但是无论指定什么参数,返回值始终是false

我能够使用SecurityContext#getUserPrincipal()方法检索经过身份验证的用户名。

这里可能有什么问题?还有别的办法吗?

我也必须单独指定web.xml中的安全角色:

<!-- after <login-config /> -->
<security-role>
    <role-name>USER</role-name>
</security-role>
<security-role>
    <role-name>ADMIN</role-name>
</security-role>

最新更新