首页 » Latest Post

webflux的mvc中添加cookie,和spring mvc 不太一样,需要再controller中的方法中加入ServerWebExchange exchange 参数,如下:

@RequestMapping("admin/login")
public CommonResult<AdminLoginResponseData> login(@RequestBody AdminLoginForm adminLoginForm, @ApiIgnore ServerWebExchange exchange) {
    CommonResult<AdminLoginResponseData> result = CommonResult.getInstance();
    if (checkAuth(adminLoginForm)) {
    

        ResponseCookie responseCookie = ResponseCookie.from("token", loginRecord.getToken()).build();
        exchange.getResponse().addCookie(responseCookie);

        AdminLoginResponseData responseData = AdminLoginResponseData.ofAdmin();
        result.setData(responseData);
    }
    return result;
}

其中的关键代码

 ResponseCookie responseCookie = ResponseCookie.from("token", loginRecord.getToken()).build();
        exchange.getResponse().addCookie(responseCookie);

java 查看版本的命令大家再熟悉不过了

java -version

但是java 11的写法是这样的

java --version

其实java 11 的才是比较标准的linux命令,linux中,一个杠代表短命令,比如-v,两个杠表示全拼命令 --version,这个困扰圈圈很久的问题,居然意外发现改了,算是一个小惊喜

请直接参考这个链接文章 http://greenhtml.com/archives/eureka-zhilian.html

1 服务端code 请直接参考这个链接文章 http://greenhtml.com/archives/eureka-zhilian.html

@RequestMapping("/user/InfoByCode")
        @ResponseBody
        @ApiOperation(value = "gateway 获取用户ID,用于放到header里面", httpMethod = "POST")
        public UserInfoView InfoByCode(@RequestParam("code") String code) {
            return UserInfoView.of(users.findFirstByInvitationCode(code));
        }

2 客户端调用直接指定url 请直接参考这个链接文章 http://greenhtml.com/archives/eureka-zhilian.html

@FeignClient(value = "THORUSER",url = "http://localhost:3331")
public interface UserClient {
  @RequestMapping("/user/InfoByCode")
   UserInfoView InfoByCode(@RequestParam("code") String code);

}

3 这样就不经过eureka,直连微服务了。 请直接参考这个链接文章 http://greenhtml.com/archives/eureka-zhilian.html

注意点:不要再返回 chain.filter...,直接return response.setComplete,这样就返回401 了,否则就会继续网后面执行,可能会返回200了。笔者就犯了这个错误

 
  @Component
    public class Session2header extends AbstractNameValueGatewayFilterFactory {
    
        private static final Logger log = LoggerFactory.getLogger(GatewayFilterFactory.class);
    
    
    
        @Override
        public GatewayFilter apply(NameValueConfig config) {
            return (exchange, chain) -> {
    
        //get UID
        ...
                if (!isSucess) {
                    ServerHttpResponse response = exchange.getResponse();
                    response.setStatusCode(HttpStatus.UNAUTHORIZED);
                    return response.setComplete();
    //                return chain.filter(exchange.mutate().response(response).build());
                }

                ServerHttpRequest request = exchange.getRequest().mutate()
                        .header("user_id", uid)
                        .build();
                return chain.filter(exchange.mutate().request(request).build());
            };
        }
    
        private String getUidFromRedis(String token) {
            return userClient.IdByToken(token);
        }
    }