当前位置:   article > 正文

使用spring cloud alibaba-网关(gateway)+安全认证(springsecurity+jwt)_gateway security jwt

gateway security jwt

上一篇博客开头说到了gateway服务之前调用有些特殊,那是因为webflux和spring-webmvc;

gateway不兼容spring-webmvc所以项目创建的时候也就剔除了,剔除之后无法使用HttpServletRequest,所以之前搭建的不能简单的替换zuul,改动太大。只能将zuul从安全认证这个模块中移除,然后独立成一个模块供gateway认证时调用。

改造之前的springsecurity,移除zuul相关依赖,在UserController中新增一个接口

  1. /**
  2. * 认证
  3. * @param token
  4. * @return
  5. */
  6. @RequestMapping(value = "/verificationToken",method = {RequestMethod.POST,RequestMethod.GET})
  7. @ResponseBody
  8. public ResultVO<Boolean> verificationToken(@RequestParam("token") String token){
  9. AuthUser authUser = JwtUtil.parseToken(token);
  10. authUser.getUsername();
  11. return Backtrack.success(true);
  12. }

此方法供gateway调用,Backtrack为一个统一返回和接收校验的类,用于抛请求异常。

  1. package com.cloudalibaba.securitypermission.common.methods;
  2. import com.cloudalibaba.securitypermission.common.vo.ResultVO;
  3. /**
  4. * 返回和接收校验
  5. * @author wqy
  6. * @version 1.0
  7. * @date 2020/6/9 15:52
  8. */
  9. public class Backtrack{
  10. /**
  11. * 成功的执行
  12. * @param t 数据体
  13. * @param msg 备注
  14. * @param <T> 泛型
  15. * @return
  16. */
  17. public static <T> ResultVO<T> success(T t, String msg){
  18. ResultVO<T> resultVO = new ResultVO<>();
  19. resultVO.setCode(10);
  20. resultVO.setData(t);
  21. resultVO.setMsg(msg);
  22. return resultVO;
  23. }
  24. /**
  25. * 成功的执行
  26. * @param t 数据体
  27. * @param <T> 泛型
  28. * @return
  29. */
  30. public static <T> ResultVO<T> success(T t){
  31. ResultVO<T> resultVO = new ResultVO<>();
  32. resultVO.setCode(10);
  33. resultVO.setData(t);
  34. return resultVO;
  35. }
  36. /**
  37. * 成功的执行
  38. * @param msg 备注
  39. * @param <T> 泛型
  40. * @return
  41. */
  42. public static <T> ResultVO<T> success(String msg){
  43. ResultVO<T> resultVO = new ResultVO<>();
  44. resultVO.setCode(10);
  45. resultVO.setMsg(msg);
  46. return resultVO;
  47. }
  48. /**
  49. * 失败的执行
  50. * @param t 数据体
  51. * @param msg 备注
  52. * @param <T> 泛型
  53. * @return
  54. */
  55. public static <T> ResultVO<T> errot(T t,String msg){
  56. ResultVO<T> resultVO = new ResultVO<>();
  57. resultVO.setCode(11);
  58. resultVO.setData(t);
  59. resultVO.setMsg(msg);
  60. return resultVO;
  61. }
  62. /**
  63. * 失败的执行
  64. * @param msg 备注
  65. * @param <T> 泛型
  66. * @return
  67. */
  68. public static <T> ResultVO<T> errot(String msg){
  69. ResultVO<T> resultVO = new ResultVO<>();
  70. resultVO.setCode(11);
  71. resultVO.setMsg(msg);
  72. return resultVO;
  73. }
  74. /**
  75. * 校验ResultVO结果
  76. * 如果code==10则成功其余则失败
  77. * @param resultVO
  78. * @return
  79. */
  80. public static <T> T checkData(ResultVO<T> resultVO){
  81. //判断返回结果是否是SuperEntity的子类
  82. //11为异常,失败
  83. if(resultVO.getCode()==11){
  84. throw new RuntimeException(resultVO.getMsg());
  85. }
  86. if(resultVO.getCode()==10){
  87. return resultVO.getData();
  88. }
  89. throw new RuntimeException("调用失败");
  90. }
  91. /**
  92. * 校验ResultVO结果
  93. * 如果code==10则成功其余则失败
  94. * @param resultVO
  95. * @return
  96. */
  97. public static String checkMsg(ResultVO<String> resultVO){
  98. //判断返回结果是否是SuperEntity的子类
  99. //11为异常,失败
  100. if(resultVO.getCode()==11){
  101. throw new RuntimeException(resultVO.getMsg());
  102. }
  103. if(resultVO.getCode()==10){
  104. return resultVO.getMsg();
  105. }
  106. throw new RuntimeException("调用失败");
  107. }
  108. }

然后修改AccessDeniedHandler和TokenExceptionHandler,这两个类是无权限和token异常

修改response.setContentType为response.setContentType("application/json; charset=utf-8");

这样安全框架这块就算修改完了。

修改gateway,创建上文中的filter文件夹。

并创建一个filter类

  1. package com.cloudalibaba.gateway.filter;
  2. import com.cloudalibaba.gateway.common.methods.Backtrack;
  3. import com.cloudalibaba.gateway.common.vo.ResultVO;
  4. import com.cloudalibaba.gateway.feign.SecuritypePermissionFeign;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
  8. import org.springframework.cloud.gateway.filter.GlobalFilter;
  9. import org.springframework.core.Ordered;
  10. import org.springframework.http.HttpHeaders;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.server.reactive.ServerHttpRequest;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.server.ServerWebExchange;
  15. import reactor.core.publisher.Mono;
  16. import java.util.Date;
  17. import java.util.List;
  18. /**
  19. * @author wqy
  20. * @version 1.0
  21. * @date 2020/8/1 15:23
  22. */
  23. @Component
  24. @Slf4j
  25. public class MyLogGateWayFilter implements GlobalFilter, Ordered {
  26. @Autowired
  27. private SecuritypePermissionFeign securitypePermissionFeign;
  28. @Override
  29. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  30. log.info("********come in MyLogGatewayFilter:" + new Date());
  31. ServerHttpRequest request = exchange.getRequest();
  32. System.out.println(request.getPath());
  33. System.out.println(request.getQueryParams());
  34. System.out.println(request.getHeaders().get("token"));
  35. HttpHeaders headers = request.getHeaders();
  36. List<String> strs = headers.get("token");
  37. //获取token并验证
  38. String token = strs.get(0);
  39. System.out.println(token);
  40. Boolean check = Backtrack.checkData(securitypePermissionFeign.verificationToken(token));
  41. System.out.println(check);
  42. return chain.filter(exchange);
  43. }
  44. @Override
  45. public int getOrder() {
  46. return 0;
  47. }
  48. }

创建feign,指向securitypermission

  1. package com.cloudalibaba.gateway.feign;
  2. import com.cloudalibaba.gateway.common.vo.ResultVO;
  3. import com.cloudalibaba.gateway.config.CustomizedConfiguration;
  4. import org.springframework.cloud.openfeign.FeignClient;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. /**
  9. * @author wqy
  10. * @version 1.0
  11. * @date 2020/8/1 16:04
  12. */
  13. @FeignClient(value = "securitypermission")
  14. public interface SecuritypePermissionFeign {
  15. /**
  16. * 验证token
  17. * @param token
  18. * @return
  19. */
  20. @RequestMapping(value = "/verificationToken",method = {RequestMethod.POST})
  21. ResultVO<Boolean> verificationToken(@RequestParam("token") String token);
  22. }

这样按正常来说是可以进行调用了,但是!!!,这个是gateway,这样会报错(HttpMessageConverters),请求没问题,但是接受返回值的时候问题就来了,gateway没有对应的解析器去解析response(也可能是默认用错了解析器)。所以要进行一个转换。

  1. package com.cloudalibaba.gateway.config;
  2. import org.springframework.beans.factory.ObjectProvider;
  3. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  4. import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
  5. import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.http.converter.HttpMessageConverter;
  9. import reactor.core.publisher.Mono;
  10. import java.util.Objects;
  11. import java.util.stream.Collectors;
  12. /**
  13. * 转换器
  14. * gateway通过feign调用的时候,gateway中没有相应的解析器(返回),所以会出现异常(HttpMessageConverters)
  15. * 然后将请求转成HttpMessageConverters即可
  16. * @author wqy
  17. */
  18. @Configuration
  19. public class GatewayConfig {
  20. @Bean
  21. KeyResolver userKeyResolver() {
  22. return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress());
  23. }
  24. @Bean
  25. @ConditionalOnMissingBean
  26. public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
  27. return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
  28. }
  29. }

这样就可以正常的访问了。

最后奉上代码

https://github.com/WPZC/cloudalibaba

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/83449
推荐阅读
相关标签
  

闽ICP备14008679号