<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ### 統一工具類的意義 api-commons幫助我們簡化每一行代碼,減少每一個方法,然代碼可讀性、容錯性更高。 提供數據源切換功能,日志記錄功能,獲取當前用戶功能,oauth白名單功能。 另外集成Hutool,完整文檔查看[hutool-doc](https://hutool.cn/docs)。 ### hutool 提供類哪些功能 一個Java基礎工具類,對文件、流、加密解密、轉碼、正則、線程、XML等JDK方法進行封裝,組成各種Util工具類,同時提供以下組件: * hutool-aop JDK動態代理封裝,提供非IOC下的切面支持 * hutool-bloomFilter 布隆過濾,提供一些Hash算法的布隆過濾 * hutool-cache 緩存 * hutool-core 核心,包括Bean操作、日期、各種Util等 * hutool-cron 定時任務模塊,提供類Crontab表達式的定時任務 * hutool-crypto 加密解密模塊 * hutool-db JDBC封裝后的數據操作,基于ActiveRecord思想 * hutool-dfa 基于DFA模型的多關鍵字查找 * hutool-extra 擴展模塊,對第三方封裝(模板引擎、郵件、Servlet、二維碼等) * hutool-http 基于HttpUrlConnection的Http客戶端封裝 * hutool-log 自動識別日志實現的日志門面 * hutool-script 腳本執行封裝,例如Javascript * hutool-setting 功能更強大的Setting配置文件和Properties封裝 * hutool-system 系統參數調用封裝(JVM信息等) * hutool-json JSON實現 * hutool-captcha 圖片驗證碼實現 * hutool-poi 針對POI中Excel的封裝 可以根據需求對每個模塊單獨引入,也可以通過引入hutool-all方式引入所有模塊。 api-commons代碼清單 **日志工具類** ``` @Aspect @Order(-1) // 保證該AOP在@Transactional之前執行 public class LogAnnotationAspect { private static final Logger logger = LoggerFactory.getLogger(LogAnnotationAspect.class); @Around("@annotation(ds)") public Object logSave(ProceedingJoinPoint joinPoint, LogAnnotation ds) throws Throwable { // 請求流水號 String transid = getRandom(); // 記錄開始時間 long start = System.currentTimeMillis(); // 獲取方法參數 String url = null; String httpMethod = null; Object result = null; List<Object> httpReqArgs = new ArrayList<Object>(); SysLog log = new SysLog(); log.setCreateTime(new Date()); LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(); if (loginAppUser != null) { log.setUsername(loginAppUser.getUsername()); } MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); LogAnnotation logAnnotation = methodSignature.getMethod().getDeclaredAnnotation(LogAnnotation.class); log.setModule(logAnnotation.module() + ":" + methodSignature.getDeclaringTypeName() + "/" + methodSignature.getName()); Object[] args = joinPoint.getArgs();// 參數值 url = methodSignature.getDeclaringTypeName() + "/"+ methodSignature.getName(); for (Object object : args) { if (object instanceof HttpServletRequest) { HttpServletRequest request = (HttpServletRequest) object; url = request.getRequestURI(); httpMethod = request.getMethod(); } else if (object instanceof HttpServletResponse) { } else { httpReqArgs.add(object); } } try { String params = JSONObject.toJSONString(httpReqArgs); log.setParams(params); // 打印請求參數參數 logger.info("開始請求,transid={}, url={} , httpMethod={}, reqData={} ", transid, url, httpMethod, params); } catch (Exception e) { logger.error("記錄參數失敗:{}", e.getMessage()); } try { // 調用原來的方法 result = joinPoint.proceed(); log.setFlag(Boolean.TRUE); } catch (Exception e) { log.setFlag(Boolean.FALSE); log.setRemark(e.getMessage()); throw e; } finally { CompletableFuture.runAsync(() -> { try { if (logAnnotation.recordRequestParam()) { LogService logService = SpringUtils.getBean(LogServiceImpl.class); logService.save(log); } } catch (Exception e) { logger.error("記錄參數失敗:{}", e.getMessage()); } }); // 獲取回執報文及耗時 logger.info("請求完成, transid={}, 耗時={}, resp={}:", transid, (System.currentTimeMillis() - start), result == null ? null : JSON.toJSONString(result)); } return result; } /** * 生成日志隨機數 * * @return */ public String getRandom() { int i = 0; StringBuilder st = new StringBuilder(); while (i < 5) { i++; st.append(ThreadLocalRandom.current().nextInt(10)); } return st.toString() + System.currentTimeMillis(); } } ``` **多數據源工具類** ``` /** * 切換數據源Advice */ @Aspect @Order(-1) // 保證該AOP在@Transactional之前執行 public class DataSourceAspect { private static final Logger logger = LoggerFactory.getLogger(DataSourceAspect.class); @Before("@annotation(ds)") public void changeDataSource(JoinPoint point, DataSource ds) throws Throwable { String dsId = ds.name(); try { DataSourceKey dataSourceKey = DataSourceKey.valueOf(dsId); DataSourceHolder.setDataSourceKey(dataSourceKey); } catch (Exception e) { logger.error("數據源[{}]不存在,使用默認數據源 > {}", ds.name(), point.getSignature()); } } @After("@annotation(ds)") public void restoreDataSource(JoinPoint point, DataSource ds) { logger.debug("Revert DataSource : {transIdo} > {}", ds.name(), point.getSignature()); DataSourceHolder.clearDataSourceKey(); } } ``` **分頁工具類** ``` /** * @author 作者 owen E-mail: 624191343@qq.com * @version 創建時間:2017年11月12日 上午22:57:51 * 分頁實體類 * total 總數 * code 是否成功 * data 當前頁結果集 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor public class PageResult<T> implements Serializable { private static final long serialVersionUID = -275582248840137389L; private Long count; private int code; private List<T> data; } ``` **oauth認證成功后獲取用戶信息工具類** ``` /** * @author 作者 owen E-mail: 624191343@qq.com * @version 創建時間:2017年11月12日 上午22:57:51 * 用戶實體綁定spring security */ @Getter @Setter public class LoginAppUser extends SysUser implements UserDetails { /** * */ private static final long serialVersionUID = -3685249101751401211L; private Set<SysRole> sysRoles; private Set<String> permissions; /*** * 權限重寫 */ @JsonIgnore @Override public Collection<? extends GrantedAuthority> getAuthorities() { Collection<GrantedAuthority> collection = new HashSet<>(); if (!CollectionUtils.isEmpty(sysRoles)) { sysRoles.parallelStream().forEach(role -> { if (role.getCode().startsWith("ROLE_")) { collection.add(new SimpleGrantedAuthority(role.getCode())); } else { collection.add(new SimpleGrantedAuthority("ROLE_" + role.getCode())); } }); } if (!CollectionUtils.isEmpty(permissions)) { permissions.parallelStream().forEach(per -> { collection.add(new SimpleGrantedAuthority(per)); }); } return collection; } @Override public boolean isAccountNonExpired() { return true; } @Override public boolean isAccountNonLocked() { return true; } @Override public boolean isCredentialsNonExpired() { return true; } @Override public boolean isEnabled() { return getEnabled(); } } ``` **認證授權白名單配置** ``` /** * @author 作者 owen E-mail: 624191343@qq.com * @version 創建時間:2017年11月12日 上午22:57:51 url白名單處理 application.yml中配置需要放權的url白名單 */ //@ConfigurationProperties(prefix = "permit") @ConfigurationProperties(prefix = "security.oauth2") public class PermitUrlProperties { /** * 監控中心和swagger需要訪問的url */ private static final String[] ENDPOINTS = { "/**/actuator/health", "/**/actuator/env", "/**/actuator/metrics/**", "/**/actuator/trace", "/**/actuator/dump", "/**/actuator/jolokia", "/**/actuator/info", "/**/actuator/logfile", "/**/actuator/refresh", "/**/actuator/flyway", "/**/actuator/liquibase", "/**/actuator/heapdump", "/**/actuator/loggers", "/**/actuator/auditevents", "/**/actuator/env/PID", "/**/actuator/jolokia/**", "/**/actuator/archaius/**", "/**/actuator/beans/**", "/**/actuator/httptrace", "/**/v2/api-docs/**", "/**/swagger-ui.html", "/**/swagger-resources/**", "/**/webjars/**", "/**/druid/**", "/**/actuator/hystrix.stream","/**/actuator/hystrix.stream**/**", "/**/turbine.stream", "/**/turbine.stream**/**", "/**/hystrix","/**/hystrix.stream", "/**/hystrix/**" ,"/**/hystrix/**/**" ,"/**/proxy.stream/**" ,"/**/favicon.ico" }; private String[] ignored; /** * 需要放開權限的url * * @param urls * 自定義的url * @return 自定義的url和監控中心需要訪問的url集合 */ public String[] getIgnored() { if (ignored == null || ignored.length == 0) { return ENDPOINTS; } List<String> list = new ArrayList<>(); for (String url : ENDPOINTS) { list.add(url); } for (String url : ignored) { list.add(url); } return list.toArray(new String[list.size()]); } public void setIgnored(String[] ignored) { this.ignored = ignored; } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看