nz-talon首次提交
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package net.geedge.confagent.controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import net.geedge.confagent.annotation.UnCheckToken;
|
||||
import net.geedge.confagent.entity.AuthEntity;
|
||||
import net.geedge.confagent.util.R;
|
||||
import net.geedge.confagent.util.RCode;
|
||||
import net.geedge.confagent.util.Tool;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value={"/auth"})
|
||||
public class AuthController extends BaseController{
|
||||
private final static Log log = Log.get();
|
||||
|
||||
@Value("${confagent.authFile:auth.yml}")
|
||||
private String authFile;
|
||||
|
||||
private static String rootPath = Tool.WebPathUtil.getRootPath();
|
||||
/**
|
||||
* @Description 每次请求auth接口重新生成 token并记录到文件
|
||||
* @Author rui
|
||||
* @Date 2021/3/23
|
||||
*/
|
||||
@RequestMapping
|
||||
@UnCheckToken
|
||||
public R auth(AuthEntity auth){
|
||||
|
||||
if(Tool.StrUtil.isBlank(auth.getName())||Tool.StrUtil.isBlank(auth.getPin())){
|
||||
return R.error(RCode.AUTH_NAME_PIN_ISNULL);
|
||||
}
|
||||
|
||||
File af = Tool.FileUtil.file(rootPath, authFile);
|
||||
log.debug("auth file path : {}" ,af.getAbsolutePath());
|
||||
Properties properties = Tool.YamlUtil.yamlToProperties(af.getAbsolutePath());
|
||||
|
||||
String name = properties.getProperty("auth.name","nezha");
|
||||
String pin = properties.getProperty("auth.pin","nezha");
|
||||
|
||||
if(Tool.StrUtil.equals(name,auth.getName())&& Tool.StrUtil.equals(pin,auth.getPin())){
|
||||
String token = Tool.IdUtil.fastShortUUID();
|
||||
log.debug("write token {} to {}",token,af.getAbsolutePath());
|
||||
writeToken(token);
|
||||
Map<String,String> map = new HashMap<>();
|
||||
map.put("token", token);
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
return R.error(RCode.AUTH_NAME_PIN_INVALID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 验证当前token是否和文件记录一致,一致后重新生成则更新文件并返回最新token
|
||||
* @Author rui
|
||||
* @Date 2021/3/24
|
||||
*/
|
||||
@GetMapping("/refresh")
|
||||
public R refresh(){
|
||||
String t = Tool.IdUtil.fastShortUUID();
|
||||
writeToken(t);
|
||||
Map<String,String> map = new HashMap<>();
|
||||
map.put("token", t);
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 注销当前 token
|
||||
* @Author rui
|
||||
* @Date 2021/3/24
|
||||
*/
|
||||
@GetMapping("logout")
|
||||
public R logout(){
|
||||
File tf = Tool.FileUtil.file(rootPath, tokenFile);
|
||||
log.debug("token file path : {}" ,tf.getAbsolutePath());
|
||||
|
||||
if(Tool.FileUtil.exist(tf)){
|
||||
Tool.FileUtil.del(tf);
|
||||
}
|
||||
if(Tool.FileUtil.exist(promtailConfPath)){
|
||||
Tool.YamlUtil.writeAsMap(null,promtailConfPath);
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
private void writeToken(String token){
|
||||
File tf = Tool.FileUtil.file(rootPath, tokenFile);
|
||||
log.debug("token file path : {}" ,tf.getAbsolutePath());
|
||||
Tool.FileUtil.writeUtf8String(token, tf);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user