shaun 介绍
基于 pac4j 的安全框架
特性:
- 功能简单,易于集成。
- 无 session。
- 使用凭证(token) 进行身份验证(默认是 jwt)。
- 前后端不分离下,能依托pac4j的各种client快速集成三方登录(redirect跳转那种),例如oauth(qq,微信) 和 cas。
模块简介:
- shaun-core: 核心包
- shaun-spring-boot-starter: spring boot 快速启动包
- shaun-test-cookie: 前后端不分离下的测试演示
- shaun-test-stateless-cookie: 前后端分离下使用cookie存token的测试演示
- shaun-test-stateless-header: 前后端分离下使用request header携带token的测试演示
使用方法
-
引入shaun-spring-boot-starter。
com.baomidou shaun-spring-boot-starter ${version} -
登录后设置相关信息到SecurityManager。
@Service
@AllArgsConstructor
public class LoginServiceImpl implements LoginService {private final SecurityManager securityManager; @Override @Transactional public String login() { // 登录成功后把用户角色权限信息存储到profile中 final JwtProfile profile = new JwtProfile(); profile.setId(userId.toString()); if (roles.contains(AdminConst.SUPER_ADMIN)) { isAdmin = true; } else { profile.setPermissions(permissionService.selectPermissionsByUserId(userId).stream() .filter(x -> Objects.nonNull(x.getCode())).map(SysPermission::getCode).collect(Collectors.toSet())); profile.setRoles(new HashSet<>(roles)); } final String token = securityManager.login(profile, isAdmin); return token; }
-
设置yml启动信息。
shaun:
salt: d614a4fdff6540c1a5b730afc5f9cc8f #非必须
exclude-path:
- /v2/api-docs
- /swagger-resources
- /doc.html
exclude-branch:
- /wechat-auth
- /webjars -
注解拦截。
类似于shiro,shaun也默认支持使用注解在controller上拦截。
相关的注解有 @HasAuthorization @HasPermission @HasRole 。
@HasPermission(value = {"add","edit"},logical = Logical.BOTH) //同时存在 @HasPermission(value = {"add","edit"},logical = Logical.ANY) //任一存在
- 前后端交互。
默认配置下 前端登录后需要把后端返回的token存下,后续接口的请求头带上Authorization。
后端可以通过 TokenProfile profile = ProfileHolder.getProfile(); 获得用户信息。
shaun 官网
https://gitee.com/baomidou/shaun
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。