🔖 Releasing / Version tags. v2.1.0

This commit is contained in:
冷冷 committed 2019-01-09 11:12:04 +08:00
1 parent f484b09395
commit 58b25756e7
34 files changed
+155 -173

No files matched your search

@@ -20,6 +20,7 @@
package com.pig4cloud.pigx.auth.config;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
import com.pig4cloud.pigx.common.security.component.PigxWebResponseExceptionTranslator;
import com.pig4cloud.pigx.common.security.service.PigxClientDetailsService;
import com.pig4cloud.pigx.common.security.service.PigxUser;
@@ -36,6 +37,8 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.A
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
@@ -90,6 +93,12 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
public TokenStore tokenStore() {
RedisTokenStore tokenStore = new RedisTokenStore(redisConnectionFactory);
tokenStore.setPrefix(SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX);
tokenStore.setAuthenticationKeyGenerator(new DefaultAuthenticationKeyGenerator() {
@Override
public String extractKey(OAuth2Authentication authentication) {
return super.extractKey(authentication) + ":" + TenantContextHolder.getTenantId();
}
});
return tokenStore;
}
@@ -99,6 +108,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
final Map<String, Object> additionalInfo = new HashMap<>(8);
PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();
additionalInfo.put("user_id", pigxUser.getId());
additionalInfo.put("username", pigxUser.getUsername());
additionalInfo.put("dept_id", pigxUser.getDeptId());
additionalInfo.put("tenant_id", pigxUser.getTenantId());
additionalInfo.put("license", SecurityConstants.PIGX_LICENSE);
@@ -26,27 +26,25 @@ import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.constant.PaginationConstants;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
import com.pig4cloud.pigx.common.security.annotation.Inner;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import lombok.AllArgsConstructor;
import org.springframework.cache.CacheManager;
import org.springframework.data.redis.core.ConvertingCursor;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.HttpHeaders;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -59,7 +57,8 @@ import java.util.Map;
@AllArgsConstructor
@RequestMapping("/token")
public class PigxTokenEndpoint {
private static final String PIGX_OAUTH_ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX + "access:";
private static final String PIGX_OAUTH_ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX + "auth_to_access:";
private static final String PIGX__ACCESS = SecurityConstants.PIGX_PREFIX + SecurityConstants.OAUTH_PREFIX + "access:";
private final TokenStore tokenStore;
private final RedisTemplate redisTemplate;
private final CacheManager cacheManager;
@@ -113,7 +112,9 @@ public class PigxTokenEndpoint {
@Inner
@DeleteMapping("/{token}")
public R<Boolean> delToken(@PathVariable("token") String token) {
return new R<>(redisTemplate.delete(PIGX_OAUTH_ACCESS + token));
OAuth2AccessToken oAuth2AccessToken = tokenStore.readAccessToken(token);
tokenStore.removeAccessToken(oAuth2AccessToken);
return new R<>();
}
@@ -126,56 +127,19 @@ public class PigxTokenEndpoint {
@Inner
@PostMapping("/page")
public R<Page> tokenList(@RequestBody Map<String, Object> params) {
List<Map<String, String>> list = new ArrayList<>();
//根据分页参数获取对应数据
List<String> pages = findKeysForPage(PIGX_OAUTH_ACCESS + "*", MapUtil.getInt(params, PaginationConstants.CURRENT), MapUtil.getInt(params, PaginationConstants.SIZE));
for (String page : pages) {
String accessToken = StrUtil.subAfter(page, PIGX_OAUTH_ACCESS, true);
OAuth2AccessToken token = tokenStore.readAccessToken(accessToken);
Map<String, String> map = new HashMap<>(8);
map.put("token_type", token.getTokenType());
map.put("token_value", token.getValue());
map.put("expires_in", token.getExpiresIn() + "");
OAuth2Authentication oAuth2Auth = tokenStore.readAuthentication(token);
Authentication authentication = oAuth2Auth.getUserAuthentication();
map.put("client_id", oAuth2Auth.getOAuth2Request().getClientId());
map.put("grant_type", oAuth2Auth.getOAuth2Request().getGrantType());
if (authentication instanceof UsernamePasswordAuthenticationToken) {
UsernamePasswordAuthenticationToken authenticationToken = (UsernamePasswordAuthenticationToken) authentication;
if (extractToken(map, authenticationToken.getPrincipal())) {
continue;
}
} else if (authentication instanceof PreAuthenticatedAuthenticationToken) {
//刷新token方式
PreAuthenticatedAuthenticationToken authenticationToken = (PreAuthenticatedAuthenticationToken) authentication;
if (extractToken(map, authenticationToken.getPrincipal())) {
continue;
}
}
list.add(map);
}
String key = String.format("%s*:%s", PIGX_OAUTH_ACCESS, TenantContextHolder.getTenantId());
List<String> pages = findKeysForPage(key, MapUtil.getInt(params, PaginationConstants.CURRENT)
, MapUtil.getInt(params, PaginationConstants.SIZE));
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
Page result = new Page(MapUtil.getInt(params, PaginationConstants.CURRENT), MapUtil.getInt(params, PaginationConstants.SIZE));
result.setRecords(list);
result.setTotal(Long.valueOf(redisTemplate.keys(PIGX_OAUTH_ACCESS + "*").size()));
result.setRecords(redisTemplate.opsForValue().multiGet(pages));
result.setTotal(Long.valueOf(redisTemplate.keys(key).size()));
return new R<>(result);
}
private boolean extractToken(Map<String, String> map, Object principal) {
if (principal instanceof PigxUser) {
PigxUser user = (PigxUser) principal;
map.put("user_id", user.getId() + "");
map.put("user_name", user.getUsername() + "");
}
return false;
}
private List<String> findKeysForPage(String patternKey, int pageNum, int pageSize) {
ScanOptions options = ScanOptions.scanOptions().match(patternKey).build();