之前给大家介绍了sso的相关知识点和集成方案,考虑到每个系统所属行业的不同,这边针对于不同行业做了一些统一的sso单点登录界面模板,使用fileupload多文件上传+OSS阿里云存储方案。
1. 阿里云oss存储Utils
-
public
class
AliyunUtils {
-
-
private
static
AliyunUtils aliyun;
-
private
AliyunUtils() {
-
-
}
-
-
public
static
synchronized
AliyunUtils getInstance(){
-
if
(aliyun==
null
){
-
aliyun=
new
AliyunUtils();
-
}
-
return
aliyun;
-
}
-
-
-
-
-
-
-
public
void
uploadByte(
byte
[] fileByte, String fileKey){
-
-
OSSClient ossClient =
new
OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
-
-
ossClient.putObject(CloudConstant.BUCKET, fileKey,
new
ByteArrayInputStream(fileByte));
-
-
ossClient.shutdown();
-
}
-
-
-
-
-
-
-
public
void
uploadInputStream(InputStream inputStream, String fileKey){
-
-
OSSClient ossClient =
new
OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
-
-
ossClient.putObject(CloudConstant.BUCKET, fileKey, inputStream);
-
-
ossClient.shutdown();
-
}
-
-
-
-
-
-
public
void
deleteFile(String fileKey){
-
-
OSSClient ossClient =
new
OSSClient(CloudConstant.ENDPOINT, CloudConstant.ACCESSKEYID, CloudConstant.ACCESSKEYSECRET);
-
-
ossClient.deleteObject(CloudConstant.BUCKET, fileKey);
-
-
ossClient.shutdown();
-
}
-
-
-
@SuppressWarnings
(
"restriction"
)
-
public
static
byte
[] BASE64DecoderStringToByte(String imgStr)
-
{
-
if
(imgStr ==
null
)
-
return
null
;
-
sun.misc.BASE64Decoder decoder =
new
sun.misc.BASE64Decoder();
-
try
{
-
-
byte
[] b = decoder.decodeBuffer(imgStr);
-
return
b;
-
}
catch
(Exception e){
-
return
null
;
-
}
-
}
-
-
public
static
void
main(String[] args) {
-
-
}
-
}
2. 阿里云配置常量类(可以配置到数据库或者properties,后面会更新配置方式)
-
public
class
CloudConstant {
-
-
-
public
static
final
String ENDPOINT =
"http://oss-cn-shanghai.aliyuncs.com"
; //外网访问域名
-
-
public
static
final
String ACCESSKEYID = "12345678qwertyu;
-
public
static
final
String ACCESSKEYSECRET =
"1234567890WERTYUIO"
;
-
public
static
final
String BUCKET =
"huiyi-bucket"
;
-
-
-
public
static
final
String BACK_IMG_INFO_PATH =
"sso/backageImg/"
;
-
-
}
3. sso templateController类
-
public
String save(SsoTemplate ssoTemplate, Model model, RedirectAttributes redirectAttributes,
@RequestParam
(value =
"file"
, required =
false
) MultipartFile file) {
-
if
(!beanValidator(model, ssoTemplate)) {
-
return
form(ssoTemplate, model);
-
}
-
-
String fileName = String.valueOf(
new
Date().getTime());
-
String staff =
""
;
-
String fileKey =
""
;
-
-
if
(file.getSize() !=
0
) {
-
staff = FilenameUtils.getExtension(file.getOriginalFilename());
-
fileKey = CloudConstant.BACK_IMG_INFO_PATH + fileName +
"."
+ staff;
-
-
AliyunUtils.getInstance().deleteFile(fileKey);
-
-
try
{
-
AliyunUtils.getInstance().uploadInputStream(file.getInputStream(), fileKey);
-
ssoTemplate.setImg(fileKey);
-
}
catch
(IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
ssoTemplateService.save(ssoTemplate);
-
addMessage(redirectAttributes,
"保存模板成功"
);
-
return
"redirect:"
+ Global.getAdminPath() +
"/sso/ssoTemplate"
;
-
}
4. 界面显示效果
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69984951/viewspace-2733701/
,如需转载,请注明出处,否则将追究法律责任。