Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

从renren更新项目 #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions renren-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,38 @@
</exclusion>
</exclusions>
</dependency>

<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<!--LocalDateTime-->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>

</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.renren.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
* @author 李帅超
* @Description: SpringMVC整合LocalDateTime用到jackson
* @date 2018-03-06 11:01
*/
@Configuration
public class JacksonConfig {

@Bean(name = "mapperObject")
public ObjectMapper getObjectMapper() {
ObjectMapper om = new ObjectMapper();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
om.registerModule(javaTimeModule);
return om;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
shiroFilter.setUnauthorizedUrl("/");

Map<String, String> filterMap = new LinkedHashMap<>();
filterMap.put("/sys/captcha", "anon");
filterMap.put("/swagger/**", "anon");
filterMap.put("/v2/api-docs", "anon");
filterMap.put("/swagger-ui.html", "anon");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Docket createRestApi() {

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("人人开源")
.description("renren-admin文档")
.title("北京新模式")
.description("api文档")
.termsOfServiceUrl("http://www.renren.io")
.version("3.1.0")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public int getValue() {
* 云服务商
*/
public enum CloudService {
/**
* 服务器
*/
LOCAL(0),
/**
* 七牛云
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

//七牛绑定的域名
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.renren.modules.oss.cloud;

import io.renren.common.exception.RRException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;

/**
* @author 李帅超
* @Description: TODO
* @date 2018-03-06 12:49
*/
@Component
public class LocalCloudStorageService extends CloudStorageService {
@Value("${upload.base.dir}")
private String baseDir;


public LocalCloudStorageService(){

}

@Override
public String upload(byte[] data, String path) {
return upload(new ByteArrayInputStream(data), path);
}

@Override
public String upload(InputStream inputStream, String path) {
try {
FileUtils.copyInputStreamToFile(inputStream,new File(getBaseDir() + path));
} catch (Exception e){
throw new RRException("上传文件失败,请检查配置信息", e);
}

return path;
}

@Override
public String uploadSuffix(byte[] data, String suffix) {
String path = getPathBySuffix(suffix);
return upload(data, path);
}

@Override
public String uploadSuffix(InputStream inputStream, String suffix) {
String path = getPathBySuffix(suffix);
return upload(inputStream, path);
}

/**
* 通过文件后缀生成文件本地路径
* @param suffix
* @return
*/
private String getPathBySuffix(String suffix) {
LocalDate localDate = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String str = "/";
String dir = "/resource/";
return dir + localDate.format(dateTimeFormatter) + str +System.currentTimeMillis() + new Random().nextInt(10000)+suffix;
}

private String getBaseDir(){
if(StringUtils.isBlank(baseDir)){
try {
baseDir = ResourceUtils.getURL("classpath:public").getPath();
} catch (FileNotFoundException e) {
throw new RRException("上传文件失败,请检查本地路径配置信息", e);
}
}
return baseDir;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@
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;

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;

Expand All @@ -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);
}


/**
* 退出
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 6 additions & 6 deletions renren-admin/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ spring:
driverClassName: com.mysql.jdbc.Driver
druid:
first: #数据源1
url: jdbc:mysql://localhost:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: renren
password: 123456
url: jdbc:mysql://192.168.29.212:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
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://192.168.29.212:3306/renren_security?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: ABCabc@123.
initial-size: 10
max-active: 100
min-idle: 10
Expand Down
Loading