Merge remote-tracking branch 'origin/master'

master
jiangjiaqi 5 years ago
commit f6e52cd30d
  1. 5
      src/main/java/com/community/pocket/Run.java
  2. 9
      src/main/java/com/community/pocket/api/ManagerController.java
  3. 3
      src/main/java/com/community/pocket/domain/Manager.java
  4. 5
      src/main/java/com/community/pocket/domain/Res.java
  5. 4
      src/main/java/com/community/pocket/domain/Token.java
  6. 1
      src/main/java/com/community/pocket/repository/ForumRepo.java
  7. 10
      src/main/java/com/community/pocket/repository/ManagerDao.java
  8. 2
      src/main/java/com/community/pocket/repository/ManagerRepo.java
  9. 3
      src/main/resources/application.properties
  10. 1
      src/main/resources/application.yaml

@ -4,10 +4,11 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class DemoApplication { //应用入口
public class Run {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args); SpringApplication.run(Run.class, args);
} }
} }

@ -17,7 +17,9 @@ import java.util.Date;
@RestController @RestController
@RequestMapping("/api/manager") @RequestMapping("/api/manager")
//客户端跨域测试
@CrossOrigin("http://localhost:4200") @CrossOrigin("http://localhost:4200")
//管理员接口
public class ManagerController { public class ManagerController {
@Autowired @Autowired
@ -25,22 +27,25 @@ public class ManagerController {
// 登陆 // 登陆
@PostMapping("login") @PostMapping("login")
@ResponseBody
public Res<Token> login(@RequestBody ManagerLogin managerLogin){ public Res<Token> login(@RequestBody ManagerLogin managerLogin){
if(StringUtils.isEmpty(managerLogin.getManagerName())){ if(StringUtils.isEmpty(managerLogin.getManagerName())){
return new Res<>(Result.FAIL,"管理员不能为空!",null); return new Res<>(Result.FAIL,"管理员不能为空!",null);
}else if(StringUtils.isEmpty(managerLogin.getPassword())){ }else if(StringUtils.isEmpty(managerLogin.getPassword())){
return new Res<>(Result.FAIL,"密码不能为空!",null); return new Res<>(Result.FAIL,"密码不能为空!",null);
//校验账号密码
}else if(!managerDao.login(managerLogin)){ }else if(!managerDao.login(managerLogin)){
return new Res<>(Result.FAIL,"账号或密码错误!",null); return new Res<>(Result.FAIL,"账号或密码错误!",null);
} }
else{ else{
// 验证通过生成令牌响应给服务端
Token token=new Token(); Token token=new Token();
Calendar c=Calendar.getInstance(); Calendar c=Calendar.getInstance();
c.setTime(new Date()); c.setTime(new Date());
token.setCreateTime(c.getTimeInMillis()); token.setCreateTime(c.getTimeInMillis());
// 令牌有效时间1小时
c.add(Calendar.HOUR,1); c.add(Calendar.HOUR,1);
token.setUseTime(c.getTimeInMillis()); token.setUseTime(c.getTimeInMillis());
// 生成token
token.setToken(DigestUtils.md5DigestAsHex((token.getCreateTime()+""+token.getUseTime()+"").getBytes())); token.setToken(DigestUtils.md5DigestAsHex((token.getCreateTime()+""+token.getUseTime()+"").getBytes()));
return new Res<>(Result.OK,"登陆成功",token); return new Res<>(Result.OK,"登陆成功",token);
} }
@ -48,7 +53,6 @@ public class ManagerController {
// 注册 // 注册
@PostMapping("register") @PostMapping("register")
@ResponseBody
public Res<String> register(@RequestBody ManagerRegister manager){ public Res<String> register(@RequestBody ManagerRegister manager){
if(StringUtils.isEmpty(manager.getManagerName())){ if(StringUtils.isEmpty(manager.getManagerName())){
return new Res<>(Result.FAIL,"管理员不能为空",null); return new Res<>(Result.FAIL,"管理员不能为空",null);
@ -65,6 +69,7 @@ public class ManagerController {
}else if(managerDao.hasUser(manager.getManagerName())){ }else if(managerDao.hasUser(manager.getManagerName())){
return new Res<>(Result.FAIL,"用户已存在",null); return new Res<>(Result.FAIL,"用户已存在",null);
}else{ }else{
// 表单数据转换到管理员实体,调用dao层持久化到数据库
Manager m=new Manager(); Manager m=new Manager();
m.setManagerName(manager.getManagerName()); m.setManagerName(manager.getManagerName());
m.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes())); m.setPassword(DigestUtils.md5DigestAsHex(manager.getPassword().getBytes()));

@ -2,6 +2,7 @@ package com.community.pocket.domain;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
//管理员
public class Manager { public class Manager {
@Id @Id
private String id; private String id;
@ -14,6 +15,8 @@ public class Manager {
// 邮箱 // 邮箱
private String email; private String email;
private String a;
public String getId() { public String getId() {
return id; return id;
} }

@ -2,10 +2,13 @@ package com.community.pocket.domain;
import com.community.pocket.Result; import com.community.pocket.Result;
//接口统一返回json响应体
public class Res<T> { public class Res<T> {
// 响应 // 响应结果
private Result result; private Result result;
// 响应详细结果
private String message; private String message;
// 自定义其他响应信息
private T body; private T body;
public Res() { public Res() {

@ -2,11 +2,15 @@ package com.community.pocket.domain;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
//登陆成功记录的令牌信息
public class Token { public class Token {
@Id @Id
private String id; private String id;
//令牌
private String token; private String token;
//创建时间
private Long createTime; private Long createTime;
// 有效时间
private Long useTime; private Long useTime;
public String getId() { public String getId() {

@ -4,6 +4,7 @@ import com.community.pocket.domain.Forum;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RepositoryRestResource;
//论坛Restful接口
@RepositoryRestResource @RepositoryRestResource
public interface ForumRepo extends MongoRepository<Forum,String> { public interface ForumRepo extends MongoRepository<Forum,String> {
} }

@ -1,5 +1,6 @@
package com.community.pocket.repository; package com.community.pocket.repository;
import com.community.pocket.domain.Manager; import com.community.pocket.domain.Manager;
import com.community.pocket.domain.form.ManagerLogin; import com.community.pocket.domain.form.ManagerLogin;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -15,10 +16,15 @@ public class ManagerDao {
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
// 登陆
public boolean login(ManagerLogin managerLogin){ public boolean login(ManagerLogin managerLogin){
// 构造查询管理员条件
Criteria criteria=Criteria.where("managerName").is(managerLogin.getManagerName()); Criteria criteria=Criteria.where("managerName").is(managerLogin.getManagerName());
try { try {
// 查询管理员
Manager manager= mongoTemplate.findOne(new Query(criteria), Manager.class); Manager manager= mongoTemplate.findOne(new Query(criteria), Manager.class);
//查询出来的管理员非空,并且管理员名和MD5(密码)匹配返回true
return manager!=null&&manager.getManagerName().equals(managerLogin.getManagerName())&&manager.getPassword().equals(DigestUtils.md5DigestAsHex(managerLogin.getPassword().getBytes())); return manager!=null&&manager.getManagerName().equals(managerLogin.getManagerName())&&manager.getPassword().equals(DigestUtils.md5DigestAsHex(managerLogin.getPassword().getBytes()));
} catch (Exception e) { } catch (Exception e) {
return false; return false;
@ -26,16 +32,18 @@ public class ManagerDao {
} }
// 查用户 //查询管理员名字是否已经注册
public boolean hasUser(String name){ public boolean hasUser(String name){
try { try {
Manager manager=mongoTemplate.findOne(new Query(Criteria.where("managerName").is(name)),Manager.class); Manager manager=mongoTemplate.findOne(new Query(Criteria.where("managerName").is(name)),Manager.class);
//非空返回true
return manager!=null; return manager!=null;
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }
} }
// 保存管理员注册信息
public boolean save(Manager manager){ public boolean save(Manager manager){
try { try {
mongoTemplate.save(manager); mongoTemplate.save(manager);

@ -1,9 +1,11 @@
package com.community.pocket.repository; package com.community.pocket.repository;
import com.community.pocket.domain.Manager; import com.community.pocket.domain.Manager;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RepositoryRestResource;
//管理员Restful接口
@RepositoryRestResource @RepositoryRestResource
public interface ManagerRepo extends MongoRepository<Manager,String> { public interface ManagerRepo extends MongoRepository<Manager,String> {
} }

@ -1,3 +0,0 @@
#spring.data.mongodb.uri=mongodb://localhost:27017/demo
#spring.datasource
#spring.datasource.url=mongodb://localhost:27017/demo

@ -1,3 +1,4 @@
#数据库配置
spring: spring:
data: data:
mongodb: mongodb:

Loading…
Cancel
Save