Fetch the repository succeeded.
基于 pac4j-jwt 的 WEB 安全组件
pac4j-jwt
,默认使用 jwt
进行身份认证jwt
配置,默认签名
+加密
yml
配置即可进行安全拦截(只有登陆需要你自己写)session
(pac4j
提供的类除外)pac4j
的各种client快速集成三方登录(redirect跳转那种),例如oauth(qq,微信) 和 cas。<dependency>
<groupId>com.baomidou</groupId>
<artifactId>shaun-spring-boot-starter</artifactId>
<version>Latest Version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>spring-boot-version</version>
</dependency>
application.yml
shaun:
salt: 32位字符串,不配置则每次启动不一样导致重启前登录的用户token失效
stateless: true # 默认 true,表示是前后端分离的
session-on: false # 默认 false,表示不用session(会影响pac4j的类对session的操作)
login-url: /login # 如果配置,则会加入 exclude-path 内,前后端不分离下鉴权失败会被重定向到此
token-location: header # 默认 header,token 的存取位置
exclude-path: # 排除具体的路径
- /v2/api-docs
- /swagger-resources
- /doc.html
exclude-branch: # 排除以此路径开头
- /webjars
expire-time: 1d # 不设置默认永久有效
# 更多配置参见源码
expire-time: jwt 有效时长(
d
后面只支持三个(s
,m
,h
)之一)10s : 表示10秒内有效
10m 结尾: 表示10分钟内有效
10h 结尾: 表示10小时内有效
1d : 表示有效时间到第二天 00:00:00
2d1h : 表示有效时间到第三天 01:00:00
@Service
@AllArgsConstructor
public class LoginServiceImpl implements LoginService {
private final SecurityManager securityManager;
@Override
@Transactional
public String login() {
// 登录成功后把用户角色权限信息存储到profile中
final TokenProfile profile = new TokenProfile();
profile.setId(userId.toString());
//profile.addRole(role:String);
//profile.setRoles(roles:Set);
//profile.addPermission(permission:String);
//profile.setPermissions(permissions:Set);
//profile.addAttribute("key","value");
final String token = securityManager.login(profile);
//如果选择token存cookie里,securityManager.login会进行自动操作
return token;
}
@HasAuthorization
,@HasPermission
,@HasRole
支持注解在
method
上以及class
上
例:
@HasPermission(value = {"add", "edit"}, logical = Logical.BOTH) //权限必须同时存在
@HasPermission(value = {"add", "edit"}, logical = Logical.ANY) //权限任一存在(默认)
TokenProfile profile = ProfileHolder.getProfile();
Sign in for post a comment
Comments ( 7 )