客户资源后台的认证

master
river 4 years ago
parent daf76a07bb
commit 859bbdf9c7
  1. 20
      dq-financial-crms-auth/pom.xml
  2. 6
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/DqFinancialCrmsAuthApplication.java
  3. 104
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/config/AuthorizationServer.java
  4. 68
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/config/SecurityConfig.java
  5. 22
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/config/TokenConfig.java
  6. 27
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/controller/CustomerLoginController.java
  7. 14
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/dao/CustomerLoginDao.java
  8. 8
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/service/CustomerLoginService.java
  9. 39
      dq-financial-crms-auth/src/main/java/com/daqing/financial/crauth/service/impl/CustomerLoginServiceImpl.java
  10. 1
      dq-financial-crms-auth/src/main/resources/application.properties
  11. 19
      dq-financial-crms-auth/src/main/resources/bootstrap.properties
  12. 10
      dq-financial-crms-auth/src/main/resources/mapper/crmsauth/CustomerLoginMapper.xml
  13. 112
      dq-financial-crms/src/main/java/com/daqing/financial/crms/service/impl/CustomerServiceImpl.java
  14. BIN
      dq-financial-crms/target/classes/com/daqing/financial/crms/feign/HrmsFeignService.class
  15. BIN
      dq-financial-crms/target/classes/com/daqing/financial/crms/service/impl/CustomerServiceImpl.class
  16. BIN
      dq-financial-hrms/target/classes/com/daqing/financial/hrms/controller/EmployeeController.class
  17. BIN
      dq-financial-hrms/target/classes/com/daqing/financial/hrms/dao/EmployeeDao.class
  18. BIN
      dq-financial-hrms/target/classes/com/daqing/financial/hrms/service/EmployeeService.class
  19. BIN
      dq-financial-hrms/target/classes/com/daqing/financial/hrms/service/impl/EmployeeServiceImpl.class
  20. 6
      dq-financial-hrms/target/classes/mapper/hrms/EmployeeDao.xml
  21. 20
      dq-framework-model/src/main/java/com/daqing/framework/domain/crms/ext/ManagerName.java
  22. 51
      dq-framework-model/src/main/java/com/daqing/framework/domain/crms/response/CrmsCode.java

@ -38,6 +38,26 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<!-- 指明版本,解决redis存储出现的问题:java.lang.NoSuchMethodError: org.springframework.data.redis.connection.RedisConnection.set([B[B)V问题 -->
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

@ -2,8 +2,14 @@ package com.daqing.financial.crauth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@EnableFeignClients(basePackages = "com.daqing.financial.crauth.feign")
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan(basePackages = "com.daqing")
public class DqFinancialCrmsAuthApplication {
public static void main(String[] args) {

@ -0,0 +1,104 @@
package com.daqing.financial.crauth.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authorization.AuthorityReactiveAuthorizationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
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.ClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;
/**
* 配置OAuth2.0授权服务器
*
* @auther River
* @date 2020/9/21 17:49
*/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private AuthorizationCodeServices authorizationCodeServices;
/**
* 配置令牌的安全约束(允许哪些请求访问)
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.tokenKeyAccess("permitAll()") // 公开提供公钥加密的端点(就是使用jwt令牌的时候需要的)
.checkTokenAccess("permitAll()") // 校验令牌
.allowFormAuthenticationForClients(); //允许表单提交
}
/**
* 配置支持哪些客户端访问
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory() // 配置在内存里,后期配置在数据库
.withClient("river") // 客户端id
.secret(new BCryptPasswordEncoder().encode("secret")) // 客户端秘钥(后期客户端访问会带着这个秘钥)
.resourceIds("resource1") // 客户端可以访问的资源列表(支持多个)
.authorizedGrantTypes("authorization_code","password","client_credentials","implicit","refresh_token") // 该客户端允许授权的方式
.scopes("all") // 允许授权的范围
.autoApprove(false) // false表示跳转到授权页面授权
.redirectUris("http://www.baidu.com"); // 加上验证回调地址
}
/**
* 配置令牌(token)的访问端点
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager) // 密码模式需要
.authorizationCodeServices(authorizationCodeServices) // 授权码模式需要
.tokenServices(tokenServices()) // 令牌的管理方式
.allowedTokenEndpointRequestMethods(HttpMethod.POST); // 允许的请求方式
}
/**
* 令牌服务
*/
@Bean
public AuthorizationServerTokenServices tokenServices(){
DefaultTokenServices service = new DefaultTokenServices();
service.setClientDetailsService(clientDetailsService); // 客户端信息的服务
service.setSupportRefreshToken(true); // 是否产生刷新令牌
service.setTokenStore(tokenStore); // 令牌的存储策略
service.setAccessTokenValiditySeconds(7200); // 令牌有效期
service.setRefreshTokenValiditySeconds(259200); // 刷新令牌有效期
return service;
}
/**
* 授权码服务(设置授权码模式的授权码如何存取,暂时在内存,后期在数据库)
*/
@Bean
public AuthorizationCodeServices authorizationCodeServices(){
return new InMemoryAuthorizationCodeServices();
}
}

@ -0,0 +1,68 @@
package com.daqing.financial.crauth.config;
import com.daqing.financial.crauth.service.impl.CustomerLoginServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.authentication.PasswordEncoderParser;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
/**
* @auther River
* @date 2020/9/22 10:26
*/
@Configuration
@EnableWebSecurity // 开启security
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 不能直接new对象,否则会在注入之前被拦截器拦截
*/
@Bean
public CustomerLoginServiceImpl customerLoginService(){
return new CustomerLoginServiceImpl();
}
// 定义用户信息(查询用户信息),security帮助我们查询,但是需要告诉他如何去查询
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customerLoginService());
}
// 密码编码器,比对密码的方式
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
/**
* 认证管理器
*/
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
// 安全拦截机制
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/*").authenticated() // 该路径下所有请求都会被拦截
.anyRequest().permitAll() // 其余的请求可以通过
.and()
.formLogin() // 允许表单认证
.successForwardUrl("/customerLogin/loginSuccess"); // 登录成功跳转路径
}
}

@ -0,0 +1,22 @@
package com.daqing.financial.crauth.config;
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.InMemoryTokenStore;
/**
* 令牌配置类
*
* @auther River
* @date 2020/9/22 9:54
*/
@Configuration
public class TokenConfig {
@Bean
public TokenStore tokenStore(){
// 内存生成,普通令牌
return new InMemoryTokenStore();
}
}

@ -0,0 +1,27 @@
package com.daqing.financial.crauth.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @auther River
* @date 2020/9/22 15:27
*/
@RestController
@RequestMapping("/customerLogin")
public class CustomerLoginController {
@PostMapping("/loginSuccess")
public String loginSuccess(){
return "success";
}
@GetMapping("/test")
public String test(){
return "Hello";
}
}

@ -0,0 +1,14 @@
package com.daqing.financial.crauth.dao;
import com.daqing.framework.domain.crms.CustomerEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* @auther River
* @date 2020/9/22 14:55
*/
@Mapper
public interface CustomerLoginDao {
CustomerEntity getCustomer(String code);
}

@ -0,0 +1,8 @@
package com.daqing.financial.crauth.service;
/**
* @auther River
* @date 2020/9/22 15:00
*/
public interface CustomerLoginService {
}

@ -0,0 +1,39 @@
package com.daqing.financial.crauth.service.impl;
import com.daqing.financial.crauth.dao.CustomerLoginDao;
import com.daqing.financial.crauth.service.CustomerLoginService;
import com.daqing.framework.domain.crms.CustomerEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
* @auther River
* @date 2020/9/22 15:01
*/
@Service
public class CustomerLoginServiceImpl implements CustomerLoginService, UserDetailsService {
@Autowired
private CustomerLoginDao customerLoginDao;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
CustomerEntity customer = customerLoginDao.getCustomer(username);
List<SimpleGrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
System.out.println(customer);
UserDetails userDetails = new User(customer.getCode(),new BCryptPasswordEncoder().encode(customer.getPassword()),authorities);
return userDetails;
}
}

@ -0,0 +1,19 @@
#·þÎñÃû³Æ
spring.application.name=dq-financial-crms
#ÅäÖÃÖÐÐĵØÖ·
spring.cloud.nacos.config.server-addr=192.168.31.142:8848
spring.cloud.nacos.config.namespace=37d72d30-3178-4173-8b5e-269a23355ed9
#spring.cloud.nacos.config.group=prod
spring.cloud.nacos.config.ext-config[0].data-id=datasource.yml
spring.cloud.nacos.config.ext-config[0].group=dev
spring.cloud.nacos.config.ext-config[0].refresh=true
spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[1].group=dev
spring.cloud.nacos.config.ext-config[1].refresh=true
spring.cloud.nacos.config.ext-config[2].data-id=other.yml
spring.cloud.nacos.config.ext-config[2].group=dev
spring.cloud.nacos.config.ext-config[2].refresh=true

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.daqing.financial.crauth.dao.CustomerLoginDao">
<select id="getCustomer" parameterType="string" resultType="com.daqing.framework.domain.crms.CustomerEntity">
SELECT code,password FROM crms_customer WHERE code = #{code}
</select>
</mapper>

@ -12,13 +12,13 @@ import com.daqing.financial.crms.service.CustomerService;
import com.daqing.framework.domain.crms.CompanyCustomerEntity;
import com.daqing.framework.domain.crms.CustomerEntity;
import com.daqing.framework.domain.crms.PersonalCustomerEntity;
import com.daqing.framework.domain.crms.ext.CrmsConstant;
import com.daqing.framework.domain.crms.ext.CustomerTO;
import com.daqing.framework.domain.crms.ext.CustomerCompanyVO;
import com.daqing.framework.domain.crms.ext.CustomerPersonalVO;
import com.daqing.framework.domain.crms.ext.*;
import com.daqing.framework.domain.crms.request.CustomerRequest;
import com.daqing.framework.domain.crms.response.CrmsCode;
import com.daqing.framework.domain.hrms.ext.EmployeeTO;
import com.daqing.framework.domain.hrms.ext.EmployeeVO;
import com.daqing.framework.domain.hrms.response.HrmsCode;
import com.daqing.framework.exception.ExceptionCast;
import com.daqing.framework.model.response.ResponseResult;
import com.daqing.framework.utils.PageUtils;
import com.daqing.framework.utils.SnowflakeIdUtils;
@ -208,25 +208,30 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity
/**
* 导出数据到excel
* @param response
* @param response 响应
*/
@Transactional
@Override
public void excelExportCustomer(HttpServletResponse response){
//TODO 查询所有的客户详细信息,并按照类型封装,后续有待优化,消耗时间太长
List<CustomerCompanyVO> companyList = new ArrayList<>();
List<CustomerPersonalVO> personalList = new ArrayList<>();
List<Long> longList = customerDao.listCustomerId();
for (Long id : longList) {
ResponseResult responseResult = this.queryCustomerById(id);
if ((responseResult.getData()).getClass() == CustomerCompanyVO.class){
companyList.add((CustomerCompanyVO) responseResult.getData());
}else {
personalList.add((CustomerPersonalVO) responseResult.getData());
try {
List<CustomerCompanyVO> companyList = new ArrayList<>();
List<CustomerPersonalVO> personalList = new ArrayList<>();
List<Long> longList = customerDao.listCustomerId();
for (Long id : longList) {
ResponseResult responseResult = this.queryCustomerById(id);
if ((responseResult.getData()).getClass() == CustomerCompanyVO.class){
companyList.add((CustomerCompanyVO) responseResult.getData());
}else {
personalList.add((CustomerPersonalVO) responseResult.getData());
}
}
ExcelUtil.writeExcelWithSheets(response,personalList,"客户资源信息一览表","个人类型",new CustomerPersonalVO())
.write(companyList,"企业类型",new CustomerCompanyVO())
.finish();
}catch (Exception e){
ExceptionCast.cast(CrmsCode.CUSTOMER_EXPORT_EXSIT);
}
ExcelUtil.writeExcelWithSheets(response,personalList,"客户资源信息一览表","个人类型",new CustomerPersonalVO())
.write(companyList,"企业类型",new CustomerCompanyVO())
.finish();
}
/**
@ -234,37 +239,68 @@ public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity
*/
@Override
public void excelTemplate(HttpServletResponse response) {
List<CustomerCompanyVO> companyList = new ArrayList<>();
List<CustomerPersonalVO> personalList = new ArrayList<>();
ExcelUtil.writeExcelWithSheets(response,personalList,"客户资源信息表模板","个人类型",new CustomerPersonalVO())
.write(companyList,"企业类型",new CustomerCompanyVO())
.finish();
try {
List<CustomerCompanyVO> companyList = new ArrayList<>();
List<CustomerPersonalVO> personalList = new ArrayList<>();
ExcelUtil.writeExcelWithSheets(response,personalList,"客户资源信息表模板","个人类型",new CustomerPersonalVO())
.write(companyList,"企业类型",new CustomerCompanyVO())
.finish();
}catch (Exception e){
ExceptionCast.cast(CrmsCode.CUSTOMER_EXPORTTEMPLATE_EXSIT);
}
}
/**
* 导入excel数据
* @param excel
*/
@Transactional
@Override
public void excelImportCustomer(MultipartFile excel) {
//TODO 导入excel数据到数据库,思路耗时太长,有待优化
List<Object> personalList = ExcelUtil.readExcel(excel, new CustomerPersonalVO(), 1);
List<Object> companyList = ExcelUtil.readExcel(excel, new CustomerCompanyVO(), 2);
List<EmployeeVO> nameList = hrmsFeignService.listEmployeeName().getData();
List<CustomerEntity> customerList = new ArrayList<>();
CustomerEntity customer;
List<PersonalCustomerEntity> pcList = new ArrayList<>();
PersonalCustomerEntity pc;
for (Object personal : personalList) {
customer = new CustomerEntity();
pc = new PersonalCustomerEntity();
BeanUtils.copyProperties(personal,customer);
BeanUtils.copyProperties(personal,pc);
customerList.add(customer);
pcList.add(pc);
try {
List<Object> personalList = ExcelUtil.readExcel(excel, new CustomerPersonalVO(), 1);
List<Object> companyList = ExcelUtil.readExcel(excel, new CustomerCompanyVO(), 2);
List<EmployeeVO> nameList = hrmsFeignService.listEmployeeName().getData();
CustomerEntity customer; // 客户基本信息
PersonalCustomerEntity pc; // 个人类型信息
CompanyCustomerEntity cc; // 企业类型信息
ManagerName manager; // 客户名称
// 个人类型
for (Object personal : personalList) {
customer = new CustomerEntity();
manager = new ManagerName();
pc = new PersonalCustomerEntity();
BeanUtils.copyProperties(personal,customer);
BeanUtils.copyProperties(personal,pc);
BeanUtils.copyProperties(personal,manager);
// 根据客户经理名称找到对应的员工id
for (EmployeeVO employeeVO : nameList){
if (Objects.equals(manager.getManager(),employeeVO.getName())){
customer.setManager(employeeVO.getId());
}
}
this.saveCustomerPersonal(customer,pc);
}
// 企业类型
for (Object company : companyList){
customer = new CustomerEntity();
manager = new ManagerName();
cc = new CompanyCustomerEntity();
BeanUtils.copyProperties(company,customer);
BeanUtils.copyProperties(company,cc);
BeanUtils.copyProperties(company,manager);
// 根据客户经理名称找到对应的员工id
for (EmployeeVO employeeVO : nameList){
if (Objects.equals(manager.getManager(),employeeVO.getName())){
customer.setManager(employeeVO.getId());
}
}
this.saveCustomerCompany(customer,cc);
}
}catch (Exception e){
ExceptionCast.cast(CrmsCode.CUSTOMER_IMPORT_EXSIT);
}
System.out.println(customerList);
System.out.println(pcList);
}
/**

@ -57,4 +57,10 @@
ON ed.dept_id = d.id
WHERE d.id = #{id}
</select>
<!-- 查询所有的员工姓名和id -->
<select id="listEmployeeName" resultType="com.daqing.framework.domain.hrms.ext.EmployeeVO">
SELECT id,name
FROM hrms_employee
</select>
</mapper>

@ -0,0 +1,20 @@
package com.daqing.framework.domain.crms.ext;
import lombok.Data;
import lombok.ToString;
/**
* 用于接收从excel导入的数据中客户的客户经理姓名
* 从而比对所有的员工name找到对应的id
* 用ExcelUtil.readExcel()中获取的数据无法强转类型而通过get方法获取属性值
* 故而通过类来接收
*
* @auther River
* @date 2020/9/21 9:25
*/
@Data
@ToString
public class ManagerName {
private String manager;
}

@ -0,0 +1,51 @@
package com.daqing.framework.domain.crms.response;
import com.daqing.framework.model.response.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
/**
* 客户资源管理操作状态集
*
* @auther River
* @date 2020/9/21 10:16
*/
@ToString
@AllArgsConstructor
public enum CrmsCode implements ResultCode {
CUSTOMER_IMPORT_EXSIT(false,20001,"导入数据失败,请检查文件和数据格式或稍后再试!"),
CUSTOMER_EXPORT_EXSIT(false,20002,"导出数据失败,请稍后再试!"),
CUSTOMER_EXPORTTEMPLATE_EXSIT(false,20003,"导出excel模板失败,请稍后再试!");
/**
* 操作是否成功
*/
@Getter
private boolean success;
/**
* 状态码
*/
@Getter
private int code;
/**
* 提示信息
*/
@Getter
private String message;
public boolean success() {
return this.success;
}
public int code() {
return this.code;
}
public String message() {
return this.message;
}
}
Loading…
Cancel
Save