parent
2e73a0f30b
commit
0b7e3ed96d
9 changed files with 212 additions and 4 deletions
@ -0,0 +1,78 @@ |
|||||||
|
/** |
||||||
|
* Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). |
||||||
|
* <p> |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* <p> |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p> |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.energy.modules.auth.granter; |
||||||
|
|
||||||
|
import cn.hutool.core.bean.BeanUtil; |
||||||
|
import cn.hutool.core.codec.Base64; |
||||||
|
import cn.hutool.core.date.DateUtil; |
||||||
|
import cn.hutool.core.util.StrUtil; |
||||||
|
import cn.hutool.crypto.SecureUtil; |
||||||
|
import cn.hutool.json.JSONUtil; |
||||||
|
import com.dayu.daf.core.launch.constant.TokenConstant; |
||||||
|
import com.dayu.daf.core.log.exception.ServiceException; |
||||||
|
import com.dayu.daf.core.tool.utils.Func; |
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import org.energy.modules.auth.properties.ScadaAuthProperties; |
||||||
|
import org.energy.modules.auth.response.ShareAuthResponse; |
||||||
|
import org.energy.modules.auth.utils.AesUtil; |
||||||
|
import org.energy.modules.auth.utils.TokenUtil; |
||||||
|
import org.energy.modules.system.entity.UserInfo; |
||||||
|
import org.energy.modules.system.service.IUserService; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
/** |
||||||
|
* ScadaTokenGranter |
||||||
|
* |
||||||
|
* @author Chill |
||||||
|
*/ |
||||||
|
@Component |
||||||
|
@AllArgsConstructor |
||||||
|
public class ScadaTokenGranter implements ITokenGranter { |
||||||
|
|
||||||
|
public static final String GRANT_TYPE = "scada"; |
||||||
|
|
||||||
|
private IUserService userService; |
||||||
|
|
||||||
|
@Resource |
||||||
|
private ScadaAuthProperties properties; |
||||||
|
|
||||||
|
@Override |
||||||
|
public UserInfo grant(TokenParameter tokenParameter) { |
||||||
|
String token = tokenParameter.getArgs().getStr("token"); |
||||||
|
String tenantId = tokenParameter.getArgs().getStr("tenantId"); |
||||||
|
UserInfo userInfo = null; |
||||||
|
if (Func.isNoneBlank(token)) { |
||||||
|
byte[] tokenByte = Base64.decode(token); |
||||||
|
String decrypt = AesUtil.decrypt(tokenByte, properties.getScadaKey(), properties.getScadaIv()); |
||||||
|
if (StrUtil.isBlank(decrypt)) { |
||||||
|
throw new ServiceException("授权错误"); |
||||||
|
} |
||||||
|
ShareAuthResponse response = BeanUtil.toBean(JSONUtil.parse(decrypt), ShareAuthResponse.class); |
||||||
|
if (DateUtil.compare(new Date(), DateUtil.parseDate(response.getExpires())) > 0) { |
||||||
|
throw new ServiceException("token过期,请重新登录。"); |
||||||
|
} |
||||||
|
String account = response.getUserName(); |
||||||
|
userInfo = userService.userInfo(tenantId, account); |
||||||
|
} else { |
||||||
|
throw new ServiceException("token错误"); |
||||||
|
} |
||||||
|
return userInfo; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,25 @@ |
|||||||
|
package org.energy.modules.auth.properties; |
||||||
|
|
||||||
|
import lombok.Data; |
||||||
|
import lombok.Getter; |
||||||
|
import lombok.Setter; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
/** |
||||||
|
* 配置类 |
||||||
|
* |
||||||
|
* @author dayu |
||||||
|
*/ |
||||||
|
@Component |
||||||
|
@Data |
||||||
|
public class ScadaAuthProperties { |
||||||
|
@Value("${sso.scada.key}") |
||||||
|
private String scadaKey; |
||||||
|
|
||||||
|
@Value("${sso.scada.iv}") |
||||||
|
private String scadaIv; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,38 @@ |
|||||||
|
package org.energy.modules.auth.response; |
||||||
|
|
||||||
|
import lombok.Getter; |
||||||
|
import lombok.Setter; |
||||||
|
import lombok.ToString; |
||||||
|
|
||||||
|
/** |
||||||
|
* 分享链接鉴权返回值 |
||||||
|
* |
||||||
|
* @author dayu |
||||||
|
* @since 2024/7/2 16:54 |
||||||
|
*/ |
||||||
|
@Getter |
||||||
|
@Setter |
||||||
|
@ToString |
||||||
|
public class ShareAuthResponse { |
||||||
|
|
||||||
|
/** |
||||||
|
* 用户唯一凭证 |
||||||
|
*/ |
||||||
|
private String uid; |
||||||
|
|
||||||
|
/** |
||||||
|
* 用户名称 |
||||||
|
*/ |
||||||
|
private String userName; |
||||||
|
|
||||||
|
/** |
||||||
|
* 角色名称 |
||||||
|
*/ |
||||||
|
private String roleName; |
||||||
|
|
||||||
|
/** |
||||||
|
* 有效期 |
||||||
|
*/ |
||||||
|
private String expires; |
||||||
|
|
||||||
|
} |
@ -0,0 +1,31 @@ |
|||||||
|
package org.energy.modules.auth.utils; |
||||||
|
|
||||||
|
import cn.hutool.crypto.Mode; |
||||||
|
import cn.hutool.crypto.Padding; |
||||||
|
import cn.hutool.crypto.symmetric.AES; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
|
/** |
||||||
|
* Aes 加解密工具类 |
||||||
|
* |
||||||
|
* @author dayu |
||||||
|
**/ |
||||||
|
@Slf4j |
||||||
|
public class AesUtil { |
||||||
|
|
||||||
|
/** |
||||||
|
* 解密 |
||||||
|
*/ |
||||||
|
public static String decrypt(byte[] content, String key, String iv) { |
||||||
|
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, key.getBytes()); |
||||||
|
aes.setIv(iv.getBytes()); |
||||||
|
try { |
||||||
|
byte[] decryptedBytes = aes.decrypt(content); |
||||||
|
return new String(decryptedBytes); |
||||||
|
} catch (Exception e) { |
||||||
|
log.warn("解密失败", e); |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue