From e1c935a25422b282b16473fff1c29d722d9e53dd Mon Sep 17 00:00:00 2001 From: wangliwen Date: Mon, 18 Apr 2022 13:59:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=AD=98=E5=82=A8=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E4=B8=8A=E4=BC=A0=E4=B8=8E=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- renren-admin/pom.xml | 7 ++ .../controller/FileUploadController.java | 60 ++++++++++ .../interceptor/IdentityInterceptor.java | 111 ++++++++++++++++++ .../renren/common/interceptor/Validator.java | 83 +++++++++++++ .../interceptor/YaweiSSOProperties.java | 18 +++ .../modules/security/config/ShiroConfig.java | 10 +- .../modules/security/config/WebMvcConfig.java | 1 + .../src/main/resources/application.yml | 8 +- .../src/main/resources/yaweisso.properties | 3 + 9 files changed, 298 insertions(+), 3 deletions(-) create mode 100644 renren-admin/src/main/java/io/renren/common/controller/FileUploadController.java create mode 100644 renren-admin/src/main/java/io/renren/common/interceptor/IdentityInterceptor.java create mode 100644 renren-admin/src/main/java/io/renren/common/interceptor/Validator.java create mode 100644 renren-admin/src/main/java/io/renren/common/interceptor/YaweiSSOProperties.java create mode 100644 renren-admin/src/main/resources/yaweisso.properties diff --git a/renren-admin/pom.xml b/renren-admin/pom.xml index 238aad94..879574ca 100644 --- a/renren-admin/pom.xml +++ b/renren-admin/pom.xml @@ -27,6 +27,7 @@ 5.22.0 2.2.9 2.7.1 + 2.0.2 @@ -200,6 +201,12 @@ IJPay-AliPay ${IJPay.version} + + + com.yawei.oav2 + yawei-pso + ${yawei-pso.version} + diff --git a/renren-admin/src/main/java/io/renren/common/controller/FileUploadController.java b/renren-admin/src/main/java/io/renren/common/controller/FileUploadController.java new file mode 100644 index 00000000..902ff819 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/common/controller/FileUploadController.java @@ -0,0 +1,60 @@ +package io.renren.common.controller; + + +import io.renren.common.utils.Result; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; + +@RestController +public class FileUploadController { + @Value("${resource.path}") + private String uploadPath; + @Value("${resource.root_url}") + private String root_url; + @Value("${server.servlet.context-path}") + private String context_path; + + private static Logger logger = LoggerFactory.getLogger(FileUploadController.class); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/"); + + @PostMapping("/upload") + public Result upload(@RequestParam("file") MultipartFile uploadFile, + HttpServletRequest request) { + logger.info("上传文件:" + uploadFile.getOriginalFilename()); + String format = sdf.format(new Date()); + File folder = new File(uploadPath + "upload" + File.separator + format); + logger.info(uploadPath + format); + if (!folder.isDirectory()) { + folder.mkdirs(); + } + // 对上传的文件重命名,避免文件重名 + String oldName = uploadFile.getOriginalFilename(); + String newName = UUID.randomUUID().toString() + + oldName.substring(oldName.lastIndexOf("."), oldName.length()); + try { + // 文件保存 + uploadFile.transferTo(new File(folder, newName)); + + // 返回上传文件的访问路径 + String filePath = request.getScheme() + "://" + root_url + + ":" + request.getServerPort() + context_path + "/upload/" + format + newName; + return new Result().ok(filePath); + } catch (IOException e) { + return new Result().error(e.getMessage()); + } + } + +} diff --git a/renren-admin/src/main/java/io/renren/common/interceptor/IdentityInterceptor.java b/renren-admin/src/main/java/io/renren/common/interceptor/IdentityInterceptor.java new file mode 100644 index 00000000..5e4920f2 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/common/interceptor/IdentityInterceptor.java @@ -0,0 +1,111 @@ +package io.renren.common.interceptor; + +import com.yawei.pso.PSORequest; +import com.yawei.pso.SSOResponse; +import com.yawei.pso.TicketManager; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.ModelAndView; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.net.URLEncoder; +import java.util.Iterator; +import java.util.Map.Entry; + +/** + * 亚微 sso拦截 + */ +@Component +public class IdentityInterceptor implements HandlerInterceptor { + private static Logger logger = LoggerFactory.getLogger(IdentityInterceptor.class); + + public final static String SEESION_USER = "seesion_user"; + + @Autowired + private YaweiSSOProperties yaweiSSOProperties; + + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + logger.info("==============执行顺序: 1、preHandle================"); + // 获取当前请求的url + String requestUri = request.getRequestURI(); + + Validator validator = Validator.getInstance(); + + String strResponse = request.getParameter(yaweiSSOProperties.getSsoKey()); + if (StringUtils.isEmpty(strResponse)) { + TicketManager tm = new TicketManager(); + if (!tm.LoadTicket(request)) { + PSORequest psoRequest = new PSORequest(request); + String requeststr = psoRequest.CreateHash(); + + String keeperUrl = yaweiSSOProperties.getKeeperUrl(); + keeperUrl = keeperUrl + "?" + yaweiSSOProperties.getSsoKey() + "=" + + URLEncoder.encode(requeststr, "UTF-8"); + response.sendRedirect(keeperUrl); + return false; + } + } else { + // 如果服务器端通过认证后,会返回后执行改操作,然后写入cookie + SSOResponse ssoResp = new SSOResponse(strResponse); + TicketManager tm = ssoResp.CreatePSOTicket(); + if (tm == null) { + PSORequest psoRequest = new PSORequest(request); + String requeststr = psoRequest.CreateHash(); + + String keeperUrl = yaweiSSOProperties.getKeeperUrl(); + keeperUrl = keeperUrl + "?" + yaweiSSOProperties.getSsoKey() + "=" + + URLEncoder.encode(requeststr, "UTF-8"); + response.sendRedirect(keeperUrl); + } else { + String domainName = yaweiSSOProperties.getDomain(); + tm.SaveTicket(response, domainName); + Iterator> iterator = request + .getParameterMap().entrySet().iterator(); + StringBuffer param = new StringBuffer(); + int i = 0; + while (iterator.hasNext()) { + Entry entry = (Entry) iterator + .next(); + if (entry.getKey().equals(yaweiSSOProperties.getSsoKey())) + continue; + else { + i++; + if (i == 1) + param.append("?").append(entry.getKey()) + .append("="); + else + param.append("&").append(entry.getKey()) + .append("="); + + if (entry.getValue() instanceof String[]) { + param.append(((String[]) entry.getValue())[0]); + } else { + param.append(entry.getValue()); + } + } + } + response.sendRedirect(requestUri + param.toString()); + return false; + } + } + validator.SetUserTicket(request); + return true; + } + + @Override + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { + logger.info("==============执行顺序: 2、postHandle================"); + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + logger.info("==============执行顺序: 3、afterCompletion================"); + } +} diff --git a/renren-admin/src/main/java/io/renren/common/interceptor/Validator.java b/renren-admin/src/main/java/io/renren/common/interceptor/Validator.java new file mode 100644 index 00000000..b02bc2f7 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/common/interceptor/Validator.java @@ -0,0 +1,83 @@ +package io.renren.common.interceptor; + +import com.yawei.pso.TicketManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpSession; + +/** + * 验证器 + */ +public class Validator { + private static Logger logger = LoggerFactory.getLogger(Validator.class); + private static ThreadLocal validatorHolder = new ThreadLocal() { + + protected Validator initialValue() { + return new Validator(); + } + + }; + + // 当前请求的session + private HttpSession session = null; + + // 当前的请求 + private HttpServletRequest request = null; + + private Validator() { + + } + + public static Validator getInstance() { + return validatorHolder.get(); + } + + /** + * 执行初始化 + * + * @param httpRequest + */ + public void init(HttpServletRequest httpRequest) { + this.request = httpRequest; + this.session = request.getSession(); + } + + /** + * 将凭证身份加入到session + * + * @param httpRequest + */ + public void SetUserTicket(HttpServletRequest httpRequest) { + try { + if (httpRequest.getSession() + .getAttribute(IdentityInterceptor.SEESION_USER) == null) { + TicketManager ticket = new TicketManager(); + if (ticket.LoadTicket(httpRequest)) { + // 登录用户姓名 + String userName = ticket.getUserName(); + // 登录用户账号 + String userAccount = ticket.getUserID(); + // 登录用户标识 + String userGuid = ticket.getADGUID(); + logger.info("===userName===" + userName); + logger.info("===userAccount===" + userAccount); + logger.info("===userGuid===" + userGuid); + } + } else { + + } + } catch (Exception ex) { + logger.error("", ex); + } + } + + /** + * 清除session + */ + public void cancel() { + this.session = null; + } + +} diff --git a/renren-admin/src/main/java/io/renren/common/interceptor/YaweiSSOProperties.java b/renren-admin/src/main/java/io/renren/common/interceptor/YaweiSSOProperties.java new file mode 100644 index 00000000..b189b099 --- /dev/null +++ b/renren-admin/src/main/java/io/renren/common/interceptor/YaweiSSOProperties.java @@ -0,0 +1,18 @@ +package io.renren.common.interceptor; + + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + + +@Data +@Component +@PropertySource("classpath:/yaweisso.properties") +@ConfigurationProperties(prefix = "sso") +public class YaweiSSOProperties { + private String domain; + private String ssoKey; + private String keeperUrl; +} diff --git a/renren-admin/src/main/java/io/renren/modules/security/config/ShiroConfig.java b/renren-admin/src/main/java/io/renren/modules/security/config/ShiroConfig.java index 37c2edb5..c07aa65d 100644 --- a/renren-admin/src/main/java/io/renren/modules/security/config/ShiroConfig.java +++ b/renren-admin/src/main/java/io/renren/modules/security/config/ShiroConfig.java @@ -19,13 +19,12 @@ import java.util.Map; /** * Shiro的配置文件 - * */ @Configuration public class ShiroConfig { @Bean - public DefaultWebSessionManager sessionManager(){ + public DefaultWebSessionManager sessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setSessionValidationSchedulerEnabled(false); sessionManager.setSessionIdUrlRewritingEnabled(false); @@ -72,6 +71,13 @@ public class ShiroConfig { filterMap.put("/front/**", "anon"); filterMap.put("/applyRecord/**", "anon"); filterMap.put("/bsabilityrecord/**", "anon"); + + /** + * 资源上传 + */ + filterMap.put("/upload", "anon"); + filterMap.put("/upload/**", "anon"); + filterMap.put("/**", "oauth2"); shiroFilter.setFilterChainDefinitionMap(filterMap); diff --git a/renren-admin/src/main/java/io/renren/modules/security/config/WebMvcConfig.java b/renren-admin/src/main/java/io/renren/modules/security/config/WebMvcConfig.java index 641e67f5..df41a497 100644 --- a/renren-admin/src/main/java/io/renren/modules/security/config/WebMvcConfig.java +++ b/renren-admin/src/main/java/io/renren/modules/security/config/WebMvcConfig.java @@ -37,6 +37,7 @@ public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new AliPayInterceptor()).addPathPatterns("/pay/alipay/**"); +// registry.addInterceptor(new IdentityInterceptor()); } @Override diff --git a/renren-admin/src/main/resources/application.yml b/renren-admin/src/main/resources/application.yml index 038ccbb6..5ce8003a 100644 --- a/renren-admin/src/main/resources/application.yml +++ b/renren-admin/src/main/resources/application.yml @@ -1,3 +1,7 @@ +#上传的静态资源配置 +resource: + root_url: 127.0.0.1 + path: E:\liwen\ # Tomcat server: tomcat: @@ -38,12 +42,14 @@ spring: min-idle: 5 # 连接池中的最小空闲连接 activiti: check-process-definitions: false + resources: + static-locations: classpath:/static,classpath:/public,file:${resource.path} fdfs: so-timeout: 600000 connect-timeout: 6000 - tracker-list: #TrackerList参数,支持多个 + tracker-list: #TrackerList参数,支持多个 - 192.168.10.10:22122 # 是否开启redis缓存 true开启 false关闭 diff --git a/renren-admin/src/main/resources/yaweisso.properties b/renren-admin/src/main/resources/yaweisso.properties new file mode 100644 index 00000000..bb802a06 --- /dev/null +++ b/renren-admin/src/main/resources/yaweisso.properties @@ -0,0 +1,3 @@ +sso.domain=yw.com.cn +sso.ssoKey=SSOToken +sso.keeperUrl=http://127.0.0.1:9090/renren-admin/sys/user/123 \ No newline at end of file