|
@@ -1,7 +1,10 @@
|
|
package com.imed.costaccount.common.shiro;
|
|
package com.imed.costaccount.common.shiro;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
+import com.imed.costaccount.common.exception.CostException;
|
|
import com.imed.costaccount.common.token.JwtUtil;
|
|
import com.imed.costaccount.common.token.JwtUtil;
|
|
|
|
+import com.imed.costaccount.model.User;
|
|
|
|
+import com.imed.costaccount.service.UserService;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
import org.apache.shiro.authc.AuthenticationInfo;
|
|
import org.apache.shiro.authc.AuthenticationInfo;
|
|
@@ -13,6 +16,8 @@ import org.apache.shiro.subject.PrincipalCollection;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 认证和授权在这里操作
|
|
* 认证和授权在这里操作
|
|
*/
|
|
*/
|
|
@@ -23,9 +28,13 @@ public class OAuth2Realm extends AuthorizingRealm {
|
|
@Autowired
|
|
@Autowired
|
|
private JwtUtil jwtUtil;
|
|
private JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private UserService userService;
|
|
|
|
+
|
|
|
|
|
|
/**
|
|
/**
|
|
* 是否需要认证
|
|
* 是否需要认证
|
|
|
|
+ *
|
|
* @param token
|
|
* @param token
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@@ -36,6 +45,7 @@ public class OAuth2Realm extends AuthorizingRealm {
|
|
|
|
|
|
/**
|
|
/**
|
|
* 登录认证
|
|
* 登录认证
|
|
|
|
+ *
|
|
* @param token token
|
|
* @param token token
|
|
* @return
|
|
* @return
|
|
* @throws AuthenticationException
|
|
* @throws AuthenticationException
|
|
@@ -45,38 +55,28 @@ public class OAuth2Realm extends AuthorizingRealm {
|
|
String principal = (String) token.getPrincipal();
|
|
String principal = (String) token.getPrincipal();
|
|
if (StrUtil.isEmpty(principal)) {
|
|
if (StrUtil.isEmpty(principal)) {
|
|
log.error("当前请求未携带token");
|
|
log.error("当前请求未携带token");
|
|
-// throw new PfmException(PfmExceptionEnum.ForbiddenException);
|
|
|
|
- // TODO: 2021/7/23
|
|
|
|
|
|
+ throw new CostException(499, "登录失败,请重新登录");
|
|
}
|
|
}
|
|
int userId = jwtUtil.getUserId(principal);
|
|
int userId = jwtUtil.getUserId(principal);
|
|
-// SysEmployee employee = employeeService.queryById(userId);
|
|
|
|
-// if (Objects.isNull(employee)) {
|
|
|
|
-// log.error("当前token{}", principal);
|
|
|
|
-// throw new PfmException(499, "登录失败,请重新登录");
|
|
|
|
-// }
|
|
|
|
|
|
+ User user = userService.getById(userId);
|
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
|
+ log.error("当前token{}", principal);
|
|
|
|
+ throw new CostException(499, "登录失败,请重新登录");
|
|
|
|
+ }
|
|
|
|
|
|
- // TODO: 2021/7/23
|
|
|
|
- SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(null, principal, getName());
|
|
|
|
|
|
+ SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, principal, getName());
|
|
return info;
|
|
return info;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
* 授权对象
|
|
* 授权对象
|
|
|
|
+ *
|
|
* @param principals
|
|
* @param principals
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
|
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
|
-// SysEmployee employee = (SysEmployee) principals.getPrimaryPrincipal();
|
|
|
|
-
|
|
|
|
- // todo 根据后续需求完成动态配置
|
|
|
|
-// String managerRole = employee.getManagerRole();
|
|
|
|
-// String[] split = managerRole.split(StrUtil.COMMA);
|
|
|
|
-// Set<String> permissions = Arrays.stream(split).map(RoleEnum::getRole).collect(Collectors.toSet());
|
|
|
|
-// SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
|
|
|
-// info.setStringPermissions(permissions);
|
|
|
|
-// return info;
|
|
|
|
- // TODO: 2021/7/23
|
|
|
|
|
|
+ // TODO: 2021/7/23 暂未设置权限
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|