在做项目重构的时候增加了两个功能
1、多数据源。
2、token的验证从以前的数据库验证,移到了redis端。
1、多数据源使用 druid-spring-boot-starter 套件
其核心代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@Configurationpublic class DynamicDataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.first")
public DataSource firstDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.second")
public DataSource secondDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) {
Map<String, DataSource> targetDataSources = new HashMap<>();
//targetDataSources.put(DataSourceContext.FIRST, firstDataSource);
targetDataSources.put(DataSourceContext.SECOND, secondDataSource);
return new DynamicDataSource(firstDataSource, targetDataSources);
}
} |
2、token验证规则使用spring-shiro,核心代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
@Componentpublic class OAuth2Realm extends AuthorizingRealm {
// @Autowired// private ShiroService shiroService; @Autowired
private RedisTokenManager redisTokenManager;
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof OAuth2Token;
}
/**
* 授权(验证权限时调用)
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// SysUserEntity user = (SysUserEntity)principals.getPrimaryPrincipal();// Long userId = user.getUserId();//// //用户权限列表// Set<String> permsSet = shiroService.getUserPermissions(userId);//// SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();// info.setStringPermissions(permsSet);// return info; return null;
}
/**
* 认证(登录时调用)
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String accessToken = (String) token.getPrincipal();
// UsernamePasswordToken up = (UsernamePasswordToken) token; //根据accessToken,查询用户信息
TokenModel tokenEntity = redisTokenManager.getToken(accessToken);
//token失效
if(tokenEntity == null ){
throw new IncorrectCredentialsException("token失效,请重新登录");
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(tokenEntity.getUserId(), accessToken, getName());
return info;
// if(tokenEntity.getType()==null||// Integer.valueOf(tokenEntity.getType()).equals(Constant.LoginUserType.CUSTOM.getValue())){// //查询用户信息// SysUserEntity user = shiroService.queryUser(tokenEntity.getUserId());// //账号锁定// if(user.getStatus() == 0){// throw new LockedAccountException("账号已被锁定,请联系管理员");// }// SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());// return info;// }else{// //查询用户信息// SysUserEntity user = shiroService.queryUser(tokenEntity.getUserId());// //账号锁定// if(user.getStatus() == 0){// throw new LockedAccountException("账号已被锁定,请联系管理员");// }// SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());// return info;// } }
} |
然后启动项目,就出现datasource循环依赖的问题。
The dependencies of some of the beans in the application context form a cycle:
evaluationCarService (field private io.yeliang.business.dao.EvaluationCarDao io.yeliang.business.service.impl.EvaluationCarServiceImpl.evaluationCarDao)
↓
evaluationCarDao defined in file [D:\workspace\tmxc-parent\tmxc-order-service\target\classes\io\yeliang\business\dao\EvaluationCarDao.class]
↓
sqlSessionFactory defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
┌─────┐
| dataSource defined in class path resource [io/yeliang/dynamicdatasource/DynamicDataSourceConfig.class]
↑ ↓
| firstDataSource defined in class path resource [io/yeliang/dynamicdatasource/DynamicDataSourceConfig.class]
↑ ↓
| dataSourceInitializer
└─────┘
过程就不过了,痛苦。
。
。
。
。
。
。
结论是把
|
1
2
3
4
|
@Componentpublic class OAuth2Realm extends AuthorizingRealm {
// @Autowired// private ShiroService shiroService;<br>中这两行的注解打开就行。或随便找个service注册都行。<br>目的是让datasource提前注册到spring容器。<br><br>这个肯定不是好办法,先这样吧。受不了了。记录一下。 |
1 |
有更好的办法,欢迎留言。谢谢 |
在做项目重构的时候增加了两个功能
1、多数据源。
2、token的验证从以前的数据库验证,移到了redis端。
1、多数据源使用 druid-spring-boot-starter 套件
其核心代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@Configurationpublic class DynamicDataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.first")
public DataSource firstDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.second")
public DataSource secondDataSource(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) {
Map<String, DataSource> targetDataSources = new HashMap<>();
//targetDataSources.put(DataSourceContext.FIRST, firstDataSource);
targetDataSources.put(DataSourceContext.SECOND, secondDataSource);
return new DynamicDataSource(firstDataSource, targetDataSources);
}
} |
2、token验证规则使用spring-shiro,核心代码如下
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
@Componentpublic class OAuth2Realm extends AuthorizingRealm {
// @Autowired// private ShiroService shiroService; @Autowired
private RedisTokenManager redisTokenManager;
@Override
public boolean supports(AuthenticationToken token) {
return token instanceof OAuth2Token;
}
/**
* 授权(验证权限时调用)
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// SysUserEntity user = (SysUserEntity)principals.getPrimaryPrincipal();// Long userId = user.getUserId();//// //用户权限列表// Set<String> permsSet = shiroService.getUserPermissions(userId);//// SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();// info.setStringPermissions(permsSet);// return info; return null;
}
/**
* 认证(登录时调用)
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String accessToken = (String) token.getPrincipal();
// UsernamePasswordToken up = (UsernamePasswordToken) token; //根据accessToken,查询用户信息
TokenModel tokenEntity = redisTokenManager.getToken(accessToken);
//token失效
if(tokenEntity == null ){
throw new IncorrectCredentialsException("token失效,请重新登录");
}
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(tokenEntity.getUserId(), accessToken, getName());
return info;
// if(tokenEntity.getType()==null||// Integer.valueOf(tokenEntity.getType()).equals(Constant.LoginUserType.CUSTOM.getValue())){// //查询用户信息// SysUserEntity user = shiroService.queryUser(tokenEntity.getUserId());// //账号锁定// if(user.getStatus() == 0){// throw new LockedAccountException("账号已被锁定,请联系管理员");// }// SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());// return info;// }else{// //查询用户信息// SysUserEntity user = shiroService.queryUser(tokenEntity.getUserId());// //账号锁定// if(user.getStatus() == 0){// throw new LockedAccountException("账号已被锁定,请联系管理员");// }// SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());// return info;// } }
} |
然后启动项目,就出现datasource循环依赖的问题。
The dependencies of some of the beans in the application context form a cycle:
evaluationCarService (field private io.yeliang.business.dao.EvaluationCarDao io.yeliang.business.service.impl.EvaluationCarServiceImpl.evaluationCarDao)
↓
evaluationCarDao defined in file [D:\workspace\tmxc-parent\tmxc-order-service\target\classes\io\yeliang\business\dao\EvaluationCarDao.class]
↓
sqlSessionFactory defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
┌─────┐
| dataSource defined in class path resource [io/yeliang/dynamicdatasource/DynamicDataSourceConfig.class]
↑ ↓
| firstDataSource defined in class path resource [io/yeliang/dynamicdatasource/DynamicDataSourceConfig.class]
↑ ↓
| dataSourceInitializer
└─────┘
过程就不过了,痛苦。
。
。
。
。
。
。
结论是把
|
1
2
3
4
|
@Componentpublic class OAuth2Realm extends AuthorizingRealm {
// @Autowired// private ShiroService shiroService;<br>中这两行的注解打开就行。或随便找个service注册都行。<br>目的是让datasource提前注册到spring容器。<br><br>这个肯定不是好办法,先这样吧。受不了了。记录一下。 |
1 |
有更好的办法,欢迎留言。谢谢 |