使用Custom Authenticator,Mobilefirst 7.0保护Java适配器失败

时间:2015-04-18 12:33:57

标签: ibm-mobilefirst mobilefirst-adapters

我正在关注并使用Custom Authenticator and Login Module中的示例代码和Java SQL Adapter中的UserAdapter。

我希望在经过身份验证后获取用户列表。

我配置authenticationConfig.xml文件

<realms>
    <realm loginModule="CustomLoginModule" name="CustomAuthenticatorRealm">
        <className>com.mypackage.MyCustomAuthenticator</className>
    </realm>
</realms>

<loginModules>
    <loginModule name="CustomLoginModule">
        <className>com.mypackage.MyCustomLoginModule</className>
    </loginModule>
</loginModules>

我配置Java适配器UserAdapterResource.java文件

@GET
@Produces("application/json")
@OAuthSecurity(scope="CustomAuthenticatorRealm")
public Response getAllUsers() throws SQLException{
    JSONArray results = new JSONArray();
    Connection con = ds.getConnection();
    PreparedStatement getAllUsers = con.prepareStatement("SELECT * FROM users");
    ResultSet data = getAllUsers.executeQuery();

    while(data.next()){
        JSONObject item = new JSONObject();
        item.put("userId", data.getString("userId"));
        item.put("firstName", data.getString("firstName"));
        item.put("lastName", data.getString("lastName"));
        item.put("password", data.getString("password"));

        results.add(item);
    }

    getAllUsers.close();
    con.close();

    return Response.ok(results).build();
}

但是当我在客户端调用上面的过程时,它仍然会返回没有身份验证要求的响应,而它必须显示登录模块

1 个答案:

答案 0 :(得分:1)

从您的代码中,您只有CustomAuthenticatorRealm领域的挑战处理程序。为什么不更新您的适配器并使用相同的域保护它,而不是使用myRealm

更新了UserAdapterResource.java骨架

@Path("/")
public class UserAdapterResource {
    // ... 

    @POST
    @OAuthSecurity(scope="CustomAuthenticatorRealm")
    public Response createUser(@FormParam("userId") String userId, 
                                @FormParam("firstName") String firstName, 
                                @FormParam("lastName") String lastName, 
                                @FormParam("password") String password) 
                                        throws SQLException{
        // ...
    }

    @GET
    @Produces("application/json")
    @Path("/{userId}")
    public Response getUser(@PathParam("userId") String userId) throws SQLException{
        // ...
    }

    @GET
    @Produces("application/json")
    @OAuthSecurity(scope="CustomAuthenticatorRealm")
    public Response getAllUsers() throws SQLException{
        // ...
    }

    // it's a good practice to protect this operation
    @PUT
    @Path("/{userId}")
    @OAuthSecurity(scope="CustomAuthenticatorRealm")
    public Response updateUser(@PathParam("userId") String userId, 
                                @FormParam("firstName") String firstName, 
                                @FormParam("lastName") String lastName, 
                                @FormParam("password") String password) 
                                        throws SQLException{
        // ...

    }

    // it's a good practice to protect this operation
    @DELETE
    @Path("/{userId}")
    @OAuthSecurity(scope="CustomAuthenticatorRealm")
    public Response deleteUser(@PathParam("userId") String userId) throws SQLException {
        // ...
    }

}

通过这些更改,当应用程序启动时,它将显示登录表单以进行身份​​验证,然后再显示用户列表。

<强>更新

Java Adapter保护使用OAuth,因此MobileFirst服务器会发出令牌进行身份验证。此令牌具有到期的生命周期。退出领域不会影响令牌。

根据您的需求实现此功能的一种方法是将令牌的TTL(生存时间)降低到10秒或15秒(或任何您想要的)。您可以通过在expirationInSeconds内的登录模块中设置authenticationConfig.xml属性来执行此操作。

<强> authenticationConfig.xml

    <!-- token will expire 10 seconds after being issued -->
    <loginModule name="CustomLoginModule" expirationInSeconds="10">
        <className>com.mypackage.MyCustomLoginModule</className>
    </loginModule>

如果应用程序通过适配器调用或任何其他方法连接到服务器已经过了10秒,那么用户将需要重新进行身份验证。