增加sonar测试并修复sonar测试的问题

This commit is contained in:
zlt
2019-02-04 14:18:47 +08:00
parent e16d8f5d02
commit 110bdc5a84
58 changed files with 181 additions and 233 deletions
@@ -30,9 +30,7 @@ public class DefaultSecurityHandlerConfig {
*/
@Bean
public AuthenticationEntryPoint authenticationEntryPoint() {
return (request, response, authException) -> {
ResponseUtil.responseWriter(objectMapper, response, authException.getMessage(), HttpStatus.UNAUTHORIZED.value());
};
return (request, response, authException) -> ResponseUtil.responseWriter(objectMapper, response, authException.getMessage(), HttpStatus.UNAUTHORIZED.value());
}
@Bean
@@ -140,7 +140,7 @@ public class RedisTemplateTokenStore implements TokenStore {
ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken) refreshToken;
Date expiration = expiringRefreshToken.getExpiration();
if (expiration != null) {
int seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L).intValue();
int seconds = (int)((expiration.getTime() - System.currentTimeMillis()) / 1000L);
redisTemplate.expire(REFRESH_TO_ACCESS + token.getRefreshToken().getValue(), seconds, TimeUnit.SECONDS);
redisTemplate.expire(ACCESS_TO_REFRESH + token.getValue(), seconds, TimeUnit.SECONDS);
@@ -221,24 +221,20 @@ public class RedisTemplateTokenStore implements TokenStore {
@Override
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
List<Object> result = redisTemplate.opsForList().range(UNAME_TO_ACCESS + getApprovalKey(clientId, userName), 0, -1);
if (result == null || result.size() == 0) {
return Collections.emptySet();
}
List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(result.size());
for (Iterator<Object> it = result.iterator(); it.hasNext(); ) {
OAuth2AccessToken accessToken = (OAuth2AccessToken) it.next();
accessTokens.add(accessToken);
}
return Collections.unmodifiableCollection(accessTokens);
return getTokenColl(result);
}
@Override
public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {
List<Object> result = redisTemplate.opsForList().range((CLIENT_ID_TO_ACCESS + clientId), 0, -1);
if (result == null || result.size() == 0) {
return getTokenColl(result);
}
private Collection<OAuth2AccessToken> getTokenColl(List<Object> result) {
if (result == null || result.isEmpty()) {
return Collections.emptySet();
}
List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(result.size());
List<OAuth2AccessToken> accessTokens = new ArrayList<>(result.size());
for (Iterator<Object> it = result.iterator(); it.hasNext(); ) {
OAuth2AccessToken accessToken = (OAuth2AccessToken) it.next();
accessTokens.add(accessToken);
@@ -50,8 +50,8 @@ public class ResJwtTokenStore {
* @return 公钥 Key
*/
private String getPubKey() {
Resource resource = new ClassPathResource(ResJwtTokenStore.PUBLIC_KEY);
try (BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream()))) {
Resource res = new ClassPathResource(ResJwtTokenStore.PUBLIC_KEY);
try (BufferedReader br = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
return br.lines().collect(Collectors.joining("\n"));
} catch (IOException ioe) {
return getKeyFromAuthorizationServer();
@@ -7,6 +7,7 @@ import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAut
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Enumeration;
@@ -18,6 +19,10 @@ import java.util.Enumeration;
*/
@Slf4j
public class AuthUtils {
private AuthUtils() {
throw new IllegalStateException("Utility class");
}
private static final String BASIC_ = "Basic ";
/**
@@ -60,7 +65,7 @@ public class AuthUtils {
/**
* *从header 请求中的clientId:clientSecret
*/
public static String[] extractClient(HttpServletRequest request) throws IOException, UnapprovedClientAuthenticationException {
public static String[] extractClient(HttpServletRequest request) {
String header = request.getHeader("Authorization");
if (header == null || !header.startsWith(BASIC_)) {
throw new UnapprovedClientAuthenticationException("请求头中client信息为空");
@@ -73,10 +78,10 @@ public class AuthUtils {
*
* @param header header中的参数
*/
public static String[] extractHeaderClient(String header) throws IOException {
byte[] base64Client = header.substring(BASIC_.length()).getBytes(CommonConstant.UTF8);
public static String[] extractHeaderClient(String header) {
byte[] base64Client = header.substring(BASIC_.length()).getBytes(StandardCharsets.UTF_8);
byte[] decoded = Base64.getDecoder().decode(base64Client);
String clientStr = new String(decoded, CommonConstant.UTF8);
String clientStr = new String(decoded, StandardCharsets.UTF_8);
String[] clientArr = clientStr.split(":");
if (clientArr.length != 2) {
throw new RuntimeException("Invalid basic authentication token");