我在哪里可以定义关注角色的用户?

时间:2016-09-05 12:42:50

标签: java jersey

我正在使用https://jersey.java.net/documentation/latest/security.html#annotation-based-security示例16.5和16.6

中定义的RolesAllowedDynamicFeature

我将其注册为:

@ApplicationPath("/demo")
public class App extends ResourceConfig {
    public App() {
        this.packages("com.bla bla bla package");
        register(RolesAllowedDynamicFeature.class);
    }
}

然后在Java类中我这样做:

@Path("request")
@RolesAllowed({ "admin", "thirdPartyDeveloper" })
public class DemoService {
}

我的问题是,在哪里说明属于管理员角色的用户以及属于第三方开发者角色的用户?

我可以在哪里设置凭证?

1 个答案:

答案 0 :(得分:2)

这取决于您的身份验证机制的样子。

Java EE安全

如果您依赖于servlet容器提供的标准Java EE Web应用程序安全机制,则可以通过应用程序的web.xml描述符配置身份验证。您很可能需要在容器中定义域并使用web.xml元素在<real-name>描述符中引用它。类似的东西:

<security-constraint>
    <web-resource-collection>
        <url-pattern>/rest/admin/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>
<security-constraint>
    <web-resource-collection>
        <url-pattern>/rest/orders/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>customer</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>my-default-realm</realm-name>
</login-config>

在这种方法中,您的领域会告诉容器用户是否在角色中。

要了解有关Java EE安全性的更多信息,请查看Java EE Tutorial

自定义身份验证

另一方面,当您执行this answer中描述的自定义身份验证时,您可以使用SecurityContext的自定义实现。在这种情况下,您需要针对LDAP,数据库,文件等手动对用户进行身份验证。

对于自定义验证,您可以使用ContainerRequestFilter,如下所示:

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        // Authenticate the user: How the authentications will be performed is up to you
        // If the authentication failed, abort the request with a 401 status code
        // If the authentication succeeded, you know who your user claims to be

        final SecurityContext securityContext = requestContext.getSecurityContext();
        requestContext.setSecurityContext(new SecurityContext() {

            @Override
            public Principal getUserPrincipal() {
                 return new Principal() {
                    @Override
                    public String getName() {
                        // Return the user name here
                        return null;
                    }
                };
            }

            @Override
            public boolean isUserInRole(String role) {
                // Return if the user is in a role
                return true;
            }

            @Override
            public boolean isSecure() {
                return securityContext.isSecure();
            }

            @Override
            public String getAuthenticationScheme() {
                return "Custom";
            }
        });
    }
}

如何执行身份验证是您的业务。 HTTP身份验证的推荐方法是在请求的Authorization标头中发送凭据。 ContainerRequestContext API可让您访问请求的详细信息。

在这种方法中,需要编写代码来确定用户是否在角色中。

注意:请注意,您不会依赖会话。必须为每个请求执行身份验证/授权过程。

相关问题