优化代码支持多用户类型扩展
This commit is contained in:
+8
-3
@@ -1,16 +1,21 @@
|
||||
package com.central.oauth2.common.config;
|
||||
package com.central.oauth2.common;
|
||||
|
||||
import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import com.central.oauth2.common.properties.TokenStoreProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* 鉴权自动配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/10/7
|
||||
* @version 1.0
|
||||
* @date 2021/7/24
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@EnableConfigurationProperties({SecurityProperties.class, TokenStoreProperties.class})
|
||||
public class SecurityPropertiesConfig {
|
||||
@ComponentScan
|
||||
public class AuthClientAutoConfiguration {
|
||||
}
|
||||
+2
@@ -3,6 +3,7 @@ package com.central.oauth2.common.store;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
|
||||
|
||||
@@ -14,6 +15,7 @@ import javax.sql.DataSource;
|
||||
* @author zlt
|
||||
* @date 2018/7/24 16:23
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "db")
|
||||
public class AuthDbTokenStore {
|
||||
@Autowired
|
||||
|
||||
+2
@@ -5,6 +5,7 @@ import com.central.oauth2.common.converter.CustomUserAuthenticationConverter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
|
||||
@@ -28,6 +29,7 @@ import java.util.Map;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "authJwt")
|
||||
public class AuthJwtTokenStore {
|
||||
@Resource
|
||||
|
||||
+2
@@ -4,6 +4,7 @@ import com.central.oauth2.common.properties.SecurityProperties;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
@@ -18,6 +19,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "redis", matchIfMissing = true)
|
||||
public class AuthRedisTokenStore {
|
||||
@Bean
|
||||
|
||||
+2
@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
@@ -31,6 +32,7 @@ import java.util.stream.Collectors;
|
||||
* @author zlt
|
||||
* @date 2018/8/20 9:25
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(prefix = "zlt.oauth2.token.store", name = "type", havingValue = "resJwt")
|
||||
public class ResJwtTokenStore {
|
||||
@Autowired
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.central.oauth2.common.token;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 表单登录的认证信息对象
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2021/7/24
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Getter
|
||||
public class CustomWebAuthenticationDetails implements Serializable {
|
||||
private static final long serialVersionUID = - 1;
|
||||
|
||||
private final String accountType;
|
||||
private final String remoteAddress;
|
||||
private final String sessionId;
|
||||
|
||||
public CustomWebAuthenticationDetails(String remoteAddress, String sessionId, String accountType) {
|
||||
this.remoteAddress = remoteAddress;
|
||||
this.sessionId = sessionId;
|
||||
this.accountType = accountType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(super.toString()).append("; accountType: ").append(this.getAccountType());
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+21
-1
@@ -1,17 +1,19 @@
|
||||
package com.central.oauth2.common.util;
|
||||
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.oauth2.common.token.CustomWebAuthenticationDetails;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 认证授权相关工具类
|
||||
@@ -104,4 +106,22 @@ public class AuthUtils {
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登陆的帐户类型
|
||||
*/
|
||||
public static String getAccountType(Authentication authentication) {
|
||||
Object details = authentication.getDetails();
|
||||
String accountType = null;
|
||||
if (details instanceof CustomWebAuthenticationDetails) {
|
||||
CustomWebAuthenticationDetails detailsObj = (CustomWebAuthenticationDetails) details;
|
||||
accountType = detailsObj.getAccountType();
|
||||
} else {
|
||||
Map<String, String> detailsMap = (Map<String, String>) details;
|
||||
if (detailsMap != null) {
|
||||
accountType = detailsMap.get(SecurityConstants.ACCOUNT_TYPE_PARAM_NAME);
|
||||
}
|
||||
}
|
||||
return accountType;
|
||||
}
|
||||
}
|
||||
|
||||
+1
-5
@@ -1,6 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.oauth2.common.config.SecurityPropertiesConfig,\
|
||||
com.central.oauth2.common.store.AuthDbTokenStore,\
|
||||
com.central.oauth2.common.store.AuthRedisTokenStore,\
|
||||
com.central.oauth2.common.store.AuthJwtTokenStore,\
|
||||
com.central.oauth2.common.store.ResJwtTokenStore
|
||||
com.central.oauth2.common.AuthClientAutoConfiguration
|
||||
+18
@@ -4,6 +4,9 @@ package com.central.common.constant;
|
||||
* Security 权限常量
|
||||
*
|
||||
* @author zlt
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public interface SecurityConstants {
|
||||
/**
|
||||
@@ -31,6 +34,11 @@ public interface SecurityConstants {
|
||||
*/
|
||||
String TENANT_HEADER = "x-tenant-header";
|
||||
|
||||
/**
|
||||
* 账号类型信息头
|
||||
*/
|
||||
String ACCOUNT_TYPE_HEADER = "x-account-type-header";
|
||||
|
||||
/**
|
||||
* 基础角色
|
||||
*/
|
||||
@@ -167,4 +175,14 @@ public interface SecurityConstants {
|
||||
* 令牌颁发者
|
||||
*/
|
||||
String ISS = "http://zlt2000.cn";
|
||||
|
||||
/**
|
||||
* 默认账号类型
|
||||
*/
|
||||
String DEF_ACCOUNT_TYPE = "admin";
|
||||
|
||||
/**
|
||||
* 账号类型参数名
|
||||
*/
|
||||
String ACCOUNT_TYPE_PARAM_NAME = "account_type";
|
||||
}
|
||||
|
||||
+5
@@ -23,6 +23,9 @@ import java.util.List;
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2018/12/21
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Slf4j
|
||||
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
@@ -61,6 +64,8 @@ public class TokenArgumentResolver implements HandlerMethodArgumentResolver {
|
||||
String userId = request.getHeader(SecurityConstants.USER_ID_HEADER);
|
||||
String username = request.getHeader(SecurityConstants.USER_HEADER);
|
||||
String roles = request.getHeader(SecurityConstants.ROLE_HEADER);
|
||||
//账号类型
|
||||
String accountType = request.getHeader(SecurityConstants.ACCOUNT_TYPE_HEADER);
|
||||
if (StrUtil.isBlank(username)) {
|
||||
log.warn("resolveArgument error username is empty");
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user