add 权限增加是否外网展示

This commit is contained in:
lijiarao
2023-08-23 16:38:11 +08:00
parent f6d02117df
commit 7290aea066
13 changed files with 146 additions and 22 deletions
@@ -23,6 +23,8 @@ public interface CommonAPI {
*/
Set<String> queryUserAuths(String username);
Set<String> queryUserAuthsExternal(String username);
/**
* 3根据 id 查询数据库中存储的 DynamicDataSourceModel
*
@@ -0,0 +1,25 @@
package com.jero.common.constant.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* @author liJiaRao外部
* @date 2023-08-23 9:02
*/
@Getter
@AllArgsConstructor
public enum LoginType {
INSIDE("inside"), EXTERNAL("external");
private String type;
public static LoginType getEnum(String type){
for (LoginType value : values()) {
if (value.getType().equals(type)) {
return value;
}
}
return null;
}
}
@@ -2,6 +2,7 @@ package com.jero.common.system.vo;
import java.util.Date;
import com.jero.common.constant.enums.LoginType;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
@@ -140,4 +141,6 @@ public class LoginUser {
private String companyId;
private LoginType loginType;
}
@@ -1,5 +1,6 @@
package com.jero.config.shiro;
import com.jero.common.constant.enums.LoginType;
import org.apache.shiro.authc.AuthenticationToken;
/**
@@ -11,11 +12,17 @@ public class JwtToken implements AuthenticationToken {
private static final long serialVersionUID = 1L;
private String token;
private LoginType loginType;
public JwtToken(String token) {
public JwtToken(String token,LoginType loginType) {
this.token = token;
this.loginType = loginType;
}
public LoginType getLoginType() {
return loginType;
}
@Override
public Object getPrincipal() {
return token;
@@ -1,6 +1,6 @@
package com.jero.config.shiro;
import cn.hutool.crypto.SecureUtil;
import com.jero.common.constant.enums.LoginType;
import com.jero.common.constant.enums.LoginUserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
@@ -12,7 +12,6 @@ import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import com.jero.common.api.CommonAPI;
import com.jero.common.constant.CacheConstant;
import com.jero.common.constant.CommonConstant;
import com.jero.common.system.util.JwtUtil;
import com.jero.common.system.vo.LoginUser;
@@ -41,13 +40,21 @@ public class ShiroRealm extends AuthorizingRealm {
@Lazy
@Resource
private RedisUtil redisUtil;
@Override
public String getName() {
return LoginType.INSIDE.getType();
}
/**
* 必须重写此方法,不然Shiro会报错
*/
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof JwtToken;
//if (token instanceof JwtToken){
// return LoginType.INSIDE.getType().equals(((JwtToken) token).getLoginType());
//} else {
// return false;
//}
}
/**
@@ -61,10 +68,8 @@ public class ShiroRealm extends AuthorizingRealm {
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
log.info("===============Shiro权限认证开始============ [ roles、permissions]==========");
String username = null;
if (principals != null) {
LoginUser sysUser = (LoginUser) principals.getPrimaryPrincipal();
username = sysUser.getUsername();
}
LoginUser sysUser = (LoginUser) principals.getPrimaryPrincipal();
username = sysUser.getUsername();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 设置用户拥有的角色集合,比如“admin,test”
@@ -73,7 +78,13 @@ public class ShiroRealm extends AuthorizingRealm {
info.setRoles(roleSet);
// 设置用户拥有的权限集合,比如“sys:role:add,sys:user:add”
Set<String> permissionSet = commonAPI.queryUserAuths(username);
LoginType loginType = sysUser.getLoginType();
Set<String> permissionSet;
if (LoginType.INSIDE.equals(loginType)){
permissionSet = commonAPI.queryUserAuths(username);
}else {
permissionSet = commonAPI.queryUserAuthsExternal(username);
}
info.addStringPermissions(permissionSet);
System.out.println(permissionSet);
log.info("===============Shiro权限认证成功==============");
@@ -98,6 +109,9 @@ public class ShiroRealm extends AuthorizingRealm {
}
// 校验token有效性
LoginUser loginUser = this.checkUserTokenIsEffect(token);
JwtToken jwtToken = (JwtToken) auth;
LoginType loginType = jwtToken.getLoginType();
loginUser.setLoginType(loginType);
return new SimpleAuthenticationInfo(loginUser, token, getName());
}
@@ -161,14 +175,17 @@ public class ShiroRealm extends AuthorizingRealm {
}
return false;
}else{
String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + token));
String cacheToken = String.valueOf(redisUtil.get(CommonConstant.PREFIX_USER_TOKEN + userName));
if (oConvertUtils.isNotEmpty(cacheToken)) {
if (!token.equals(cacheToken)){
throw new AuthenticationException("已在其它设备登录,请重新登录!");
}
// 校验token有效性
if (!JwtUtil.verify(cacheToken, userName, passWord)) {
//String newAuthorization = JwtUtil.sign(userName, passWord);
String newAuthorization = JwtUtil.sign(userName, passWord);
// 设置超时时间
//redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, newAuthorization);
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME *2 / 1000);
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + userName, newAuthorization);
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + userName, JwtUtil.EXPIRE_TIME *2 / 1000);
log.debug("——————————用户在线操作,更新token保证不掉线—————————jwtTokenRefresh——————— "+ token);
}
return true;
@@ -1,6 +1,7 @@
package com.jero.config.shiro.filters;
import cn.hutool.core.util.StrUtil;
import com.jero.common.constant.enums.LoginType;
import com.jero.common.util.AuthenticationUtils;
import com.jero.common.util.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
@@ -73,8 +74,9 @@ public class JwtFilter extends BasicHttpAuthenticationFilter {
authenticationUtils.check(pushToken,publicKey);
}else {
// update-end--Author:lvdandan Date:20210105 forJT-355 OA聊天添加token验证,获取token参数
JwtToken jwtToken = new JwtToken(token);
String externalTerminalShow = httpServletRequest.getHeader("externalTerminalShow");
LoginType loginType = LoginType.getEnum(externalTerminalShow);
JwtToken jwtToken = new JwtToken(token,loginType);
// 提交给realm进行登入,如果错误他会抛出异常并被捕获
getSubject(request, response).login(jwtToken);
// 如果没有抛出异常则代表登入成功,返回true