package com.cloud.user.controller;
import com.cloud.common.utils.MD5Util;
import com.cloud.model.common.TokenInfo;
import com.cloud.model.sys.AppUser;
import com.cloud.model.sys.constants.CredentialType;
import com.cloud.user.filter.AuthNoneIgnore;
import com.cloud.user.service.AppUserService;
import com.cloud.user.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
/*import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.util.OAuth2Utils;*/
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/data/api-u")
@Api(value = "LoginController", description = "登陆、刷新token、退出")
public class LoginController {
@Autowired
private AppUserService userService;
@Autowired
private TokenService tokenService;
/**
* 系统登陆
* 根据用户名登录
* 采用oauth2密码模式获取access_token和refresh_token
*
* @param username
* @param password
* @return
*/
@PostMapping("/sys/login")
@ApiOperation(value = "系统登陆", notes = "采用oauth2密码模式获取access_token和refresh_token", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "登录名", required = true, dataType = "String", paramType = "query"),
@ApiImplicitParam(name = "password", value = "登录密码", required = true, dataType = "String", paramType = "query"),
})
@AuthNoneIgnore
public TokenInfo login(String username, String password) {
Map params = new HashMap<>();
params.put("username", username);
params.put("password", MD5Util.encode(password));
AppUser user = userService.login(params);
if(user == null){
return new TokenInfo();
}
String token = tokenService.getToken(user);
return new TokenInfo(token);
}
/**
* 系统刷新refresh_token
*
* @param refresh_token
* @return
*/
@PostMapping("/sys/refresh_token")
@ApiOperation(value = "系统刷新refresh_token", notes = "系统刷新refresh_token", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiImplicitParams({
@ApiImplicitParam(name = "refresh_token", value = "refresh_token", required = true, dataType = "String", paramType = "path"),
})
public TokenInfo refresh_token(String refresh_token, String access_token) {
Map rs = new HashMap<>();
tokenService.refreshUserToken(refresh_token);
return new TokenInfo(refresh_token);
}
/**
* 退出
*
* @param access_token
*/
@GetMapping("/sys/logout")
@ApiOperation(value = "退出", notes = "退出", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiImplicitParams({
@ApiImplicitParam(name = "access_token", value = "access_token", required = true, dataType = "String", paramType = "path"),
@ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "path"),
})
public void logout(String access_token, @RequestHeader(required = false, value = "Authorization") String token) {
if (StringUtils.isBlank(access_token)) {
if (StringUtils.isNoneBlank(token)) {
access_token = token.substring(TokenInfo.BEARER_TYPE.length() + 1);
}
}
tokenService.loginOff(access_token);
}
}