登录

路由配置信息:使用Spring 5的函数式web框架
/*
 *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
 *  <p>
 *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  <p>
 * https://www.gnu.org/licenses/lgpl.html
 *  <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.smartdata.gateway.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

import com.smartdata.gateway.handler.HystrixFallbackHandler;
import com.smartdata.gateway.handler.ImageCodeHandler;

/**
 * @author heluwei
 * @date 2019/2/1
 * 路由配置信息
 */
@Slf4j
@Configuration
@AllArgsConstructor //使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数
public class RouterFunctionConfiguration {
    private final HystrixFallbackHandler hystrixFallbackHandler;
    private final ImageCodeHandler imageCodeHandler;

    @Bean
    public RouterFunction routerFunction() {
        return RouterFunctions.route(
            RequestPredicates.path("/fallback")
                .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler)
            .andRoute(RequestPredicates.GET("/code")
                .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler);

    }

}
 1 /*
 2  *  Copyright (c) 2019-2020, 冷冷 (wangiegie@gmail.com).
 3  *  <p>
 4  *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 5  *  you may not use this file except in compliance with the License.
 6  *  You may obtain a copy of the License at
 7  *  <p>
 8  * https://www.gnu.org/licenses/lgpl.html
 9  *  <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.smartdata.gateway.handler;
18 
19 import com.google.code.kaptcha.Producer;
20 import com.smartdata.common.core.constant.CommonConstants;
21 
22 import lombok.AllArgsConstructor;
23 import lombok.extern.slf4j.Slf4j;
24 import org.springframework.core.io.ByteArrayResource;
25 import org.springframework.data.redis.core.RedisTemplate;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.MediaType;
28 import org.springframework.stereotype.Component;
29 import org.springframework.util.FastByteArrayOutputStream;
30 import org.springframework.web.reactive.function.BodyInserters;
31 import org.springframework.web.reactive.function.server.HandlerFunction;
32 import org.springframework.web.reactive.function.server.ServerRequest;
33 import org.springframework.web.reactive.function.server.ServerResponse;
34 import reactor.core.publisher.Mono;
35 
36 import javax.imageio.ImageIO;
37 import java.awt.image.BufferedImage;
38 import java.io.IOException;
39 import java.util.concurrent.TimeUnit;
40 
41 /**
42  * @author lengleng
43  * @date 2019/2/1
44  * 验证码生成逻辑处理类
45  */
46 @Slf4j
47 @Component
48 @AllArgsConstructor
49 public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
50     private final Producer producer;  //google的验证码生成工具
51     private final RedisTemplate redisTemplate;
52 
53     @Override
54     public Mono<ServerResponse> handle(ServerRequest serverRequest) {
55         //生成验证码
56         String text = producer.createText();
57         BufferedImage image = producer.createImage(text);
58 
59         //保存验证码信息,保存到redis中。key:DEFAULT_CODE_KEY 时间是60
60         String randomStr = serverRequest.queryParam("randomStr").get();
61         redisTemplate.opsForValue().set(CommonConstants.DEFAULT_CODE_KEY + randomStr, text, 60, TimeUnit.SECONDS);
62 
63         // 转换流信息写出
64         FastByteArrayOutputStream os = new FastByteArrayOutputStream();
65         try {
66             ImageIO.write(image, "jpeg", os);
67         } catch (IOException e) {
68             log.error("ImageIO write err", e);
69             return Mono.error(e);
70         }
71 
72         return ServerResponse
73             .status(HttpStatus.OK)
74             .contentType(MediaType.IMAGE_JPEG)
75             .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));
76     }
77 }
 1 /*
 2  *  Copyright (c) 2019-2020 (wangiegie@gmail.com).
 3  *  <p>
 4  *  Licensed under the GNU Lesser General Public License 3.0 (the "License");
 5  *  you may not use this file except in compliance with the License.
 6  *  You may obtain a copy of the License at
 7  *  <p>
 8  * https://www.gnu.org/licenses/lgpl.html
 9  *  <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.smartdata.gateway.handler;
18 
19 import com.google.code.kaptcha.Producer;
20 import com.smartdata.common.core.constant.CommonConstants;
21 
22 import lombok.AllArgsConstructor;
23 import lombok.extern.slf4j.Slf4j;
24 import org.springframework.core.io.ByteArrayResource;
25 import org.springframework.data.redis.core.RedisTemplate;
26 import org.springframework.http.HttpStatus;
27 import org.springframework.http.MediaType;
28 import org.springframework.stereotype.Component;
29 import org.springframework.util.FastByteArrayOutputStream;
30 import org.springframework.web.reactive.function.BodyInserters;
31 import org.springframework.web.reactive.function.server.HandlerFunction;
32 import org.springframework.web.reactive.function.server.ServerRequest;
33 import org.springframework.web.reactive.function.server.ServerResponse;
34 import reactor.core.publisher.Mono;
35 
36 import javax.imageio.ImageIO;
37 import java.awt.image.BufferedImage;
38 import java.io.IOException;
39 import java.util.concurrent.TimeUnit;
40 
41 /**
42  * @author lengleng
43  * @date 2019/2/1
44  * 验证码生成逻辑处理类
45  */
46 @Slf4j
47 @Component
48 @AllArgsConstructor
49 public class ImageCodeHandler implements HandlerFunction<ServerResponse> {
50     private final Producer producer;  //google的验证码生成工具
51     private final RedisTemplate redisTemplate;
52 
53     @Override
54     public Mono<ServerResponse> handle(ServerRequest serverRequest) {
55         //生成验证码
56         String text = producer.createText();
57         BufferedImage image = producer.createImage(text);
58 
59         //保存验证码信息,保存到redis中。key:DEFAULT_CODE_KEY 时间是60
60         String randomStr = serverRequest.queryParam("randomStr").get();
61         redisTemplate.opsForValue().set(CommonConstants.DEFAULT_CODE_KEY + randomStr, text, 60, TimeUnit.SECONDS);
62 
63         // 转换流信息写出
64         FastByteArrayOutputStream os = new FastByteArrayOutputStream();
65         try {
66             ImageIO.write(image, "jpeg", os);
67         } catch (IOException e) {
68             log.error("ImageIO write err", e);
69             return Mono.error(e);
70         }
71 
72         return ServerResponse
73             .status(HttpStatus.OK)
74             .contentType(MediaType.IMAGE_JPEG)
75             .body(BodyInserters.fromResource(new ByteArrayResource(os.toByteArray())));
76     }
77 }
原文地址:https://www.cnblogs.com/bulrush/p/12573178.html