master
parent
c5aaca5163
commit
c42e34b507
|
|
@ -20,6 +20,7 @@ import org.springframework.security.web.authentication.logout.LogoutSuccessHandl
|
||||||
import com.boot.security.server.dto.LoginUser;
|
import com.boot.security.server.dto.LoginUser;
|
||||||
import com.boot.security.server.dto.ResponseInfo;
|
import com.boot.security.server.dto.ResponseInfo;
|
||||||
import com.boot.security.server.dto.Token;
|
import com.boot.security.server.dto.Token;
|
||||||
|
import com.boot.security.server.filter.TokenFilter;
|
||||||
import com.boot.security.server.service.TokenService;
|
import com.boot.security.server.service.TokenService;
|
||||||
import com.boot.security.server.utils.ResponseUtil;
|
import com.boot.security.server.utils.ResponseUtil;
|
||||||
|
|
||||||
|
|
@ -96,6 +97,10 @@ public class SecurityHandlerConfig {
|
||||||
Authentication authentication) throws IOException, ServletException {
|
Authentication authentication) throws IOException, ServletException {
|
||||||
ResponseInfo info = ResponseInfo.builder().code(HttpStatus.OK.value() + "").message("退出成功").build();
|
ResponseInfo info = ResponseInfo.builder().code(HttpStatus.OK.value() + "").message("退出成功").build();
|
||||||
|
|
||||||
|
String token = TokenFilter.getToken(request);
|
||||||
|
boolean flag = tokenService.deleteToken(token);
|
||||||
|
System.out.println(flag);
|
||||||
|
|
||||||
ResponseUtil.responseJson(response, HttpStatus.OK.value(), info);
|
ResponseUtil.responseJson(response, HttpStatus.OK.value(), info);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import java.util.concurrent.TimeUnit;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
|
@ -29,12 +28,11 @@ public class TokenServiceImpl implements TokenService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token saveToken(LoginUser loginUser) {
|
public Token saveToken(LoginUser loginUser) {
|
||||||
String oldToken = getTokenByUserId(loginUser.getId());
|
String token = getTokenByUserId(loginUser.getId());
|
||||||
if (!StringUtils.isEmpty(oldToken)) {
|
if (StringUtils.isEmpty(token)) {
|
||||||
deleteToken(oldToken);
|
token = UUID.randomUUID().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
String token = UUID.randomUUID().toString();
|
|
||||||
loginUser.setToken(token);
|
loginUser.setToken(token);
|
||||||
updateLoginUser(loginUser);
|
updateLoginUser(loginUser);
|
||||||
|
|
||||||
|
|
@ -44,7 +42,6 @@ public class TokenServiceImpl implements TokenService {
|
||||||
/**
|
/**
|
||||||
* 更新缓存的用户信息
|
* 更新缓存的用户信息
|
||||||
*/
|
*/
|
||||||
@Async
|
|
||||||
@Override
|
@Override
|
||||||
public void updateLoginUser(LoginUser loginUser) {
|
public void updateLoginUser(LoginUser loginUser) {
|
||||||
redisTemplate.boundValueOps(getTokenKey(loginUser.getToken())).set(loginUser, expireSeconds, TimeUnit.SECONDS);
|
redisTemplate.boundValueOps(getTokenKey(loginUser.getToken())).set(loginUser, expireSeconds, TimeUnit.SECONDS);
|
||||||
|
|
@ -59,8 +56,11 @@ public class TokenServiceImpl implements TokenService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean deleteToken(String token) {
|
public boolean deleteToken(String token) {
|
||||||
if (redisTemplate.hasKey(getTokenKey(token))) {
|
String key = getTokenKey(token);
|
||||||
redisTemplate.delete(getTokenKey(token));
|
LoginUser loginUser = redisTemplate.opsForValue().get(key);
|
||||||
|
if (loginUser != null) {
|
||||||
|
redisTemplate.delete(key);
|
||||||
|
redisTemplate.delete(getUserIdKey(loginUser.getId()));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@
|
||||||
<a href='javascript:;' lay-id="-998" data-url="pages/user/updateHeadImg.html"><i class="fa fa-drupal" aria-hidden="true"></i> 头像</a>
|
<a href='javascript:;' lay-id="-998" data-url="pages/user/updateHeadImg.html"><i class="fa fa-drupal" aria-hidden="true"></i> 头像</a>
|
||||||
</dd>
|
</dd>
|
||||||
<dd>
|
<dd>
|
||||||
<a href="/logout"><i class="fa fa-sign-out" aria-hidden="true"></i> 退出</a>
|
<a href="javascript:;" onclick="logout()"><i class="fa fa-sign-out" aria-hidden="true"></i> 退出</a>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
||||||
|
|
@ -107,6 +107,16 @@ function showUnreadNotice(){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function logout(){
|
||||||
|
$.ajax({
|
||||||
|
type : 'get',
|
||||||
|
url : '/logout',
|
||||||
|
success : function(data) {
|
||||||
|
location.href='/login.html';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
var active;
|
var active;
|
||||||
|
|
||||||
layui.use(['layer', 'element'], function() {
|
layui.use(['layer', 'element'], function() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue