shaun基于 pac4j 的安全框架

联合创作 · 2023-09-30 08:42

基于 pac4j 的安全框架


特性:



  1. 功能简单,易于集成。

  2. 无 session。

  3. 使用凭证(token) 进行身份验证(默认是 jwt)。

  4. 前后端不分离下,能依托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的测试演示


使用方法



  1. 引入shaun-spring-boot-starter。



<dependency>
<groupId>com.baomidou</groupId>
<artifactId>shaun-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>


  1. 登录后设置相关信息到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;
}


  1. 设置yml启动信息。



shaun:
salt: d614a4fdff6540c1a5b730afc5f9cc8f #非必须
exclude-path:
- /v2/api-docs
- /swagger-resources
- /doc.html
exclude-branch:
- /wechat-auth
- /webjars


  1. 注解拦截。


类似于shiro,shaun也默认支持使用注解在controller上拦截。


相关的注解有 @HasAuthorization @HasPermission @HasRole 。



@HasPermission(value = {"add","edit"},logical = Logical.BOTH) //同时存在
@HasPermission(value = {"add","edit"},logical = Logical.ANY) //任一存在


  1. 前后端交互。


默认配置下 前端登录后需要把后端返回的token存下,后续接口的请求头带上Authorization。


后端可以通过 TokenProfile profile = ProfileHolder.getProfile(); 获得用户信息。


 



浏览 14
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报