wei.zhang2 2017-10-16 09:54:45 +08:00
parent c5aaca5163
commit c42e34b507
4 changed files with 24 additions and 9 deletions

View File

@ -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.ResponseInfo;
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.utils.ResponseUtil;
@ -96,6 +97,10 @@ public class SecurityHandlerConfig {
Authentication authentication) throws IOException, ServletException {
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);
}
};

View File

@ -6,7 +6,6 @@ import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@ -29,12 +28,11 @@ public class TokenServiceImpl implements TokenService {
@Override
public Token saveToken(LoginUser loginUser) {
String oldToken = getTokenByUserId(loginUser.getId());
if (!StringUtils.isEmpty(oldToken)) {
deleteToken(oldToken);
String token = getTokenByUserId(loginUser.getId());
if (StringUtils.isEmpty(token)) {
token = UUID.randomUUID().toString();
}
String token = UUID.randomUUID().toString();
loginUser.setToken(token);
updateLoginUser(loginUser);
@ -44,7 +42,6 @@ public class TokenServiceImpl implements TokenService {
/**
*
*/
@Async
@Override
public void updateLoginUser(LoginUser loginUser) {
redisTemplate.boundValueOps(getTokenKey(loginUser.getToken())).set(loginUser, expireSeconds, TimeUnit.SECONDS);
@ -59,8 +56,11 @@ public class TokenServiceImpl implements TokenService {
@Override
public boolean deleteToken(String token) {
if (redisTemplate.hasKey(getTokenKey(token))) {
redisTemplate.delete(getTokenKey(token));
String key = getTokenKey(token);
LoginUser loginUser = redisTemplate.opsForValue().get(key);
if (loginUser != null) {
redisTemplate.delete(key);
redisTemplate.delete(getUserIdKey(loginUser.getId()));
return true;
}

View File

@ -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>
</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>
</dl>
</li>

View File

@ -107,6 +107,16 @@ function showUnreadNotice(){
});
}
function logout(){
$.ajax({
type : 'get',
url : '/logout',
success : function(data) {
location.href='/login.html';
}
});
}
var active;
layui.use(['layer', 'element'], function() {