2020-05-24 20:40:55 +08:00
|
|
|
package com.ruoyi.auth.controller;
|
|
|
|
|
|
2020-06-10 11:31:13 +08:00
|
|
|
import java.util.Map;
|
2020-05-24 20:40:55 +08:00
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
|
|
|
|
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
|
|
|
|
|
import org.springframework.security.oauth2.provider.token.TokenStore;
|
|
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestHeader;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2020-06-10 11:31:13 +08:00
|
|
|
import com.ruoyi.common.core.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.core.constant.SecurityConstants;
|
2020-05-24 20:40:55 +08:00
|
|
|
import com.ruoyi.common.core.domain.R;
|
|
|
|
|
import com.ruoyi.common.core.utils.StringUtils;
|
2020-06-10 11:31:13 +08:00
|
|
|
import com.ruoyi.system.api.RemoteLogService;
|
2020-05-24 20:40:55 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* token 控制
|
|
|
|
|
*
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/token")
|
|
|
|
|
public class TokenController
|
|
|
|
|
{
|
|
|
|
|
@Autowired
|
|
|
|
|
private TokenStore tokenStore;
|
|
|
|
|
|
2020-06-10 11:31:13 +08:00
|
|
|
@Autowired
|
|
|
|
|
private RemoteLogService remoteLogService;
|
|
|
|
|
|
2020-05-24 20:40:55 +08:00
|
|
|
@DeleteMapping("/logout")
|
|
|
|
|
public R<?> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader)
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isEmpty(authHeader))
|
|
|
|
|
{
|
|
|
|
|
return R.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String tokenValue = authHeader.replace(OAuth2AccessToken.BEARER_TYPE, StringUtils.EMPTY).trim();
|
|
|
|
|
OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
|
|
|
|
|
if (accessToken == null || StringUtils.isEmpty(accessToken.getValue()))
|
|
|
|
|
{
|
|
|
|
|
return R.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 清空 access token
|
|
|
|
|
tokenStore.removeAccessToken(accessToken);
|
|
|
|
|
|
|
|
|
|
// 清空 refresh token
|
|
|
|
|
OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
|
|
|
|
|
tokenStore.removeRefreshToken(refreshToken);
|
2020-06-10 11:31:13 +08:00
|
|
|
Map<String, ?> map = accessToken.getAdditionalInformation();
|
|
|
|
|
if (map.containsKey(SecurityConstants.DETAILS_USERNAME))
|
|
|
|
|
{
|
|
|
|
|
String username = (String) map.get(SecurityConstants.DETAILS_USERNAME);
|
|
|
|
|
// 记录用户退出日志
|
|
|
|
|
remoteLogService.saveLogininfor(username, Constants.LOGOUT, "退出成功");
|
|
|
|
|
}
|
2020-05-24 20:40:55 +08:00
|
|
|
return R.ok();
|
|
|
|
|
}
|
|
|
|
|
}
|