zwzw1219 2017-10-15 21:32:02 +08:00
parent 46da4c00a3
commit 29e015aa8b
2 changed files with 7 additions and 9 deletions

View File

@ -15,7 +15,6 @@ import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.util.StringUtils;
import com.boot.security.server.dto.LoginUser;
import com.boot.security.server.dto.ResponseInfo;
@ -42,15 +41,8 @@ public class SecurityHandlerConfig {
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
Token token = null;
String _token = tokenService.getTokenByUserId(loginUser.getId());
if (!StringUtils.isEmpty(_token)) {
token = Token.builder().token(_token).build();
tokenService.addExpireTime(loginUser);
} else {
token = tokenService.saveToken(loginUser);
}
Token token = tokenService.saveToken(loginUser);
ResponseUtil.responseJson(response, HttpStatus.OK.value(), token);
}
};

View File

@ -8,6 +8,7 @@ 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;
import com.boot.security.server.dto.LoginUser;
import com.boot.security.server.dto.Token;
@ -28,6 +29,11 @@ public class TokenServiceImpl implements TokenService {
@Override
public Token saveToken(LoginUser loginUser) {
String oldToken = idTokenRedisTemplate.opsForValue().get(loginUser.getId());
if (!StringUtils.isEmpty(oldToken)) {
deleteToken(oldToken);
}
String token = UUID.randomUUID().toString();
loginUser.setToken(token);
updateLoginUser(loginUser);