UserPrincipal的参考代码
package com.qbsea.mysboot2mybatis.mysboot2shirojwt.api.v1.model;
import com.qbsea.mysboot2mybatis.mysboot2shirojwt.LockablePrincipal;
import lombok.Getter;
import lombok.Setter;
import org.apache.shiro.SecurityUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Setter
@Getter
public class UserPrincipal implements LockablePrincipal {
private Long id;
private String mobile;
private String password;
private boolean locked;//实现LockablePrincipal里的方法
@Override
public String getSubject() {
return getId().toString();
}
@Override
public String getSecret() {
return getPassword().toString();
}
// @Override //可以添加自定义权限
// public Collection<String> getRoles() {
// List<String> roles = new ArrayList<>();
// if (isLeader()) {
// roles.add(User.ROLE_LEADER);
// }
// if (StringUtils.isNotBlank(mobile) && isCraftsman()) {
// roles.add(User.ROLE_CRAFTSMAN);
// }
// return roles;
// }
//获取当前线程的Principal
public static UserPrincipal current(){
return (UserPrincipal)SecurityUtils.getSubject().getPrincipal();
}
//判断是否登录
public static boolean isLogin(){
return SecurityUtils.getSubject().isAuthenticated();
}
}