添加管理员模块注释

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

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

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

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

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

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

@ -1,9 +1,11 @@
package com.community.pocket.repository;
import com.community.pocket.domain.Manager;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
//管理员Restful接口
@RepositoryRestResource
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:
data:
mongodb:

Loading…
Cancel
Save