Play 2 Java authenticate plugin - HTTP status code response instead of redirect

左心房为你撑大大i 提交于 2020-01-05 04:03:32

问题


We are using Play 2 authenticate plugin for a REST API and I would like to simply return 200 or 403 for login attempts.

The plugin's code looks like this:

public static Result loginAndRedirect(final Context context,
        final AuthUser loginUser) {
    storeUser(context.session(), loginUser);
    return Controller.redirect(getJumpUrl(context));
}

Is there any way to avoid the redirect without forking the plugin project?


回答1:


I ended up handling this at the controller:

public static Result login() {   
    Result r = MyUsernamePasswordAuthProvider.handleLogin(ctx());
    if (r instanceof Redirect && PlayAuthenticate.getUser(session()) != null) {
        return ok();
    }
    return forbidden();
}

There might be better ways to do this though.




回答2:


I just stumbled in the same scenario, and as nico_ekito pointed out, this can be achieved by extending PlayAuthenticate.Resolver and overriding:

@Override
    public Call afterAuth() {
        return routes.Application.restAfterAuth();
    }

So you can return any route of your app.



来源:https://stackoverflow.com/questions/13507605/play-2-java-authenticate-plugin-http-status-code-response-instead-of-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!