Deadbolt play java Change password the first time login

不打扰是莪最后的温柔 提交于 2019-12-25 09:16:16

问题


I have 4 group: admin, service, user, guest, admin is created in database, the first time admin login required change password, I want to redirect to form change password and only admin need that, I set redirect in onAuthFailure, but service, user, guest that not authentication in some action and redirect change password form, have a good idea, please tell me, what should I do for every role redirect an other link? , I just read document about deadbolt in 2 day, can I don't understand more, sorry for my English.

Thanks.


回答1:


Within a DeadboltHandler implementation, the onAuthFailure method can use the getSubject to get the current user, and through that, the roles held by the user.

public class MyDeadboltHandler implements DeadboltHandler {
    private final DeadboltExecutionContextProvider executionContextProvider;
    private final DeadboltAnalyzer analyzer;

    @Inject
    public MyDeadboltHandler(final ExecutionContextProvider ecProvider,
                             final DeadboltAnalyzer analyzer) {
        this.executionContextProvider = ecProvider.get();
        this.analyzer =analyzer;
    }

    public CompletionStage<Result> onAuthFailure(Http.Context context,
                                                 Optional<String> content) {
        final ExecutionContext executionContext = executionContextProvider.get();
        final ExecutionContextExecutor executor = HttpExecution.fromThread(executionContext);
        return getSubject(context).thenApplyAsync(maybeSubject ->
            maybeSubject.map(subject -> analyzer.hasRole(maybeSubject, "admin") ? /*go to admin section*/
                                                                                : /*go to non-admin section*/)
                        .orElseGet(() -> /*no user present*/),
                                   executor);
    }

    // other methods
}

Anywhere there's a comment in that example, e.g. /*go to admin section*/ you need to replace it with a Result.

There are other methods available in DeadboltAnalyzer, so you can have more complex checks than just analyzer.hasRole(maybeSubject, "admin") if necessary.



来源:https://stackoverflow.com/questions/38848416/deadbolt-play-java-change-password-the-first-time-login

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