Skip to content

Commit f91378d

Browse files
authored
Merge pull request #41 from zlt2000/dev
Dev
2 parents f183773 + 51b69fa commit f91378d

File tree

22 files changed

+362
-11
lines changed

22 files changed

+362
-11
lines changed

zlt-commons/zlt-auth-client-spring-boot-starter/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@
4242
<artifactId>javax.servlet-api</artifactId>
4343
<scope>provided</scope>
4444
</dependency>
45+
<dependency>
46+
<groupId>org.apache.tomcat.embed</groupId>
47+
<artifactId>tomcat-embed-websocket</artifactId>
48+
<optional>true</optional>
49+
</dependency>
4550
</dependencies>
4651
</project>

zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/config/DefaultResourceServerConf.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.security.config.http.SessionCreationPolicy;
1010
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
1111
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
12+
import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
1213
import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;
1314
import org.springframework.security.oauth2.provider.expression.OAuth2WebSecurityExpressionHandler;
1415
import org.springframework.security.oauth2.provider.token.TokenStore;
@@ -36,13 +37,17 @@ public class DefaultResourceServerConf extends ResourceServerConfigurerAdapter {
3637
@Autowired
3738
private SecurityProperties securityProperties;
3839

40+
@Resource
41+
private TokenExtractor tokenExtractor;
42+
3943
@Override
4044
public void configure(ResourceServerSecurityConfigurer resources) {
4145
resources.tokenStore(tokenStore)
4246
.stateless(true)
4347
.authenticationEntryPoint(authenticationEntryPoint)
4448
.expressionHandler(expressionHandler)
45-
.accessDeniedHandler(oAuth2AccessDeniedHandler);
49+
.accessDeniedHandler(oAuth2AccessDeniedHandler)
50+
.tokenExtractor(tokenExtractor);
4651
}
4752

4853
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.central.oauth2.common.config;
2+
3+
import com.central.oauth2.common.util.AuthUtils;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.web.context.request.RequestContextHolder;
6+
import org.springframework.web.context.request.ServletRequestAttributes;
7+
8+
import javax.websocket.server.ServerEndpointConfig;
9+
10+
/**
11+
* webSocket鉴权配置
12+
*
13+
* @author zlt
14+
* @version 1.0
15+
* @date 2022/5/8
16+
* <p>
17+
* Blog: https://zlt2000.gitee.io
18+
* Github: https://github.com/zlt2000
19+
*/
20+
@Slf4j
21+
public class WcAuthConfigurator extends ServerEndpointConfig.Configurator {
22+
@Override
23+
public boolean checkOrigin(String originHeaderValue) {
24+
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
25+
try {
26+
//检查token有效性
27+
AuthUtils.checkAccessToken(servletRequestAttributes.getRequest());
28+
} catch (Exception e) {
29+
log.error("WebSocket-auth-error", e);
30+
return false;
31+
}
32+
return super.checkOrigin(originHeaderValue);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.central.oauth2.common.service.impl;
2+
3+
import com.central.oauth2.common.properties.SecurityProperties;
4+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
5+
import org.springframework.security.core.Authentication;
6+
import org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.util.AntPathMatcher;
9+
10+
import javax.annotation.Resource;
11+
import javax.servlet.http.HttpServletRequest;
12+
13+
/**
14+
* 自定义 TokenExtractor
15+
*
16+
* @author zlt
17+
* @version 1.0
18+
* @date 2022/6/4
19+
* <p>
20+
* Blog: https://zlt2000.gitee.io
21+
* Github: https://github.com/zlt2000
22+
*/
23+
@ConditionalOnClass(HttpServletRequest.class)
24+
@Component
25+
public class CustomBearerTokenExtractor extends BearerTokenExtractor {
26+
@Resource
27+
private SecurityProperties securityProperties;
28+
29+
private final AntPathMatcher antPathMatcher = new AntPathMatcher();
30+
31+
/**
32+
* 解决只要请求携带access_token,排除鉴权的url依然会被拦截
33+
*/
34+
@Override
35+
public Authentication extract(HttpServletRequest request) {
36+
//判断当前请求为排除鉴权的url时,直接返回null
37+
for (String url : securityProperties.getIgnore().getUrls()) {
38+
if (antPathMatcher.match(url, request.getRequestURI())) {
39+
return null;
40+
}
41+
}
42+
return super.extract(request);
43+
}
44+
}

zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/util/AuthUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
import com.central.common.constant.CommonConstant;
44
import com.central.common.constant.SecurityConstants;
55
import com.central.common.model.SysUser;
6+
import com.central.common.utils.SpringUtil;
67
import com.central.oauth2.common.token.CustomWebAuthenticationDetails;
78
import lombok.extern.slf4j.Slf4j;
89
import org.springframework.security.core.Authentication;
910
import org.springframework.security.oauth2.common.OAuth2AccessToken;
11+
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
1012
import org.springframework.security.oauth2.common.exceptions.UnapprovedClientAuthenticationException;
13+
import org.springframework.security.oauth2.provider.OAuth2Authentication;
14+
import org.springframework.security.oauth2.provider.token.TokenStore;
1115

1216
import javax.servlet.http.HttpServletRequest;
1317
import java.nio.charset.StandardCharsets;
@@ -66,6 +70,29 @@ private static String extractHeaderToken(HttpServletRequest request) {
6670
return null;
6771
}
6872

73+
/**
74+
* 校验accessToken
75+
*/
76+
public static void checkAccessToken(HttpServletRequest request) {
77+
String accessToken = extractToken(request);
78+
checkAccessToken(accessToken);
79+
}
80+
81+
public static void checkAccessToken(String accessTokenValue) {
82+
TokenStore tokenStore = SpringUtil.getBean(TokenStore.class);
83+
OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
84+
if (accessToken == null || accessToken.getValue() == null) {
85+
throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
86+
} else if (accessToken.isExpired()) {
87+
tokenStore.removeAccessToken(accessToken);
88+
throw new InvalidTokenException("Access token expired: " + accessTokenValue);
89+
}
90+
OAuth2Authentication result = tokenStore.readAuthentication(accessToken);
91+
if (result == null) {
92+
throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
93+
}
94+
}
95+
6996
/**
7097
* *从header 请求中的clientId:clientSecret
7198
*/

zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/SpringUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.context.ApplicationContext;
44
import org.springframework.context.ApplicationContextAware;
5+
import org.springframework.core.annotation.Order;
56
import org.springframework.core.env.Environment;
67
import org.springframework.stereotype.Component;
78

@@ -11,6 +12,7 @@
1112
* @author 作者 owen E-mail: 624191343@qq.com
1213
*/
1314
@Component
15+
@Order(0)
1416
public class SpringUtil implements ApplicationContextAware {
1517

1618
private static ApplicationContext applicationContext = null;

zlt-commons/zlt-common-core/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ com.central.common.config.BannerInitializer
33

44
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
55
com.central.common.feign.fallback.UserServiceFallbackFactory,\
6-
com.central.common.lock.LockAspect
6+
com.central.common.lock.LockAspect,\
7+
com.central.common.utils.SpringUtil

zlt-commons/zlt-db-spring-boot-starter/src/main/java/com/central/db/config/MybatisPlusAutoConfigure.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ public class MybatisPlusAutoConfigure {
4040
@Bean
4141
public MybatisPlusInterceptor paginationInterceptor() {
4242
MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
43-
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
4443
boolean enableTenant = tenantProperties.getEnable();
4544
//是否开启多租户隔离
4645
if (enableTenant) {
4746
CustomTenantInterceptor tenantInterceptor = new CustomTenantInterceptor(
4847
tenantLineHandler, tenantProperties.getIgnoreSqls());
4948
mpInterceptor.addInnerInterceptor(tenantInterceptor);
5049
}
50+
mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
5151
return mpInterceptor;
5252
}
5353

zlt-commons/zlt-loadbalancer-spring-boot-starter/src/main/java/com/central/common/lb/chooser/RandomRuleChooser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.central.common.lb.chooser;
22

33
import com.alibaba.nacos.common.utils.CollectionUtils;
4-
import lombok.extern.log4j.Log4j2;
4+
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.cloud.client.ServiceInstance;
66

77
import java.util.List;
@@ -12,7 +12,7 @@
1212
*
1313
* @author jarvis create by 2022/3/13
1414
*/
15-
@Log4j2
15+
@Slf4j
1616
public class RandomRuleChooser implements IRuleChooser {
1717
@Override
1818
public ServiceInstance choose(List<ServiceInstance> instances) {

zlt-commons/zlt-loadbalancer-spring-boot-starter/src/main/java/com/central/common/lb/chooser/RoundRuleChooser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.central.common.lb.chooser;
22

33
import com.alibaba.nacos.common.utils.CollectionUtils;
4-
import lombok.extern.log4j.Log4j2;
4+
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.cloud.client.ServiceInstance;
66

77
import java.util.List;
@@ -12,7 +12,7 @@
1212
*
1313
* @author jarvis create by 2022/3/13
1414
*/
15-
@Log4j2
15+
@Slf4j
1616
public class RoundRuleChooser implements IRuleChooser{
1717

1818
private AtomicInteger position;

0 commit comments

Comments
 (0)