Coder Social home page Coder Social logo

rabbitnoteeth / vertx-auth-simple Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 0.0 91 KB

适用于resuful api的vertx-web动态权限认证框架 (a dynamic auth handler for vertx web which based on vertx-auth-common)

Java 100.00%
vertx auth restful kotlin vertx-web

vertx-auth-simple's Introduction

vertx-auth-simple

基于vertx-auth-common和resuful api的vertx动态权限认证框架
适用场景:用户登陆后动态从数据库或其他介质中获取权限信息

配置使用(how to use)




1.提供SimpleAuthProvider接口实现(provide a implement of SimpleAuthProvider)



public class MyAuthProviderImpl implements SimpleAuthProvider {

    /**
     * 
     * @param authInfo 用户登录时提交的验证信息
     * @param resultHandler 处理用户验证结果
     *                      验证成功,创建SimpleAuthUser对象(该对象中包含用户权限信息)交由resultHandler处理
     *                      验证失败,自定义包含失败原因的异常,交由resultHandler处理
    */
    public void authenticate(JsonObject authInfo, Handler> resultHandler) {
        /** for example:
        if(authInfo is right){
            SimpleAuthUser user = new SimpleAuthUser();
            user.setAuthProvider(this);
            user.setPrincipal(...);
            user.appendPermissions(...);
            resultHandler.handle(Future.succeededFuture(user));
        }else{
            resultHandler.handle(Future.failedFuture(a Throwable));
        } 
        */
    }

}




2.配置router(config the Router)


// 创建配置类
SimpleAuthOptions options = new SimpleAuthOptions();
// 定义不需要拦截的访问
List annoPermissions = listOf("GET:/articles/page","GET:/articleClassifies/tree")
options.setAnnoPermissions(annoPermissions);

// 创建并注册权限处理器
SimpleAuthHandler authHandler = SimpleAuthHandler.create(this.vertx,new myAuthProviderImpl(),options);

router.route().handler(authHandler);




3.用户登录(user login)


Subject subject = SubjectUtil.getSubject(routingContext);
subject.login(context,params, res -> {
    if(res.succeeded()){
        context.response().write(...);
    }else{
        context.response().write(...);
    }
    context.end();
});




4.获取Subject实体(get the subject of user)


Subject subject = SubjectUtil.getSubject(routingContext);




4.注意
在vertx-auth-simple中,默认的权限字符串生成格式为 "请求方法:请求地址",如 "GET:/articles/page" ,
同时,默认的权限字符串处理策略对静态资源文件进行了处理,允许访问静态资源(具体实现可查看DefaultPermissionStrategyImpl.class) ,
所以可以放心地将SimpleAuthHandler声明在静态资源处理器StaticHandler之前


5.扩展功能
下面所有扩展接口在vertx-auth-simple中都提供了默认实现(可零配置开箱即用),可以根据场景需要来自定义各个接口实现

1> 实现PermissionStrategy接口,自定义权限字符串的生成格式和校验规则


SimpleAuthOptions options = new SimpleAuthOptions();
options.setPermissionStrategy(new MyPermissionStrategyImpl());

2> 实现SessionIdStrategy接口,自定义sessionId处理策略


 SimpleAuthOptions options = new SimpleAuthOptions();
 options.setSessionIdStrategy(new MySessionIdStrategyImpl());

3> 实现SessionPersistStrategy接口,自定义session持久化方式


 SimpleAuthOptions options = new SimpleAuthOptions();
 options.setSessionPersistStrategy(new MySessionPersistStrategyImpl());

4> 实现RememberMePersistStrategy接口,自定义rememberMe信息的持久化方式


 SimpleAuthOptions options = new SimpleAuthOptions();
 options.setRememberMePersistStrategy(new MyRememberMePersistStrategyImpl());

5> 实现RealmStrategy接口,自定义权限验证成功或者失败后的处理动作


  SimpleAuthOptions options = new SimpleAuthOptions();
  options.setRealmStrategy(new MyRealmStrategyImpl());

vertx-auth-simple's People

Contributors

rabbitnoteeth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.