parent
daf76a07bb
commit
859bbdf9c7
22 changed files with 488 additions and 39 deletions
@ -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> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -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…
Reference in new issue