美文网首页
spring cloud中使用Basic验证

spring cloud中使用Basic验证

作者: pilisiyang | 来源:发表于2019-11-18 11:51 被阅读0次

    eureka注册的时候使用Basic验证

    在 bootstrap.properties 修改注册 eureka 配置

    eureka.client.serviceUrl.defaultZone=http://${用户名}:${密码}@127.0.0.1:8888/eureka/
    

    spring security配置Basic验证

    在 bootstrap.properties 配置

    management.security.enabled=true
    security.user.name=${用户名}
    security.user.password=${密码}
    

    Feign调用时带上Basic验证

    首先配置 Feign 的 Configuration 类

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.context.annotation.Scope;
    
    import feign.auth.BasicAuthRequestInterceptor;
    import feign.codec.Encoder;
    import feign.form.spring.SpringFormEncoder;
    
    /**
     * @author pilsy
     */
    @Configuration
    public class FeignClientConfiguration {
    
        @Value("${security.user.name}")
        private String username;
    
        @Value("${security.user.password}")
        private String password;
    
        @Bean
        public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
            return new BasicAuthRequestInterceptor(username, password);
        }
        
        @Bean
        @Primary
        @Scope("prototype")
        public Encoder multipartFormEncoder() {
            return new SpringFormEncoder();
        }
    
        @Bean
        public feign.Logger.Level multipartLoggerLevel() {
            return feign.Logger.Level.FULL;
        }
    }
    

    在使用时,需要在@FeignClient配置此configuration。

    RestTemplate请求带上Basic验证

    首先定义ClientHttpRequestInterceptor,拦截请求配置Basic验证。

    import org.apache.commons.codec.binary.Base64;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpRequest;
    import org.springframework.http.client.ClientHttpRequestExecution;
    import org.springframework.http.client.ClientHttpRequestInterceptor;
    import org.springframework.http.client.ClientHttpResponse;
    
    import java.io.IOException;
    
    /**
     * RestTemplate的基础验证拦截器
     *
     * @author plsy
     */
    public class AuthorizationInterceptor implements ClientHttpRequestInterceptor {
    
        @Value("${security.user.name}")
        private String username;
    
        @Value("${security.user.password}")
        private String password;
    
        @Override
        public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
            HttpHeaders headers = request.getHeaders();
            String plainCreds = username + ":" + password;
            String base64Creds = new String(Base64.encodeBase64(plainCreds.getBytes()));
            headers.add(HttpHeaders.AUTHORIZATION, "Basic " + base64Creds);
            return execution.execute(request, body);
        }
    
    }
    
    

    配置 RestTemplate

    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.web.client.RestOperations;
    import org.springframework.web.client.RestTemplate;
    
    import java.nio.charset.Charset;
    import java.util.Collections;
    
    /**
     * restTemplate 的工厂类
     *
     * @author plsy
     */
    @Configuration
    public class RestTemplateConfig {
    
        @Bean
        public RestTemplate restTemplate() {
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setReadTimeout(60000);
            requestFactory.setConnectTimeout(60000);
    
            RestTemplate restTemplate = new RestTemplate(requestFactory);
            // 使用 utf-8 编码集的
            restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
            //设置基础验证拦截器
            restTemplate.setInterceptors(Collections.singletonList(new AuthorizationInterceptor()));
    
            return restTemplate;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:spring cloud中使用Basic验证

          本文链接:https://www.haomeiwen.com/subject/vxivictx.html