mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-03 17:42:20 +08:00
feat:feign整合sentinel
This commit is contained in:
parent
d1e261339b
commit
08a78772ac
@ -2,7 +2,7 @@ package com.youlai.mall.oms;
|
|||||||
|
|
||||||
|
|
||||||
import com.youlai.mall.pms.api.app.PmsSkuFeignService;
|
import com.youlai.mall.pms.api.app.PmsSkuFeignService;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||||
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
|
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@ -12,7 +12,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
|
|||||||
|
|
||||||
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@EnableFeignClients(basePackageClasses = { UmsMemberFeignService.class, PmsSkuFeignService.class})
|
@EnableFeignClients(basePackageClasses = { MemberFeignClient.class, PmsSkuFeignService.class})
|
||||||
@EnableRabbit
|
@EnableRabbit
|
||||||
public class OmsApplication {
|
public class OmsApplication {
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package com.youlai.mall.oms.controller.admin;
|
package com.youlai.mall.oms.controller.admin;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
@ -10,7 +9,7 @@ import com.youlai.mall.oms.pojo.domain.OmsOrder;
|
|||||||
import com.youlai.mall.oms.pojo.domain.OmsOrderItem;
|
import com.youlai.mall.oms.pojo.domain.OmsOrderItem;
|
||||||
import com.youlai.mall.oms.service.IOrderItemService;
|
import com.youlai.mall.oms.service.IOrderItemService;
|
||||||
import com.youlai.mall.oms.service.IOrderService;
|
import com.youlai.mall.oms.service.IOrderService;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||||
import com.youlai.mall.ums.pojo.dto.MemberDTO;
|
import com.youlai.mall.ums.pojo.dto.MemberDTO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiImplicitParam;
|
import io.swagger.annotations.ApiImplicitParam;
|
||||||
@ -39,7 +38,7 @@ public class OrderController {
|
|||||||
|
|
||||||
private IOrderService orderService;
|
private IOrderService orderService;
|
||||||
private IOrderItemService orderItemService;
|
private IOrderItemService orderItemService;
|
||||||
private UmsMemberFeignService memberFeignService;
|
private MemberFeignClient memberFeignClient;
|
||||||
|
|
||||||
@ApiOperation("订单列表")
|
@ApiOperation("订单列表")
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@ -70,7 +69,7 @@ public class OrderController {
|
|||||||
orderItems = Optional.ofNullable(orderItems).orElse(new ArrayList<>());
|
orderItems = Optional.ofNullable(orderItems).orElse(new ArrayList<>());
|
||||||
|
|
||||||
// 会员明细
|
// 会员明细
|
||||||
Result<MemberDTO> result = memberFeignService.getUserById(order.getMemberId());
|
Result<MemberDTO> result = memberFeignClient.getUserById(order.getMemberId());
|
||||||
MemberDTO member = result.getData();
|
MemberDTO member = result.getData();
|
||||||
orderBO.setOrder(order).setOrderItems(orderItems).setMember(member);
|
orderBO.setOrder(order).setOrderItems(orderItems).setMember(member);
|
||||||
return Result.success(orderBO);
|
return Result.success(orderBO);
|
||||||
|
@ -27,11 +27,10 @@ import com.youlai.mall.oms.service.ICartService;
|
|||||||
import com.youlai.mall.oms.service.IOrderItemService;
|
import com.youlai.mall.oms.service.IOrderItemService;
|
||||||
import com.youlai.mall.oms.service.IOrderService;
|
import com.youlai.mall.oms.service.IOrderService;
|
||||||
import com.youlai.mall.pms.api.app.PmsSkuFeignService;
|
import com.youlai.mall.pms.api.app.PmsSkuFeignService;
|
||||||
import com.youlai.mall.pms.pojo.domain.PmsSku;
|
|
||||||
import com.youlai.mall.pms.pojo.dto.SkuDTO;
|
import com.youlai.mall.pms.pojo.dto.SkuDTO;
|
||||||
import com.youlai.mall.pms.pojo.dto.SkuLockDTO;
|
import com.youlai.mall.pms.pojo.dto.SkuLockDTO;
|
||||||
import com.youlai.mall.ums.api.UmsAddressFeignService;
|
import com.youlai.mall.ums.api.MemberAddressFeignClient;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||||
import com.youlai.mall.ums.pojo.domain.UmsAddress;
|
import com.youlai.mall.ums.pojo.domain.UmsAddress;
|
||||||
import io.seata.spring.annotation.GlobalTransactional;
|
import io.seata.spring.annotation.GlobalTransactional;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
@ -59,12 +58,12 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
|
|||||||
|
|
||||||
private ICartService cartService;
|
private ICartService cartService;
|
||||||
private PmsSkuFeignService skuFeignService;
|
private PmsSkuFeignService skuFeignService;
|
||||||
private UmsAddressFeignService addressFeignService;
|
private MemberAddressFeignClient addressFeignService;
|
||||||
private IOrderItemService orderItemService;
|
private IOrderItemService orderItemService;
|
||||||
private RabbitTemplate rabbitTemplate;
|
private RabbitTemplate rabbitTemplate;
|
||||||
private StringRedisTemplate redisTemplate;
|
private StringRedisTemplate redisTemplate;
|
||||||
private ThreadPoolExecutor threadPoolExecutor;
|
private ThreadPoolExecutor threadPoolExecutor;
|
||||||
private UmsMemberFeignService memberFeignService;
|
private MemberFeignClient memberFeignClient;
|
||||||
|
|
||||||
private BusinessNoGenerator businessNoGenerator;
|
private BusinessNoGenerator businessNoGenerator;
|
||||||
|
|
||||||
@ -233,7 +232,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OmsOrder> impleme
|
|||||||
// 扣减余额
|
// 扣减余额
|
||||||
Long userId = RequestUtils.getUserId();
|
Long userId = RequestUtils.getUserId();
|
||||||
Long payAmount = order.getPayAmount();
|
Long payAmount = order.getPayAmount();
|
||||||
Result deductBalanceResult = memberFeignService.deductBalance(userId, payAmount);
|
Result deductBalanceResult = memberFeignClient.deductBalance(userId, payAmount);
|
||||||
if (!Result.isSuccess(deductBalanceResult)) {
|
if (!Result.isSuccess(deductBalanceResult)) {
|
||||||
throw new BizException("扣减账户余额失败");
|
throw new BizException("扣减账户余额失败");
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@FeignClient(name = "mall-ums",contextId = "address")
|
@FeignClient(name = "mall-ums",contextId = "address")
|
||||||
public interface UmsAddressFeignService {
|
public interface MemberAddressFeignClient {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取地址详情
|
* 获取地址详情
|
@ -8,7 +8,7 @@ import org.springframework.cloud.openfeign.FeignClient;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@FeignClient(name = "mall-ums",contextId = "member")
|
@FeignClient(name = "mall-ums",contextId = "member")
|
||||||
public interface UmsMemberFeignService {
|
public interface MemberFeignClient {
|
||||||
|
|
||||||
@PostMapping("/api.app/v1/members")
|
@PostMapping("/api.app/v1/members")
|
||||||
Result add(@RequestBody UmsMember user);
|
Result add(@RequestBody UmsMember user);
|
@ -1,13 +1,14 @@
|
|||||||
package com.youlai.admin.api;
|
package com.youlai.admin.api;
|
||||||
|
|
||||||
|
import com.youlai.admin.api.fallback.UserFeignFallbackClient;
|
||||||
import com.youlai.admin.pojo.dto.UserDTO;
|
import com.youlai.admin.pojo.dto.UserDTO;
|
||||||
import com.youlai.common.result.Result;
|
import com.youlai.common.result.Result;
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
|
||||||
@FeignClient(value = "youlai-admin")
|
@FeignClient(value = "youlai-admin", fallback = UserFeignFallbackClient.class)
|
||||||
public interface UserFeignService {
|
public interface UserFeignClient {
|
||||||
|
|
||||||
@GetMapping("/api.admin/v1/users/username/{username}")
|
@GetMapping("/api.admin/v1/users/username/{username}")
|
||||||
Result<UserDTO> getUserByUsername(@PathVariable String username);
|
Result<UserDTO> getUserByUsername(@PathVariable String username);
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.youlai.admin.api.fallback;
|
||||||
|
|
||||||
|
import com.youlai.admin.api.UserFeignClient;
|
||||||
|
import com.youlai.admin.pojo.dto.UserDTO;
|
||||||
|
import com.youlai.common.result.Result;
|
||||||
|
import com.youlai.common.result.ResultCode;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author haoxr
|
||||||
|
* @createTime 2021/4/24 21:30
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class UserFeignFallbackClient implements UserFeignClient {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Result<UserDTO> getUserByUsername(String username) {
|
||||||
|
log.error("Feign远程调用服务发生故障,获取用户信息失败降级");
|
||||||
|
return Result.failed(ResultCode.DEGRADATION);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
|
com.youlai.admin.api.fallback.UserFeignFallbackClient
|
@ -1,7 +1,6 @@
|
|||||||
package com.youlai.admin.controller;
|
package com.youlai.admin.controller;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
|
||||||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||||
@ -146,6 +145,8 @@ public class UserController extends BaseController {
|
|||||||
public Result getUserByUsername(
|
public Result getUserByUsername(
|
||||||
@PathVariable String username
|
@PathVariable String username
|
||||||
) {
|
) {
|
||||||
|
log.info("进入getUserByUsername方法");
|
||||||
|
int i = 1 / 0;
|
||||||
SysUser user = iSysUserService.getOne(new LambdaQueryWrapper<SysUser>()
|
SysUser user = iSysUserService.getOne(new LambdaQueryWrapper<SysUser>()
|
||||||
.eq(SysUser::getUsername, username));
|
.eq(SysUser::getUsername, username));
|
||||||
|
|
||||||
@ -169,10 +170,13 @@ public class UserController extends BaseController {
|
|||||||
|
|
||||||
|
|
||||||
@ApiOperation(value = "获取当前登陆的用户信息")
|
@ApiOperation(value = "获取当前登陆的用户信息")
|
||||||
@SentinelResource(value = "getCurrentUser",blockHandlerClass = UserBlockHandler.class,blockHandler =
|
@SentinelResource(value = "getCurrentUser",
|
||||||
"handleGetCurrentUserBlock")
|
blockHandlerClass = UserBlockHandler.class, blockHandler = "handleGetCurrentUserBlock"
|
||||||
|
)
|
||||||
@GetMapping("/me")
|
@GetMapping("/me")
|
||||||
public Result<UserVO> getCurrentUser() {
|
public Result<UserVO> getCurrentUser() {
|
||||||
|
log.info("获取当前登陆的用户信息 begin");
|
||||||
|
|
||||||
UserVO userVO = new UserVO();
|
UserVO userVO = new UserVO();
|
||||||
|
|
||||||
// 用户基本信息
|
// 用户基本信息
|
||||||
|
@ -3,14 +3,28 @@ package com.youlai.admin.handler;
|
|||||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||||
import com.youlai.admin.pojo.vo.UserVO;
|
import com.youlai.admin.pojo.vo.UserVO;
|
||||||
import com.youlai.common.result.Result;
|
import com.youlai.common.result.Result;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 用户接口降级逻辑
|
||||||
* @author haoxr
|
* @author haoxr
|
||||||
* @description TODO
|
|
||||||
* @createTime 2021/4/23 23:30
|
* @createTime 2021/4/23 23:30
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class UserBlockHandler {
|
public class UserBlockHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户信息的熔断降级处理
|
||||||
|
* @param blockException
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public static Result<UserVO> handleGetCurrentUserBlock(BlockException blockException) {
|
public static Result<UserVO> handleGetCurrentUserBlock(BlockException blockException) {
|
||||||
return Result.success(new UserVO());
|
return Result.success(new UserVO());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Result handleGetUserByUsernameBlock(String username,BlockException blockException){
|
||||||
|
log.info("降级了:{}",username);
|
||||||
|
return Result.failed("降级 了");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ spring:
|
|||||||
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
file-extension: yaml
|
file-extension: yaml
|
||||||
sentinel:
|
sentinel:
|
||||||
enabled: false
|
enabled: true
|
||||||
eager: true # 取消控制台懒加载,项目启动即连接Sentinel
|
eager: true # 取消控制台懒加载,项目启动即连接Sentinel
|
||||||
transport:
|
transport:
|
||||||
client-ip: localhost
|
client-ip: localhost
|
||||||
|
@ -63,6 +63,17 @@
|
|||||||
<artifactId>spring-security-oauth2-jose</artifactId>
|
<artifactId>spring-security-oauth2-jose</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Sentinel流量控制、熔断降级 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<!-- Sentinel规则持久化至Nacos配置 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.csp</groupId>
|
||||||
|
<artifactId>sentinel-datasource-nacos</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.youlai</groupId>
|
<groupId>com.youlai</groupId>
|
||||||
<artifactId>ums-api</artifactId>
|
<artifactId>ums-api</artifactId>
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
package com.youlai.auth;
|
package com.youlai.auth;
|
||||||
|
|
||||||
import com.youlai.admin.api.UserFeignService;
|
import com.youlai.admin.api.UserFeignClient;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
|
||||||
@EnableFeignClients(basePackageClasses = {UserFeignService.class, UmsMemberFeignService.class})
|
@EnableFeignClients(basePackageClasses = {UserFeignClient.class, MemberFeignClient.class})
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
public class AuthApplication {
|
public class AuthApplication {
|
||||||
|
@ -3,7 +3,6 @@ package com.youlai.auth.config;
|
|||||||
import cn.hutool.http.HttpStatus;
|
import cn.hutool.http.HttpStatus;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.youlai.auth.domain.User;
|
import com.youlai.auth.domain.User;
|
||||||
import com.youlai.auth.filter.CustomClientCredentialsTokenEndpointFilter;
|
|
||||||
import com.youlai.auth.service.JdbcClientDetailsServiceImpl;
|
import com.youlai.auth.service.JdbcClientDetailsServiceImpl;
|
||||||
import com.youlai.auth.service.UserDetailsServiceImpl;
|
import com.youlai.auth.service.UserDetailsServiceImpl;
|
||||||
import com.youlai.common.constant.AuthConstants;
|
import com.youlai.common.constant.AuthConstants;
|
||||||
@ -17,6 +16,8 @@ import org.springframework.core.io.ClassPathResource;
|
|||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
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.AuthorizationServerConfigurerAdapter;
|
||||||
@ -47,6 +48,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
|||||||
private DataSource dataSource;
|
private DataSource dataSource;
|
||||||
private AuthenticationManager authenticationManager;
|
private AuthenticationManager authenticationManager;
|
||||||
private UserDetailsServiceImpl userDetailsService;
|
private UserDetailsServiceImpl userDetailsService;
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置客户端详情(数据库)
|
* 配置客户端详情(数据库)
|
||||||
@ -86,20 +88,13 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configure(AuthorizationServerSecurityConfigurer security) {
|
public void configure(AuthorizationServerSecurityConfigurer security) {
|
||||||
/*security.allowFormAuthenticationForClients();*/
|
security.allowFormAuthenticationForClients();
|
||||||
CustomClientCredentialsTokenEndpointFilter endpointFilter = new CustomClientCredentialsTokenEndpointFilter(security);
|
|
||||||
endpointFilter.afterPropertiesSet();
|
|
||||||
endpointFilter.setAuthenticationEntryPoint(authenticationEntryPoint());
|
|
||||||
security.addTokenEndpointAuthenticationFilter(endpointFilter);
|
|
||||||
|
|
||||||
security.authenticationEntryPoint(authenticationEntryPoint())
|
|
||||||
.tokenKeyAccess("isAuthenticated()")
|
|
||||||
.checkTokenAccess("permitAll()");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义认证异常响应数据
|
* 自定义认证异常响应数据
|
||||||
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
@ -133,8 +128,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
|||||||
public KeyPair keyPair() {
|
public KeyPair keyPair() {
|
||||||
KeyStoreKeyFactory factory = new KeyStoreKeyFactory(
|
KeyStoreKeyFactory factory = new KeyStoreKeyFactory(
|
||||||
new ClassPathResource("youlai.jks"), "123456".toCharArray());
|
new ClassPathResource("youlai.jks"), "123456".toCharArray());
|
||||||
KeyPair keyPair = factory.getKeyPair(
|
KeyPair keyPair = factory.getKeyPair("youlai", "123456".toCharArray());
|
||||||
"youlai", "123456".toCharArray());
|
|
||||||
return keyPair;
|
return keyPair;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,9 +142,18 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
|||||||
User user = (User) authentication.getUserAuthentication().getPrincipal();
|
User user = (User) authentication.getUserAuthentication().getPrincipal();
|
||||||
map.put(AuthConstants.USER_ID_KEY, user.getId());
|
map.put(AuthConstants.USER_ID_KEY, user.getId());
|
||||||
map.put(AuthConstants.CLIENT_ID_KEY, user.getClientId());
|
map.put(AuthConstants.CLIENT_ID_KEY, user.getClientId());
|
||||||
map.put(AuthConstants.USER_NAME_KEY,user.getUsername());
|
map.put(AuthConstants.USER_NAME_KEY, user.getUsername());
|
||||||
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map);
|
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(map);
|
||||||
return accessToken;
|
return accessToken;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DaoAuthenticationProvider authenticationProvider() {
|
||||||
|
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
|
||||||
|
provider.setHideUserNotFoundExceptions(false);
|
||||||
|
provider.setUserDetailsService(userDetailsService);
|
||||||
|
provider.setPasswordEncoder(passwordEncoder);
|
||||||
|
return provider;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ import com.youlai.common.result.Result;
|
|||||||
import com.youlai.common.result.ResultCode;
|
import com.youlai.common.result.ResultCode;
|
||||||
import com.youlai.common.web.exception.BizException;
|
import com.youlai.common.web.exception.BizException;
|
||||||
import com.youlai.common.web.util.RequestUtils;
|
import com.youlai.common.web.util.RequestUtils;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||||
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
import com.youlai.mall.ums.pojo.domain.UmsMember;
|
||||||
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
@ -20,7 +20,6 @@ import io.swagger.annotations.ApiOperation;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
|
||||||
import org.apache.logging.log4j.util.Strings;
|
import org.apache.logging.log4j.util.Strings;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
import org.springframework.security.oauth2.common.OAuth2AccessToken;
|
||||||
@ -86,7 +85,7 @@ public class AuthController {
|
|||||||
|
|
||||||
|
|
||||||
private WxMaService wxService;
|
private WxMaService wxService;
|
||||||
private UmsMemberFeignService memberFeignService;
|
private MemberFeignClient memberFeignClient;
|
||||||
private PasswordEncoder passwordEncoder;
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@ -104,7 +103,7 @@ public class AuthController {
|
|||||||
String openid = session.getOpenid();
|
String openid = session.getOpenid();
|
||||||
String sessionKey = session.getSessionKey();
|
String sessionKey = session.getSessionKey();
|
||||||
|
|
||||||
Result<AuthMemberDTO> result = memberFeignService.getUserByOpenid(openid);
|
Result<AuthMemberDTO> result = memberFeignClient.getUserByOpenid(openid);
|
||||||
|
|
||||||
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员
|
if (ResultCode.USER_NOT_EXIST.getCode().equals(result.getCode())) { // 微信授权登录 会员信息不存在时 注册会员
|
||||||
String encryptedData = parameters.get("encryptedData");
|
String encryptedData = parameters.get("encryptedData");
|
||||||
@ -123,7 +122,7 @@ public class AuthController {
|
|||||||
.setPassword(passwordEncoder.encode(openid).replace(AuthConstants.BCRYPT, Strings.EMPTY)) // 加密密码移除前缀加密方式 {bcrypt}
|
.setPassword(passwordEncoder.encode(openid).replace(AuthConstants.BCRYPT, Strings.EMPTY)) // 加密密码移除前缀加密方式 {bcrypt}
|
||||||
.setStatus(GlobalConstants.STATUS_NORMAL_VALUE);
|
.setStatus(GlobalConstants.STATUS_NORMAL_VALUE);
|
||||||
|
|
||||||
Result res = memberFeignService.add(user);
|
Result res = memberFeignClient.add(user);
|
||||||
if (!ResultCode.SUCCESS.getCode().equals(res.getCode())) {
|
if (!ResultCode.SUCCESS.getCode().equals(res.getCode())) {
|
||||||
throw new BizException("注册会员失败");
|
throw new BizException("注册会员失败");
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.youlai.common.result.Result;
|
|||||||
import com.youlai.common.result.ResultCode;
|
import com.youlai.common.result.ResultCode;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
import org.springframework.security.authentication.InternalAuthenticationServiceException;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
|
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
@ -12,6 +13,18 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class AuthExceptionHandler {
|
public class AuthExceptionHandler {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户不存在
|
||||||
|
*
|
||||||
|
* @param e
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@ExceptionHandler(UsernameNotFoundException.class)
|
||||||
|
public Result handleUsernameNotFoundException(UsernameNotFoundException e) {
|
||||||
|
return Result.failed(ResultCode.USER_NOT_EXIST);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户名和密码异常
|
* 用户名和密码异常
|
||||||
*
|
*
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
package com.youlai.auth.filter;
|
|
||||||
|
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
|
||||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
|
|
||||||
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter;
|
|
||||||
import org.springframework.security.web.AuthenticationEntryPoint;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重写filter实现客户端自定义异常处理
|
|
||||||
*/
|
|
||||||
public class CustomClientCredentialsTokenEndpointFilter extends ClientCredentialsTokenEndpointFilter {
|
|
||||||
|
|
||||||
private AuthorizationServerSecurityConfigurer configurer;
|
|
||||||
private AuthenticationEntryPoint authenticationEntryPoint;
|
|
||||||
|
|
||||||
public CustomClientCredentialsTokenEndpointFilter(AuthorizationServerSecurityConfigurer configurer) {
|
|
||||||
this.configurer = configurer;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setAuthenticationEntryPoint(AuthenticationEntryPoint authenticationEntryPoint) {
|
|
||||||
super.setAuthenticationEntryPoint(null);
|
|
||||||
this.authenticationEntryPoint = authenticationEntryPoint;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected AuthenticationManager getAuthenticationManager() {
|
|
||||||
return configurer.and().getSharedObject(AuthenticationManager.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
setAuthenticationFailureHandler((request, response, e) -> authenticationEntryPoint.commence(request, response, e));
|
|
||||||
setAuthenticationSuccessHandler((request, response, authentication) -> {
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +1,16 @@
|
|||||||
package com.youlai.auth.service;
|
package com.youlai.auth.service;
|
||||||
|
|
||||||
import com.youlai.admin.api.UserFeignService;
|
import com.youlai.admin.api.UserFeignClient;
|
||||||
import com.youlai.admin.pojo.dto.UserDTO;
|
import com.youlai.admin.pojo.dto.UserDTO;
|
||||||
import com.youlai.auth.domain.User;
|
import com.youlai.auth.domain.User;
|
||||||
import com.youlai.common.constant.AuthConstants;
|
import com.youlai.common.constant.AuthConstants;
|
||||||
import com.youlai.common.result.Result;
|
import com.youlai.common.result.Result;
|
||||||
import com.youlai.common.result.ResultCode;
|
import com.youlai.common.result.ResultCode;
|
||||||
import com.youlai.common.web.exception.BizException;
|
|
||||||
import com.youlai.common.web.util.RequestUtils;
|
import com.youlai.common.web.util.RequestUtils;
|
||||||
import com.youlai.mall.ums.api.UmsMemberFeignService;
|
import com.youlai.mall.ums.api.MemberFeignClient;
|
||||||
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
import com.youlai.mall.ums.pojo.dto.AuthMemberDTO;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authentication.AccountExpiredException;
|
import org.springframework.security.authentication.AccountExpiredException;
|
||||||
import org.springframework.security.authentication.DisabledException;
|
import org.springframework.security.authentication.DisabledException;
|
||||||
import org.springframework.security.authentication.LockedException;
|
import org.springframework.security.authentication.LockedException;
|
||||||
@ -25,10 +25,11 @@ import org.springframework.stereotype.Service;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class UserDetailsServiceImpl implements UserDetailsService {
|
public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
|
||||||
private UserFeignService userFeignService;
|
private UserFeignClient userFeignClient;
|
||||||
private UmsMemberFeignService memberFeignService;
|
private MemberFeignClient memberFeignClient;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
@ -38,28 +39,24 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
|||||||
Result result;
|
Result result;
|
||||||
switch (clientId) {
|
switch (clientId) {
|
||||||
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
|
case AuthConstants.ADMIN_CLIENT_ID: // 后台用户
|
||||||
result = userFeignService.getUserByUsername(username);
|
result = userFeignClient.getUserByUsername(username);
|
||||||
|
log.info("获取用户信息:{}",result.toString());
|
||||||
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
||||||
UserDTO userDTO = (UserDTO) result.getData();
|
UserDTO userDTO = (UserDTO) result.getData();
|
||||||
user = new User(userDTO);
|
user = new User(userDTO);
|
||||||
} else {
|
|
||||||
throw new BizException(result.getMsg());
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
|
case AuthConstants.WEAPP_CLIENT_ID: // 小程序会员
|
||||||
result = memberFeignService.getUserByOpenid(username);
|
result = memberFeignClient.getUserByOpenid(username);
|
||||||
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
|
||||||
AuthMemberDTO authMemberDTO = (AuthMemberDTO) result.getData();
|
AuthMemberDTO authMemberDTO = (AuthMemberDTO) result.getData();
|
||||||
user = new User(authMemberDTO);
|
user = new User(authMemberDTO);
|
||||||
} else {
|
|
||||||
throw new BizException(result.getMsg());
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (user == null) {
|
if (user == null || user.getId() == null) {
|
||||||
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
|
throw new UsernameNotFoundException(ResultCode.USER_NOT_EXIST.getMsg());
|
||||||
}
|
} else if (!user.isEnabled()) {
|
||||||
if (!user.isEnabled()) {
|
|
||||||
throw new DisabledException("该账户已被禁用!");
|
throw new DisabledException("该账户已被禁用!");
|
||||||
} else if (!user.isAccountNonLocked()) {
|
} else if (!user.isAccountNonLocked()) {
|
||||||
throw new LockedException("该账号已被锁定!");
|
throw new LockedException("该账号已被锁定!");
|
||||||
|
@ -13,3 +13,23 @@ spring:
|
|||||||
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
file-extension: yaml
|
file-extension: yaml
|
||||||
group: DEFAULT_GROUP
|
group: DEFAULT_GROUP
|
||||||
|
sentinel:
|
||||||
|
enabled: true
|
||||||
|
eager: true # 取消控制台懒加载,项目启动即连接Sentinel
|
||||||
|
transport:
|
||||||
|
client-ip: localhost
|
||||||
|
dashboard: localhost:8080
|
||||||
|
datasource:
|
||||||
|
# 降级规则
|
||||||
|
degrade:
|
||||||
|
nacos:
|
||||||
|
server-addr: ${spring.cloud.nacos.discovery.server-addr}
|
||||||
|
dataId: ${spring.application.name}-degrade-rules
|
||||||
|
groupId: SENTINEL_GROUP
|
||||||
|
data-type: json
|
||||||
|
rule-type: degrade
|
||||||
|
|
||||||
|
# 开启feign对sentinel的支持
|
||||||
|
feign:
|
||||||
|
sentinel:
|
||||||
|
enabled: true
|
||||||
|
@ -10,6 +10,8 @@ import java.io.Serializable;
|
|||||||
* @date 2020-06-23
|
* @date 2020-06-23
|
||||||
**/
|
**/
|
||||||
@Data
|
@Data
|
||||||
|
// 忽略null值
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
public class Result<T> implements Serializable {
|
public class Result<T> implements Serializable {
|
||||||
|
|
||||||
private String code;
|
private String code;
|
||||||
@ -18,7 +20,6 @@ public class Result<T> implements Serializable {
|
|||||||
|
|
||||||
private String msg;
|
private String msg;
|
||||||
|
|
||||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
|
||||||
private Integer total;
|
private Integer total;
|
||||||
|
|
||||||
public static <T> Result<T> success() {
|
public static <T> Result<T> success() {
|
||||||
|
@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|||||||
* @author hxrui
|
* @author hxrui
|
||||||
* @date 2020-02-25 13:54
|
* @date 2020-02-25 13:54
|
||||||
**/
|
**/
|
||||||
//@RestControllerAdvice
|
// @RestControllerAdvice
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user