spring-tiny-auth

一个简单权限管理框架, 支持rest风格路径、@Auth注解进行权限控制

License

License

GroupId

GroupId

com.github.lkqm
ArtifactId

ArtifactId

spring-tiny-auth
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

spring-tiny-auth
一个简单权限管理框架, 支持rest风格路径、@Auth注解进行权限控制
Project URL

Project URL

https://github.com/lkqm/spring-tiny-auth
Source Code Management

Source Code Management

https://github.com/lkqm/spring-tiny-auth

Download spring-tiny-auth

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.lkqm/spring-tiny-auth/ -->
<dependency>
    <groupId>com.github.lkqm</groupId>
    <artifactId>spring-tiny-auth</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lkqm/spring-tiny-auth/
implementation 'com.github.lkqm:spring-tiny-auth:1.0.1'
// https://jarcasting.com/artifacts/com.github.lkqm/spring-tiny-auth/
implementation ("com.github.lkqm:spring-tiny-auth:1.0.1")
'com.github.lkqm:spring-tiny-auth:jar:1.0.1'
<dependency org="com.github.lkqm" name="spring-tiny-auth" rev="1.0.1">
  <artifact name="spring-tiny-auth" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lkqm', module='spring-tiny-auth', version='1.0.1')
)
libraryDependencies += "com.github.lkqm" % "spring-tiny-auth" % "1.0.1"
[com.github.lkqm/spring-tiny-auth "1.0.1"]

Dependencies

compile (6)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar 1.5.22.RELEASE
org.springframework.boot : spring-boot-autoconfigure jar 1.5.22.RELEASE
org.springframework.boot : spring-boot-configuration-processor Optional jar 1.5.22.RELEASE
org.projectlombok : lombok Optional jar 1.18.2
org.powermock : powermock-module-junit4 jar 2.0.2
org.powermock : powermock-api-mockito2 jar 2.0.2

provided (4)

Group / Artifact Type Version
org.springframework : spring-jdbc jar 4.3.27.RELEASE
redis.clients : jedis jar 3.1.0
org.springframework.data : spring-data-redis jar 1.8.23.RELEASE
com.google.code.gson : gson jar 2.8.6

test (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 1.5.22.RELEASE
junit : junit jar 4.12
org.mockito : mockito-core jar 2.28.2

Project Modules

There are no modules declared in this project.

spring-tiny-auth

Maven Central Travis (.org) branch

一个简单权限管理框架(代码不到400行), 支持基于路径拦截、支持rest风格路径权限、注解@Auth支持多种权限认证(基于角色的权限), 不支持登录、登出、缓存等功能(与权限认证无关, 业务中可自定灵活实现).

说明: 内部基于spring webmvc框架HandlerInterceptor拦截器实现

Future

  • 十分钟上手, 代码不到400行
  • 支持基于路径的权限控制(rest风格)
  • 注解@Auth控制访问权限
  • 内置支持多种token管理方案
  • 支持JDK1.7+, Spring Boot1.5.x, Spring Boot2.x

Quick: spring-boot

  1. 添加依赖

    <dependency>
        <groupId>com.github.lkqm</groupId>
        <artifactId>spring-tiny-auth</artifactId>
        <version>${version}</version>
    </dependency>
  2. 提供自定义AuthInfoProvider

    @Component
    public class TestAuthInfoProvider implements AuthInfoProvider {
        @Override
        public AuthInfo doGetAuthInfo() {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            // !!!: 解析请求得到相关的用户信息
            // AuthInfo: 提供了权限信息(路径权限、角色、权限), 是否是超级管理员, 登录是否过期
            return null;
        }
    }
  3. 启动类添加注解: @EnableTinyAuth

  4. 配置

       tiny-auth.anno-patterns=/login                                 # 允许匿名访问的地址
       tiny-auth.authen-patterns=/logout, /admin/menu, /admin/info    # 只需要认证(登录)就能访问的地址
       tiny-auth.author-patterns=/**                                  # 需要认证授权的地址
  5. 相关异常

    当访问保护资源未登录、登录过期、或者无权限时,会抛出特定异常, 需要自定义处理异常, 异常结构如下:

    |-- AuthException                # Auth相关的基异常
    |---- AuthNotLoggedException     # 未登录
    |---- AuthExpiredException       # 登录过期
    |---- AuthPermissionException    # 无访问权限
    

路径权限

注意路径权限是取匹配Controller处理方法进行绝对相等比较, 当:/user/{id}, 不匹配例子: /user/{idx}

@Auth注解

@Auth注解value值指定了spring el表达式, 例如: @Auth("authen()"), 内部预定义表达式如下:

  • anno(): 匿名访问
  • authen(): 只需要认证(登录)
  • hasRole('admin', 'operator'): 拥有任意一个角色
  • hasAllRole('admin', 'operator'): 必须拥有所有角色
  • hasPermission('admin', 'operator'): 拥有任意一个权限
  • hasAllPermission('complaint:add', 'complaint:delete'): 必须拥有所有权限

提示: 常量类AuthConstants定义了anno(), authen()表达式

Token管理

TokenManager内置提供的token管理, 支持如下几种类型, 可通过配置tiny-auth.token.type:

  • HttpSessionTokenManager(httpsession): 基于HttpSession存储
  • JdbcTokenManager(jdbc): 基于关系型数据库, 使用需要手动创建表
  • JedisTokenManager(jedis): 基于redis存储, 使用jedis客户端连接
  • RedisTemplateTokenManager(redistemplate): 基于redis存储, 使用RedisTemplate连接

类似项目

  • light-security: Light Security是一个基于jwt的权限控制框架,支持与Spring Boot配合使用,支持Spring MVC与WebFlux

Versions

Version
1.0.1
1.0.0