对象池完善,添加用户登入接口但未实现
This commit is contained in:
parent
769b4db1a1
commit
519c6e1fb4
|
@ -0,0 +1,46 @@
|
|||
package com.example.catchTheLetters.controller;
|
||||
|
||||
import com.example.catchTheLetters.entity.LoginDto;
|
||||
import com.example.catchTheLetters.entity.User;
|
||||
import com.example.catchTheLetters.service.AuthService;
|
||||
import com.example.catchTheLetters.utils.R;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 认证控制器
|
||||
* @author spyn
|
||||
*/
|
||||
@RestController
|
||||
@Slf4j
|
||||
@Api(tags = "认证API")
|
||||
@RequestMapping("/auth")
|
||||
public class AuthController {
|
||||
|
||||
@Resource
|
||||
private AuthService authService;
|
||||
|
||||
@ApiOperation(value = "用户登录")
|
||||
@PostMapping("/login")
|
||||
public R<String> login(@RequestBody LoginDto loginDto) {
|
||||
return R.ok(authService.login(loginDto));
|
||||
}
|
||||
|
||||
@ApiOperation(value = "用户注销")
|
||||
@PostMapping("/logout")
|
||||
public R logout(String token) {
|
||||
return authService.logout(token) ? R.ok() : R.fail();
|
||||
}
|
||||
|
||||
@ApiOperation(value = "验证token")
|
||||
@PostMapping("/verify")
|
||||
public R<User> verify(String token) {
|
||||
return R.ok(authService.verify(token));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.example.catchTheLetters.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 登录信息类
|
||||
* @author spyn
|
||||
*/
|
||||
@Data
|
||||
public class LoginDto implements Serializable {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public LoginDto() {
|
||||
}
|
||||
|
||||
public LoginDto(String username, String password) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.example.catchTheLetters.service;
|
||||
|
||||
import com.example.catchTheLetters.entity.LoginDto;
|
||||
import com.example.catchTheLetters.entity.User;
|
||||
|
||||
/**
|
||||
* 认证服务接口
|
||||
* @author spyn
|
||||
*/
|
||||
public interface AuthService {
|
||||
/**
|
||||
* 登录
|
||||
* @param loginDto 登录信息
|
||||
* @return token
|
||||
*/
|
||||
String login(LoginDto loginDto);
|
||||
|
||||
/**
|
||||
* 注销
|
||||
* @param token token
|
||||
* @return 是否成功
|
||||
*/
|
||||
boolean logout(String token);
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
* @param token token
|
||||
* @return 用户信息(不可以包含密码)
|
||||
*/
|
||||
User verify(String token);
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.example.catchTheLetters.service.impl;
|
||||
|
||||
import com.example.catchTheLetters.entity.LoginDto;
|
||||
import com.example.catchTheLetters.entity.User;
|
||||
import com.example.catchTheLetters.service.AuthService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AuthServiceImpl implements AuthService {
|
||||
|
||||
@Resource
|
||||
private MongoTemplate mongoTemplate;
|
||||
|
||||
@Override
|
||||
public String login(LoginDto loginDto) {
|
||||
// TODO 从MongoDB中查询用户信息,返回TOKEN
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean logout(String token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User verify(String token) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package com.example.catchTheLetters.utils;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||
|
@ -17,15 +19,54 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
*/
|
||||
private final ConcurrentLinkedDeque<T> pool;
|
||||
|
||||
/**
|
||||
* 重置对象委托
|
||||
*/
|
||||
private final Consumer<T> reset;
|
||||
|
||||
/**
|
||||
* 创建对象委托
|
||||
*/
|
||||
private final Supplier<T> factory;
|
||||
|
||||
/**
|
||||
* 重置对象委托
|
||||
* 对象池上限
|
||||
*/
|
||||
private final Consumer<T> reset;
|
||||
@Setter
|
||||
@Getter
|
||||
private Integer limit;
|
||||
|
||||
/**
|
||||
* 是否阻塞
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
private Boolean block;
|
||||
|
||||
/**
|
||||
* 当前对象数
|
||||
*/
|
||||
private Integer count;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
* @param factory 创建对象委托
|
||||
* @param reset 重置对象委托
|
||||
* @param block 是否阻塞
|
||||
* @param limit 对象池上限
|
||||
*/
|
||||
public ObjectPool(Supplier<T> factory, Consumer<T> reset, Boolean block, Integer limit) {
|
||||
this.pool = new ConcurrentLinkedDeque<>();
|
||||
this.factory = factory;
|
||||
this.reset = reset;
|
||||
this.block = block;
|
||||
this.limit = limit;
|
||||
// 预先创建对象
|
||||
for (int i = 0; i < limit; i++) {
|
||||
this.pool.push(factory.get());
|
||||
}
|
||||
count = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
|
@ -33,21 +74,29 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
* @param reset 重置对象委托
|
||||
*/
|
||||
public ObjectPool(Supplier<T> factory, Consumer<T> reset) {
|
||||
this.pool = new ConcurrentLinkedDeque<>();
|
||||
this.factory = factory;
|
||||
this.reset = reset;
|
||||
this(factory, reset, false, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出一个对象
|
||||
* @return 对象
|
||||
*/
|
||||
public T borrowObject() {
|
||||
T object;
|
||||
if ((object = pool.pollFirst()) == null) {
|
||||
object = (T) factory.get();
|
||||
public T borrowObject() throws InterruptedException {
|
||||
if (!pool.isEmpty()) return pool.pop();
|
||||
// 如果对象池为空,且此时没到上限,创建新对象,否则查看是否阻塞,若不阻塞,则返回null,否则等待
|
||||
if (count < limit) {
|
||||
count++;
|
||||
return factory.get();
|
||||
}
|
||||
return object;
|
||||
if (!block) {
|
||||
return null;
|
||||
}
|
||||
synchronized (this) {
|
||||
while (pool.isEmpty()) {
|
||||
this.wait();
|
||||
}
|
||||
}
|
||||
return pool.pop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -60,7 +109,10 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
}
|
||||
// 重置对象
|
||||
reset.accept(object);
|
||||
this.pool.push(object);
|
||||
synchronized (this) {
|
||||
this.pool.push(object);
|
||||
this.notify();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -79,15 +131,4 @@ public class ObjectPool<T> implements Iterable<T> {
|
|||
public java.util.Iterator<T> iterator() {
|
||||
return this.pool.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 覆写toString方法,便于输出对象池信息
|
||||
* @return 对象池信息
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ObjectPool{" +
|
||||
"pool=" + pool +
|
||||
"} Count: " + pool.size();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,3 +8,5 @@ spring:
|
|||
database: catch-the-letters
|
||||
username: main
|
||||
password: spynsql
|
||||
server:
|
||||
port: 3536
|
||||
|
|
Loading…
Reference in New Issue