This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-nz-talon/src/main/java/net/geedge/confagent/controller/AuthController.java
2021-07-09 17:01:27 +08:00

102 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}