forked from ITDragonBlog/daydayup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
627f4d2
commit c5facba
Showing
7 changed files
with
265 additions
and
27 deletions.
There are no files selected for viewing
34 changes: 22 additions & 12 deletions
34
SpringBoot/SpringData/springbootStudy/src/main/java/com/itdragon/common/ITDragonUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,37 @@ | ||
package com.itdragon.common; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.TimeZone; | ||
import java.util.UUID; | ||
|
||
import org.springframework.util.DigestUtils; | ||
|
||
import com.itdragon.pojo.User; | ||
|
||
/** | ||
* 工具类 | ||
* @author itdragon | ||
* | ||
*/ | ||
public class ITDragonUtils { | ||
|
||
private static final String HASH_ALGORITHM = "SHA-1"; | ||
private static final int HASH_INTERATIONS = 1024; | ||
private static final int SALT_SIZE = 8; | ||
public class ItdragonUtils { | ||
|
||
/** | ||
* 设定安全的密码,生成随机的salt并经过1024次 sha-1 hash | ||
* 加盐加密的策略非常多,根据实际业务来 | ||
*/ | ||
private void entryptPassword(User user) { | ||
// byte[] salt = Digests.generateSalt(SALT_SIZE); | ||
// user.setSalt(Encodes.encodeHex(salt)); | ||
// | ||
// byte[] hashPassword = Digests.sha1(user.getPlainPassword().getBytes(), salt, HASH_INTERATIONS); | ||
// user.setPassword(Encodes.encodeHex(hashPassword)); | ||
public static void entryptPassword(User user) { | ||
String salt = UUID.randomUUID().toString(); | ||
String temPassword = salt + user.getPlainPassword(); | ||
String md5Password = DigestUtils.md5DigestAsHex(temPassword.getBytes()); | ||
user.setSalt(salt); | ||
user.setPassword(md5Password); | ||
} | ||
|
||
public static String getCurrentDateTime() { | ||
TimeZone zone = TimeZone.getTimeZone("Asia/Shanghai"); | ||
TimeZone.setDefault(zone); | ||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
return df.format(new Date()); | ||
} | ||
|
||
} |
138 changes: 138 additions & 0 deletions
138
SpringBoot/SpringData/springbootStudy/src/main/java/com/itdragon/common/ItdragonResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.itdragon.common; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class ItdragonResult { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); // 定义jackson对象 | ||
|
||
private Integer status; // 响应业务状态 | ||
|
||
private String msg; // 响应消息 | ||
|
||
private Object data; // 响应中的数据 | ||
|
||
public ItdragonResult() { | ||
} | ||
|
||
public ItdragonResult(Integer status, String msg, Object data) { | ||
this.status = status; | ||
this.msg = msg; | ||
this.data = data; | ||
} | ||
|
||
public ItdragonResult(Object data) { | ||
this.status = 200; | ||
this.msg = "OK"; | ||
this.data = data; | ||
} | ||
|
||
public static ItdragonResult ok(Object data) { | ||
return new ItdragonResult(data); | ||
} | ||
|
||
public static ItdragonResult ok() { | ||
return new ItdragonResult(null); | ||
} | ||
|
||
public static ItdragonResult build(Integer status, String msg, Object data) { | ||
return new ItdragonResult(status, msg, data); | ||
} | ||
|
||
public static ItdragonResult build(Integer status, String msg) { | ||
return new ItdragonResult(status, msg, null); | ||
} | ||
|
||
public Integer getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(Integer status) { | ||
this.status = status; | ||
} | ||
|
||
public String getMsg() { | ||
return msg; | ||
} | ||
|
||
public void setMsg(String msg) { | ||
this.msg = msg; | ||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
/** | ||
* 将json结果集转化为ITDragonResult对象 | ||
* | ||
* @param jsonData json数据 | ||
* @param clazz ITDragonResult中的object类型 | ||
* @return | ||
*/ | ||
public static ItdragonResult formatToPojo(String jsonData, Class<?> clazz) { | ||
try { | ||
if (clazz == null) { | ||
return MAPPER.readValue(jsonData, ItdragonResult.class); | ||
} | ||
JsonNode jsonNode = MAPPER.readTree(jsonData); | ||
JsonNode data = jsonNode.get("data"); | ||
Object obj = null; | ||
if (clazz != null) { | ||
if (data.isObject()) { | ||
obj = MAPPER.readValue(data.traverse(), clazz); | ||
} else if (data.isTextual()) { | ||
obj = MAPPER.readValue(data.asText(), clazz); | ||
} | ||
} | ||
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* 没有object对象的转化 | ||
* | ||
* @param json | ||
* @return | ||
*/ | ||
public static ItdragonResult format(String json) { | ||
try { | ||
return MAPPER.readValue(json, ItdragonResult.class); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Object是集合转化 | ||
* | ||
* @param jsonData json数据 | ||
* @param clazz 集合中的类型 | ||
* @return | ||
*/ | ||
public static ItdragonResult formatToList(String jsonData, Class<?> clazz) { | ||
try { | ||
JsonNode jsonNode = MAPPER.readTree(jsonData); | ||
JsonNode data = jsonNode.get("data"); | ||
Object obj = null; | ||
if (data.isArray() && data.size() > 0) { | ||
obj = MAPPER.readValue(data.traverse(), | ||
MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); | ||
} | ||
return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package com.itdragon.controller; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.validation.Valid; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.itdragon.repository.UserRepository; | ||
import com.itdragon.common.ItdragonUtils; | ||
import com.itdragon.pojo.User; | ||
import com.itdragon.service.UserService; | ||
|
||
@Controller | ||
|
@@ -15,9 +20,32 @@ public class UserController { | |
@Autowired | ||
private UserService userService; | ||
|
||
@RequestMapping(method = RequestMethod.POST) | ||
public String register(@Valid User user, HttpServletRequest request) { | ||
// userService.registerUser(user); | ||
try { | ||
// 注册成功直接将当前用户保存到session中 | ||
request.getSession().setAttribute("user", user); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return "redirect:/login"; | ||
} | ||
|
||
@RequestMapping("findAll") | ||
@ResponseBody | ||
public String findAll() { | ||
User user = new User(); | ||
user.setAccount("itdragon"); | ||
user.setUserName("ITDragonBlog"); | ||
user.setEmail("[email protected]"); | ||
user.setIphone("12345677890"); | ||
user.setPlainPassword("987654321"); | ||
user.setPlatform("weixin"); | ||
user.setCreatedDate(ItdragonUtils.getCurrentDateTime()); | ||
user.setUpdatedDate(ItdragonUtils.getCurrentDateTime()); | ||
ItdragonUtils.entryptPassword(user); | ||
userService.registerUser(user); | ||
return userService.findAll().toString(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.