Is there a way to conditionally apply annotations?

假装没事ソ 提交于 2019-12-08 15:27:18

问题


In my java-play application I have the annotation @RequiresAuthentication(clientName = "CasClient") inside my controller.

I only want to authenticate users in my production environment.

How can I apply annotations conditionally?

If the way I'm approaching authentication is wrong, what is the conventional way of doing CAS authentication only on production in a java play application?


回答1:


You could implement authenticators to authenticate users. you could you write your authentication logic in your authenticator implementation.

Play already comes with a built in authenticator action, which we will extend to add our logic. We will call this authenticator Secured.

import play.*;
import play.mvc.*;
import play.mvc.Http.*;

import models.*;

public class Secured extends Security.Authenticator {

    @Override
    public String getUsername(Context ctx) {
        return ctx.session().get("email");
    }

    @Override
    public Result onUnauthorized(Context ctx) {
        return redirect(routes.Application.login());
    }
}

We have implemented two methods here. getUsername is used to get the username of the current logged in user. In our case this is the email address, that we set in the email attribute in the session when the user logged in. If this method returns a value, then the authenticator considers the user to be logged in, and lets the request proceed. If however the method returns null, then the authenticator will block the request, and instead invoke onUnathorized, which we have implemented to redirect to our login screen. You could implement your own business logic for user verify user.

Now let’s use this authenticator. In Application.java, add the @Security.Authenticated annotation with our authenticator to the index method:

import play.mvc.Controller;
import play.mvc.Result;

public class Application extends Controller {

     @Security.Authenticated(Secured.class)
      public static Result index() {
        return ok(index.render(
         Project.findInvolving(request().username()), 
           Task.findTodoInvolving(request().username()),
            User.find.byId(request().username())
         )); 
     }
}

Refs:Play Implementing Authenticator Example



来源:https://stackoverflow.com/questions/36948271/is-there-a-way-to-conditionally-apply-annotations

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