From cae13e7f35fc4a08f8946a32a51503ec638e47f2 Mon Sep 17 00:00:00 2001 From: lishuaichao Date: Thu, 8 Mar 2018 10:50:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=88=B0=E6=9C=AC=E5=9C=B0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E5=8A=9F=E8=83=BD;=202.=E5=A2=9E=E5=8A=A0=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E9=AA=8C=E8=AF=81=E7=A0=81=E5=9C=A8=E8=BE=93=E9=94=99?= =?UTF-8?q?=E5=A4=9A=E6=AC=A1=E6=89=8D=E6=98=BE=E7=A4=BA=E5=8A=9F=E8=83=BD?= =?UTF-8?q?;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- renren-admin/pom.xml | 27 ++++++++ .../io/renren/common/config/ShiroConfig.java | 1 + .../java/io/renren/common/utils/Constant.java | 4 ++ .../modules/oss/cloud/CloudStorageConfig.java | 2 +- .../renren/modules/oss/cloud/OSSFactory.java | 6 +- .../oss/controller/SysOssController.java | 1 - .../sys/controller/SysLoginController.java | 65 ++++++++++++++++--- .../renren/modules/sys/shiro/ShiroUtils.java | 5 +- .../src/main/resources/application-dev.yml | 10 +-- .../src/main/resources/application.yml | 15 ++++- .../resources/statics/js/modules/oss/oss.js | 10 ++- .../src/main/resources/templates/index.html | 6 +- .../src/main/resources/templates/login.html | 26 ++++++-- .../resources/templates/modules/oss/oss.html | 3 + .../src/main/resources/application.yml | 4 +- .../src/main/resources/generator.properties | 8 +-- 16 files changed, 154 insertions(+), 39 deletions(-) diff --git a/renren-admin/pom.xml b/renren-admin/pom.xml index b1403129..90d7eda3 100644 --- a/renren-admin/pom.xml +++ b/renren-admin/pom.xml @@ -90,11 +90,38 @@ + + + + org.springframework.boot + spring-boot-devtools + provided + true + + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.9.2 + + + org.mybatis + mybatis-typehandlers-jsr310 + 1.0.2 + + ${project.artifactId} + + org.springframework.boot + spring-boot-maven-plugin + + true + + org.springframework.boot spring-boot-maven-plugin diff --git a/renren-admin/src/main/java/io/renren/common/config/ShiroConfig.java b/renren-admin/src/main/java/io/renren/common/config/ShiroConfig.java index 5ba87538..e8b62aa7 100644 --- a/renren-admin/src/main/java/io/renren/common/config/ShiroConfig.java +++ b/renren-admin/src/main/java/io/renren/common/config/ShiroConfig.java @@ -77,6 +77,7 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { shiroFilter.setUnauthorizedUrl("/"); Map filterMap = new LinkedHashMap<>(); + filterMap.put("/sys/captcha", "anon"); filterMap.put("/swagger/**", "anon"); filterMap.put("/v2/api-docs", "anon"); filterMap.put("/swagger-ui.html", "anon"); diff --git a/renren-admin/src/main/java/io/renren/common/utils/Constant.java b/renren-admin/src/main/java/io/renren/common/utils/Constant.java index 12bf3322..2c8dc5c1 100644 --- a/renren-admin/src/main/java/io/renren/common/utils/Constant.java +++ b/renren-admin/src/main/java/io/renren/common/utils/Constant.java @@ -85,6 +85,10 @@ public int getValue() { * 云服务商 */ public enum CloudService { + /** + * 服务器 + */ + LOCAL(0), /** * 七牛云 */ diff --git a/renren-admin/src/main/java/io/renren/modules/oss/cloud/CloudStorageConfig.java b/renren-admin/src/main/java/io/renren/modules/oss/cloud/CloudStorageConfig.java index 777f767b..587ed854 100644 --- a/renren-admin/src/main/java/io/renren/modules/oss/cloud/CloudStorageConfig.java +++ b/renren-admin/src/main/java/io/renren/modules/oss/cloud/CloudStorageConfig.java @@ -37,7 +37,7 @@ public class CloudStorageConfig implements Serializable { private static final long serialVersionUID = 1L; //类型 1:七牛 2:阿里云 3:腾讯云 - @Range(min=1, max=3, message = "类型错误") + @Range(min=0, max=3, message = "类型错误") private Integer type; //七牛绑定的域名 diff --git a/renren-admin/src/main/java/io/renren/modules/oss/cloud/OSSFactory.java b/renren-admin/src/main/java/io/renren/modules/oss/cloud/OSSFactory.java index 9ec9d614..6a26b65b 100644 --- a/renren-admin/src/main/java/io/renren/modules/oss/cloud/OSSFactory.java +++ b/renren-admin/src/main/java/io/renren/modules/oss/cloud/OSSFactory.java @@ -38,8 +38,10 @@ public final class OSSFactory { public static CloudStorageService build(){ //获取云存储配置信息 CloudStorageConfig config = sysConfigService.getConfigObject(ConfigConstant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class); - - if(config.getType() == Constant.CloudService.QINIU.getValue()){ + if(config.getType() == Constant.CloudService.LOCAL.getValue()){ + //return new LocalCloudStorageService(); + return SpringContextUtils.getBean("localCloudStorageService",LocalCloudStorageService.class); + }else if(config.getType() == Constant.CloudService.QINIU.getValue()){ return new QiniuCloudStorageService(config); }else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){ return new AliyunCloudStorageService(config); diff --git a/renren-admin/src/main/java/io/renren/modules/oss/controller/SysOssController.java b/renren-admin/src/main/java/io/renren/modules/oss/controller/SysOssController.java index f980ebd1..290ec9f5 100644 --- a/renren-admin/src/main/java/io/renren/modules/oss/controller/SysOssController.java +++ b/renren-admin/src/main/java/io/renren/modules/oss/controller/SysOssController.java @@ -141,7 +141,6 @@ public R upload(@RequestParam("file") MultipartFile file) throws Exception { @RequiresPermissions("sys:oss:all") public R delete(@RequestBody Long[] ids){ sysOssService.deleteBatchIds(Arrays.asList(ids)); - return R.ok(); } diff --git a/renren-admin/src/main/java/io/renren/modules/sys/controller/SysLoginController.java b/renren-admin/src/main/java/io/renren/modules/sys/controller/SysLoginController.java index a8109014..79db8d1e 100644 --- a/renren-admin/src/main/java/io/renren/modules/sys/controller/SysLoginController.java +++ b/renren-admin/src/main/java/io/renren/modules/sys/controller/SysLoginController.java @@ -24,7 +24,9 @@ import org.apache.shiro.authc.*; import org.apache.shiro.subject.Subject; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @@ -32,6 +34,7 @@ import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.IOException; @@ -46,51 +49,97 @@ public class SysLoginController { @Autowired private Producer producer; - + private String errorNumber = "errorNumber"; + @Value("${captcha.error.number:5}") + private Integer captchaErrorNumber; + + /** + * 生成验证码 + * @param response + * @throws IOException + */ @RequestMapping("captcha.jpg") public void captcha(HttpServletResponse response)throws IOException { response.setHeader("Cache-Control", "no-store, no-cache"); response.setContentType("image/jpeg"); - //生成文字验证码 String text = producer.createText(); //生成图片验证码 BufferedImage image = producer.createImage(text); //保存到shiro session ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text); - ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); } + + /** + * 登陆是否需要输入验证码 + * @return + */ + @GetMapping("/sys/captcha") + @ResponseBody + public R geterrorNumber(HttpSession session){ + //判断次数 + Integer errorNum = (Integer) session.getAttribute(errorNumber); + if (captchaErrorNumber==null){ + captchaErrorNumber = 0; + } + if(errorNum == null||errorNum < captchaErrorNumber){ + return R.ok().put("code",0); + }else{ + return R.error("请求次数过多,需要显示验证码").put("code",1); + } + } + /** * 登录 */ @ResponseBody @RequestMapping(value = "/sys/login", method = RequestMethod.POST) - public R login(String username, String password, String captcha) { + public R login(String username, String password, String captcha,HttpSession session) { String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY); - if(!captcha.equalsIgnoreCase(kaptcha)){ - return R.error("验证码不正确"); + //验证码输错次数 + Integer errorNum = (Integer) session.getAttribute(errorNumber); + if (captchaErrorNumber==null){ + captchaErrorNumber = 0; + } + if(errorNum != null&&errorNum >= captchaErrorNumber){ + if(!captcha.equalsIgnoreCase(kaptcha)){ + return R.error("验证码不正确"); + } } - + try{ Subject subject = ShiroUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, password); subject.login(token); }catch (UnknownAccountException e) { + addErrorNumber(session); return R.error(e.getMessage()); }catch (IncorrectCredentialsException e) { + addErrorNumber(session); return R.error("账号或密码不正确"); }catch (LockedAccountException e) { + addErrorNumber(session); return R.error("账号已被锁定,请联系管理员"); }catch (AuthenticationException e) { + addErrorNumber(session); return R.error("账户验证失败"); } return R.ok(); } - + + /** + * 增加验证错误次数 + */ + private void addErrorNumber(HttpSession session) { + Integer errorNum = (Integer) session.getAttribute(errorNumber); + session.setAttribute(errorNumber, (errorNum==null?0:errorNum) + 1); + } + + /** * 退出 */ diff --git a/renren-admin/src/main/java/io/renren/modules/sys/shiro/ShiroUtils.java b/renren-admin/src/main/java/io/renren/modules/sys/shiro/ShiroUtils.java index 614ca77f..3f17d64c 100644 --- a/renren-admin/src/main/java/io/renren/modules/sys/shiro/ShiroUtils.java +++ b/renren-admin/src/main/java/io/renren/modules/sys/shiro/ShiroUtils.java @@ -16,7 +16,6 @@ package io.renren.modules.sys.shiro; -import io.renren.common.exception.RRException; import io.renren.modules.sys.entity.SysUserEntity; import org.apache.shiro.SecurityUtils; import org.apache.shiro.crypto.hash.SimpleHash; @@ -74,9 +73,9 @@ public static void logout() { public static String getKaptcha(String key) { Object kaptcha = getSessionAttribute(key); - if(kaptcha == null){ + /*if(kaptcha == null){ throw new RRException("验证码已失效"); - } + }*/ getSession().removeAttribute(key); return kaptcha.toString(); } diff --git a/renren-admin/src/main/resources/application-dev.yml b/renren-admin/src/main/resources/application-dev.yml index d4a3765b..11a08e6d 100644 --- a/renren-admin/src/main/resources/application-dev.yml +++ b/renren-admin/src/main/resources/application-dev.yml @@ -5,12 +5,12 @@ spring: druid: first: #数据源1 url: jdbc:mysql://localhost:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 - username: renren - password: 123456 + username: root + password: ABCabc@123. second: #数据源2 - url: jdbc:mysql://10.10.168.18:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 - username: renren - password: 123456 + url: jdbc:mysql://localhost:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8 + username: root + password: ABCabc@123. initial-size: 10 max-active: 100 min-idle: 10 diff --git a/renren-admin/src/main/resources/application.yml b/renren-admin/src/main/resources/application.yml index 87b8b1e4..5a4d03ea 100644 --- a/renren-admin/src/main/resources/application.yml +++ b/renren-admin/src/main/resources/application.yml @@ -6,9 +6,19 @@ server: min-spare-threads: 30 port: 8080 context-path: /renren-admin - +#文件上传文件夹 +upload: + base: + dir: #D:/ziyuan/ +captcha: + error: + number: 5 # mysql spring: + mvc: + static-path-pattern: /** + resources: + static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${upload.base.dir} # 环境 dev|test|prod profiles: active: dev @@ -35,6 +45,7 @@ spring: freemarker: suffix: .html request-context-attribute: request + # Mybatis配置 #mybatis: # mapperLocations: classpath:mapper/**/*.xml @@ -78,4 +89,4 @@ mybatis-plus: call-setters-on-nulls: true #logging logging: - level: debug \ No newline at end of file + level: debug diff --git a/renren-admin/src/main/resources/statics/js/modules/oss/oss.js b/renren-admin/src/main/resources/statics/js/modules/oss/oss.js index ff900b48..a5a9cff1 100644 --- a/renren-admin/src/main/resources/statics/js/modules/oss/oss.js +++ b/renren-admin/src/main/resources/statics/js/modules/oss/oss.js @@ -4,8 +4,9 @@ $(function () { datatype: "json", colModel: [ { label: 'id', name: 'id', width: 20, key: true }, - { label: 'URL地址', name: 'url', width: 160 }, - { label: '创建时间', name: 'createDate', width: 40 } + { label: '图片', name: 'url', width: 10 ,index: 'dsource_alarm',align: "center", sortable: false, editable: false, formatter: alarmFormatter }, + { label: 'URL地址', name: 'url', width: 150 }, + { label: '创建时间', name: 'createDate', width: 40} ], viewrecords: true, height: 385, @@ -32,7 +33,12 @@ $(function () { $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); } }); + function alarmFormatter(cellvalue, options, rowdata) + { + return ''; + + } new AjaxUpload('#upload', { action: baseURL + "sys/oss/upload", name: 'file', diff --git a/renren-admin/src/main/resources/templates/index.html b/renren-admin/src/main/resources/templates/index.html index 124f77c8..a119f048 100644 --- a/renren-admin/src/main/resources/templates/index.html +++ b/renren-admin/src/main/resources/templates/index.html @@ -27,9 +27,9 @@