Browse Source

框架修改

master
ccsens_zhengzhichuan 1 month ago
parent
commit
eaa55cf2e7
  1. 4
      research-admin/src/main/java/com/research/web/controller/tenant/TmsLoginController.java
  2. 2
      research-common/src/main/java/com/research/common/utils/SecurityUtils.java
  3. 26
      research-framework/src/main/java/com/research/framework/config/SecurityConfig.java
  4. 36
      research-framework/src/main/java/com/research/framework/datasource/DynamicDataSource.java
  5. 3
      research-framework/src/main/java/com/research/framework/web/service/UserDetailsServiceImpl.java
  6. 2
      research-framework/src/main/java/com/research/framework/web/service/WebTmsLoginService.java
  7. 14
      research-system/src/main/java/com/research/system/service/impl/TmsTenantUserServiceImpl.java

4
research-admin/src/main/java/com/research/web/controller/tenant/TmsLoginController.java

@ -1,5 +1,6 @@
package com.research.web.controller.tenant; package com.research.web.controller.tenant;
import com.research.common.annotation.Anonymous;
import com.research.common.annotation.DataSource; import com.research.common.annotation.DataSource;
import com.research.common.annotation.Log; import com.research.common.annotation.Log;
import com.research.common.constant.Constants; import com.research.common.constant.Constants;
@ -52,6 +53,7 @@ public class TmsLoginController {
* @param loginBody 登录信息 * @param loginBody 登录信息
* @return 结果 * @return 结果
*/ */
@Anonymous
@PostMapping("/login") @PostMapping("/login")
@DataSource(DataSourceType.MASTER) @DataSource(DataSourceType.MASTER)
public AjaxResult login(@RequestBody LoginBody loginBody) { public AjaxResult login(@RequestBody LoginBody loginBody) {
@ -69,6 +71,7 @@ public class TmsLoginController {
* @param loginBody 登录信息 * @param loginBody 登录信息
* @return 结果 * @return 结果
*/ */
@Anonymous
@DataSource(DataSourceType.MASTER) @DataSource(DataSourceType.MASTER)
@PostMapping("/loginSimple") @PostMapping("/loginSimple")
public AjaxResult loginSimple(@RequestBody LoginBody loginBody) { public AjaxResult loginSimple(@RequestBody LoginBody loginBody) {
@ -144,6 +147,7 @@ public class TmsLoginController {
* @param dto * @param dto
* @return * @return
*/ */
@Anonymous
@PostMapping("/queryTenantById") @PostMapping("/queryTenantById")
@DataSource(DataSourceType.MASTER) @DataSource(DataSourceType.MASTER)
public JsonResponse<TmsTenant> queryTenantById(@RequestBody TmsLoginUserVo.Query dto){ public JsonResponse<TmsTenant> queryTenantById(@RequestBody TmsLoginUserVo.Query dto){

2
research-common/src/main/java/com/research/common/utils/SecurityUtils.java

@ -186,7 +186,7 @@ public class SecurityUtils
} }
catch (Exception e) catch (Exception e)
{ {
return 1901557972215377920L; return 1L;
// throw new ServiceException("获取租户ID异常", HttpStatus.UNAUTHORIZED); // throw new ServiceException("获取租户ID异常", HttpStatus.UNAUTHORIZED);
} }
} }

26
research-framework/src/main/java/com/research/framework/config/SecurityConfig.java

@ -1,5 +1,6 @@
package com.research.framework.config; package com.research.framework.config;
import com.research.framework.security.provider.DmsUserAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -75,7 +76,7 @@ public class SecurityConfig
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(userDetailsService); daoAuthenticationProvider.setUserDetailsService(userDetailsService);
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder()); daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder());
return new ProviderManager(daoAuthenticationProvider); return new ProviderManager(daoAuthenticationProvider, dmsUserAuthenticationProvider());
} }
/** /**
@ -111,7 +112,7 @@ public class SecurityConfig
.authorizeHttpRequests((requests) -> { .authorizeHttpRequests((requests) -> {
permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll()); permitAllUrl.getUrls().forEach(url -> requests.antMatchers(url).permitAll());
// 对于登录login 注册register 验证码captchaImage 允许匿名访问 // 对于登录login 注册register 验证码captchaImage 允许匿名访问
requests.antMatchers("/login", "/register", "/captchaImage").permitAll() requests.antMatchers("/login", "/register", "/captchaImage", "web/login", "web/loginSimple").permitAll()
// 静态资源,可匿名访问 // 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
@ -136,4 +137,25 @@ public class SecurityConfig
{ {
return new BCryptPasswordEncoder(); return new BCryptPasswordEncoder();
} }
@Bean
public DmsUserAuthenticationProvider dmsUserAuthenticationProvider(){
return new DmsUserAuthenticationProvider();
}
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// //填报端认证
// auth.authenticationProvider(dmsUserAuthenticationProvider());
// //用户名密码认证
// auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
// }
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
// ... 其他配置
.authenticationProvider(dmsUserAuthenticationProvider()); // 注入自定义 Provider
return http.build();
}
} }

36
research-framework/src/main/java/com/research/framework/datasource/DynamicDataSource.java

@ -1,5 +1,7 @@
package com.research.framework.datasource; package com.research.framework.datasource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map; import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
@ -23,4 +25,38 @@ public class DynamicDataSource extends AbstractRoutingDataSource
{ {
return DynamicDataSourceContextHolder.getDataSourceType(); return DynamicDataSourceContextHolder.getDataSourceType();
} }
/**
* 切换数据源
*
* @return
*/
@Override
protected DataSource determineTargetDataSource() {
Object dataSourceKey = this.determineCurrentLookupKey();
// 默认系统数据源
if (dataSourceKey == null) {
return super.getResolvedDefaultDataSource();
}
DataSource dataSource = DataSourceManager.DATA_SOURCE_POOL_JDBC.get(dataSourceKey);
if (dataSource == null) {
throw new RuntimeException("数据源不存在!");
}
return dataSource;
}
/**
* 获取连接
*
* @return
* @throws SQLException
*/
@Override
public Connection getConnection() throws SQLException {
Connection connection = this.determineTargetDataSource().getConnection();
return connection;
}
} }

3
research-framework/src/main/java/com/research/framework/web/service/UserDetailsServiceImpl.java

@ -1,6 +1,7 @@
package com.research.framework.web.service; package com.research.framework.web.service;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import com.research.framework.datasource.DataSourceManager;
import com.research.framework.datasource.DynamicDataSourceContextHolder; import com.research.framework.datasource.DynamicDataSourceContextHolder;
import com.research.system.domain.vo.TmsLoginUserVo; import com.research.system.domain.vo.TmsLoginUserVo;
import com.research.system.service.TmsLoginService; import com.research.system.service.TmsLoginService;
@ -91,6 +92,8 @@ public class UserDetailsServiceImpl implements UserDetailsService
DynamicDataSourceContextHolder.setDataSourceType(dataScoreVo.getDataSourceKey()); DynamicDataSourceContextHolder.setDataSourceType(dataScoreVo.getDataSourceKey());
//查询用户密码 //查询用户密码
SysUser sysUser = userService.selectUserByUserName(dataScoreVo.getUsername()); SysUser sysUser = userService.selectUserByUserName(dataScoreVo.getUsername());
//销毁
DynamicDataSourceContextHolder.clearDataSourceType();
if (sysUser == null) { if (sysUser == null) {
throw new ServiceException(MessageUtils.message("user.not.exists")); throw new ServiceException(MessageUtils.message("user.not.exists"));
} }

2
research-framework/src/main/java/com/research/framework/web/service/WebTmsLoginService.java

@ -109,7 +109,7 @@ public class WebTmsLoginService
public String loginSimple(String username, String password, String code, String uuid) public String loginSimple(String username, String password, String code, String uuid)
{ {
// 验证码校验 // 验证码校验
validateCaptcha(username, code, uuid); // validateCaptcha(username, code, uuid);
// 登录前置校验 // 登录前置校验
loginPreCheck(username, password); loginPreCheck(username, password);
// 用户验证 // 用户验证

14
research-system/src/main/java/com/research/system/service/impl/TmsTenantUserServiceImpl.java

@ -2,10 +2,10 @@ package com.research.system.service.impl;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import com.research.system.domain.dto.TmsTenantUserDto; import com.research.system.domain.dto.TmsTenantUserDto;
import com.research.system.domain.po.SrvDataSource; import com.research.system.domain.po.*;
import com.research.system.domain.po.TmsTenantUser;
import com.research.system.domain.po.TmsTenantUserExample;
import com.research.system.domain.vo.AdminTenantUserVo; import com.research.system.domain.vo.AdminTenantUserVo;
import com.research.system.persist.mapper.SrvDataSourceMapper;
import com.research.system.persist.mapper.TmsTenantMapper;
import com.research.system.persist.mapper.TmsTenantUserMapper; import com.research.system.persist.mapper.TmsTenantUserMapper;
import com.research.system.service.TmsTenantUserService; import com.research.system.service.TmsTenantUserService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -24,9 +24,17 @@ public class TmsTenantUserServiceImpl implements TmsTenantUserService {
@Resource @Resource
private TmsTenantUserMapper tmsTenantUserMapper; private TmsTenantUserMapper tmsTenantUserMapper;
@Resource
private TmsTenantMapper tmsTenantMapper;
@Resource
private SrvDataSourceMapper srvDataSourceMapper;
@Override @Override
public SrvDataSource getDataSourceByTenantId(Long tenantId) { public SrvDataSource getDataSourceByTenantId(Long tenantId) {
TmsTenant tmsTenant = tmsTenantMapper.selectByPrimaryKey(tenantId);
if (tmsTenant != null && tmsTenant.getDataSourceId() != null) {
return srvDataSourceMapper.selectByPrimaryKey(tmsTenant.getDataSourceId());
}
return null; return null;
} }

Loading…
Cancel
Save